在列表中识别连续重复项最多的Pythonic方法是什么? [英] What's the most Pythonic way to identify consecutive duplicates in a list?

查看:114
本文介绍了在列表中识别连续重复项最多的Pythonic方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数列表,我想要能够识别出连续的重复区块:也就是说,我想产生一个包含每个副本的复制的排序保留列表(int_in_question,出现次数)。



例如,如果我有一个列表,如:

  0,0,0,3,3,2,5,2,6,6] 

希望结果为:

  [(0,3),(3,2),(2,1),( 5,1),(2,1),(6,2)] 

em>公平地使用for循环,临时表和计数器执行此操作的简单方法:

  result_list = [] 
current = source_list [0]
count = 0
在source_list中的值:
如果value == current:
count + = 1
else:
result_list.append((current,count))
current = value
count = 1
result_list.append((current,count))

但我真的很喜欢python的fu指令编程习语,我想用一个简单的生成器表达式来做到这一点。但是,在使用发电机时,我发现很难保留分计数。我有一个两步的过程可能会让我在那里,但现在我被困了。



有没有一个特别优雅/ pythonic的方式来做到这一点,特别是与发生器?

解决方案

 >>>来自itertools import groupby 
>>>> L = [0,0,0,3,3,2,5,2,6,6]
>>>对于组(L)中的k,g,分组_L= [(k,i代表i中的1))
>>>> #or(k,len(list(g))),但是创建一个中间列表
>>>分组_
[(0,3),(3,2),(2,1),(5,1),(2,1),(6,2)]

电池包括,正如他们所说。



建议使用 sum 和来自JBernardo的生成器表达式;看到评论。


I've got a list of integers and I want to be able to identify contiguous blocks of duplicates: that is, I want to produce an order-preserving list of duples where each duples contains (int_in_question, number of occurrences).

For example, if I have a list like:

[0, 0, 0, 3, 3, 2, 5, 2, 6, 6]

I want the result to be:

[(0, 3), (3, 2), (2, 1), (5, 1), (2, 1), (6, 2)]

I have a fairly simple way of doing this with a for-loop, a temp, and a counter:

result_list = []
current = source_list[0]
count = 0
for value in source_list:
    if value == current:
        count += 1
    else:
        result_list.append((current, count))
        current = value
        count = 1
result_list.append((current, count))

But I really like python's functional programming idioms, and I'd like to be able to do this with a simple generator expression. However I find it difficult to keep sub-counts when working with generators. I have a feeling a two-step process might get me there, but for now I'm stumped.

Is there a particularly elegant/pythonic way to do this, especially with generators?

解决方案

>>> from itertools import groupby
>>> L = [0, 0, 0, 3, 3, 2, 5, 2, 6, 6]
>>> grouped_L = [(k, sum(1 for i in g)) for k,g in groupby(L)]
>>> # Or (k, len(list(g))), but that creates an intermediate list
>>> grouped_L
[(0, 3), (3, 2), (2, 1), (5, 1), (2, 1), (6, 2)]

Batteries included, as they say.

Suggestion for using sum and generator expression from JBernardo; see comment.

这篇关于在列表中识别连续重复项最多的Pythonic方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆