Python,使用list,查找最大序列长度

时间:2014-06-25 14:02:36

标签: python algorithm list

例如test_list:

test_list = ['a', 'a', 'a', 'b', 'b', 'a', 'c', 'b', 'a', 'a']

我需要使用哪种工具或算法来获取最大序列数,例如:

'a' = 3
'b' = 2
'c = 1

5 个答案:

答案 0 :(得分:5)

使用dict跟踪最大长度,itertools.groupby按顺序对序列进行分组:

from itertools import groupby

max_count = {}

for val, grp in groupby(test_list):
    count = sum(1 for _ in grp)
    if count > max_count.get(val, 0):
        max_count[val] = count

演示:

>>> from itertools import groupby
>>> test_list = ['a', 'a', 'a', 'b', 'b', 'a', 'c', 'b', 'a', 'a']
>>> max_count = {}
>>> for val, grp in groupby(test_list):
...     count = sum(1 for _ in grp)
...     if count > max_count.get(val, 0):
...         max_count[val] = count
... 
>>> max_count
{'a': 3, 'c': 1, 'b': 2}

答案 1 :(得分:1)

这是直接的方法:

Counts, Count, Last_item = {}, 0, None
test_list = ['a', 'a', 'a', 'b', 'b', 'a', 'c', 'b', 'a', 'a']
for item in test_list:
   if Last_item == item:
       Count+=1
   else:
       Count=1
       Last_item=item
   if Count>Counts.get(item, 0):
       Counts[item]=Count

print Counts
# {'a': 3, 'c': 1, 'b': 2}

答案 2 :(得分:0)

您应该阅读字典是什么(dict中的Python)以及如何存储序列的出现次数。

然后弄清楚如何编写逻辑 -

Figure out how to loop over your list.  As you go, for every item -
     If it isn't the same as the previous item
         Store how many times you saw the previous item in a row into the dictionary
     Else
         Increment how many times you've seen the item in the current sequence

Print your results

答案 3 :(得分:0)

您可以使用re模块查找列表中所有字符组成的字符串中所有字符序列。然后只选择单个字符的最大字符串。

import re

test_list = ['a', 'a', 'b', 'b', 'a', 'c', 'b', 'a', 'a', 'a']

# First obtain the characters.
unique  = set(test_list)

max_count = {}

for elem in unique:
    # Find all sequences for the same character.
    result = re.findall('{0}+'.format(elem), "".join(test_list))
    # Find the longest.
    maximun = max(result)
    # Save result.
    max_count.update({elem: len(maximun)})

print(max_count)

这将打印:{'c': 1, 'b': 2, 'a': 3}

答案 4 :(得分:0)

对于Python,Martijn Pieters' groupby是最佳答案。

也就是说,这是一种“基本”方式,可以翻译成任何语言:

test_list = ['a', 'a', 'a', 'b', 'b', 'a', 'c', 'b', 'a', 'a']

hm={}.fromkeys(set(test_list), 0)
idx=0
ll=len(test_list)  
while idx<ll:
    item=test_list[idx]
    start=idx
    while idx<ll and test_list[idx]==item:
        idx+=1
    end=idx
    hm[item]=max(hm[item],end-start)    


print hm 
# {'a': 3, 'c': 1, 'b': 2}