如何计算列表列表中相同值的最长序列,然后在元组中输出最大序列 [英] how to count the longest sequence of the same value in a list of lists, and then output the largest sequence in a tuple
问题描述
我有一个文本文件中的列表9的列表),其值类似于以下内容:
I have a list of lists of lists 9in a text file) with values similar to what is below:
L = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
这是我正在使用的功能:
This is the function I'm using:
def longest_sequence(l):
counter = 0
sl = []
sublists = []
for i in l:
if (l[counter+1]==l[counter]):
sl.append(l[counter])
counter = counter + 1
else:
counter = 0
sublists.append([sl[i], len(sl)])
return sublists
现在,在这种情况下,右侧仅计数一个值,该值首先出现(1),然后转到下一个类似于序列的行,而我得到的输出就是这个值:
Right now counts only one value in this case the one that appears first (1) then it goes to the next line which is similar a sequence and the output i'm getting is this one:
returns the sub lists [[1, 111], [1, 222], [1, 333], [1, 444], [1, 555], [1, 666], [1, 777], [1, 888]]
基本上,我想做的是检查列表,并从该列表中验证哪个子列表的长度最长,所以我应该得到如下所示的东西:
basically what I'm trying to do is checking the list and from that list verifying which sub-list has the longest length, so I should be getting something like this instead:
sl = [(1, 111), (0, 395), (1, 65), (2, 358), (1, 71)]
作为第二个元组,返回为ti的值包含在所有子列表中连续重复395次(最长)的值.
Being the second tuple the one returned as ti contains the value that got repeated continuously 395 times (longest length) among all sub lists.
推荐答案
您只需使用 itertools.groupby()
:
In []:
import itertools as it
[(k, sum(1 for _ in g)) for k, g in it.groupby(L)]
# [(k, len(list(g)) for k, g in it.groupby(L)] # alternative
Out[]:
[(1, 112), (0, 394), (1, 65), (2, 359), (1, 71)]
要获得最大值,可以将 max()
与 key
一起使用,例如:
To get the maximum, you can use max()
with a key
, e.g.:
In []:
import operator as op
counts = [(k, sum(1 for _ in g)) for k, g in it.groupby(L)]
max(counts, key=op.itemgetter(1))
Out[]:
(0, 394)
但是,修复您的代码.
- 您在弄乱索引(
counter
)时,在else:
块中重置索引时,会从头开始.只需在for
循环中为索引使用range(1,len(l))
. - 您无需在
else:
块中重置sl
(因此,它会以111
的速度增长),但是您确实不需要要创建sl
列表,只需对项目进行计数 - 您错过了最后一个值的情况
- 处理最后一个值需要对逻辑进行一些重新排序
- You are confusing your indexing (
counter
), when you reset it in theelse:
block you start from the beginning again. Just userange(1, len(l))
in yourfor
loop for the index. - You don't reset
sl
in theelse:
block (hence it keeps growing by111
) but you really don't need to create thesl
list just count the items - You miss the case of the last value
- Dealing with the last value needs a little reordering of logic
如此固定,它看起来像:
So fixed, it would look like:
def longest_sequence(l):
counter = 1
sublists = []
for i in range(1, len(l)):
if l[i] != l[i-1]:
sublists.append([l[i-1], counter])
counter = 0
counter += 1
if counter > 0:
sublists.append((l[i], counter))
return sublists
In []:
longest_sequence(L)
Out[]:
[(1, 112), (0, 394), (1, 65), (2, 359), (1, 71)]
In []:
max(longest_sequence(L), key=op.itemgetter(1))
Out[]:
(0, 394)
这篇关于如何计算列表列表中相同值的最长序列,然后在元组中输出最大序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!