[英]Python: determine length of sequence of equal items in list
I have a list as follows: 我有一个列表如下:
l = [0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,2,2,2]
I want to determine the length of a sequence of equal items, ie for the given list I want the output to be: 我想确定一系列相等项的长度,即对于给定的列表,我希望输出为:
[(0, 6), (1, 6), (0, 4), (2, 3)]
(or a similar format). (或类似的格式)。
I thought about using a defaultdict
but it counts the occurrences of each item and accumulates it for the entire list, since I cannot have more than one key '0'. 我考虑使用
defaultdict
但它会计算每个项目的出现次数,并为整个列表累积它,因为我不能有多个键'0'。
Right now, my solution looks like this: 现在,我的解决方案如下所示:
out = []
cnt = 0
last_x = l[0]
for x in l:
if x == last_x:
cnt += 1
else:
out.append((last_x, cnt))
cnt = 1
last_x = x
out.append((last_x, cnt))
print out
I am wondering if there is a more pythonic way of doing this. 我想知道是否有更多的pythonic方式来做到这一点。
You almost surely want to use itertools.groupby : 你几乎肯定想使用itertools.groupby :
l = [0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,2,2,2]
answer = []
for key, iter in itertools.groupby(l):
answer.append((key, len(list(iter))))
# answer is [(0, 6), (1, 6), (0, 4), (2, 3)]
If you want to make it more memory efficient, yet add more complexity, you can add a length function: 如果您想提高内存效率,增加更多复杂性,可以添加一个长度函数:
def length(l):
if hasattr(l, '__len__'):
return len(l)
else:
i = 0
for _ in l:
i += 1
return i
l = [0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,2,2,2]
answer = []
for key, iter in itertools.groupby(l):
answer.append((key, length(iter)))
# answer is [(0, 6), (1, 6), (0, 4), (2, 3)]
Note though that I have not benchmarked the length() function, and it's quite possible it will slow you down. 请注意,虽然我没有对length()函数进行基准测试,但它很可能会减慢你的速度。
Mike's answer is good, but the itertools._grouper
returned by groupby will never have a __len__
method so there is no point testing for it 迈克的答案很好,但是groupby返回的
itertools._grouper
永远不会有__len__
方法,所以没有点测试它
I use sum(1 for _ in i)
to get the length of the itertools._grouper 我使用
sum(1 for _ in i)
来获取itertools._grouper的长度
>>> import itertools as it
>>> L = [0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,2,2,2]
>>> [(k, sum(1 for _ in i)) for k, i in it.groupby(L)]
[(0, 6), (1, 6), (0, 4), (2, 3)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.