简体   繁体   English

Python,使用列表,找到最大序列长度

[英]Python, work with list, find max sequence length

for example test_list: 例如test_list:

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

what tool or algorithm i need to use, to get max sequences count, for this example: 对于此示例,我需要使用哪种工具或算法来获得最大序列数:

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

Using a dict to track max lengths, and itertools.groupby to group the sequences by consecutive value: 使用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

Demo: 演示:

>>> 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}

Here is a direct way to do it: 这是直接的方法:

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}

You should read about what a dictionary is ( dict in Python ) and how you could store how many occurrences there are for a sequence. 你应该了解什么是字典( dictPython ),你怎么能存储多少次有一个序列。

Then figure out how to code the logic - 然后弄清楚如何编码逻辑-

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

You can use re module for find all sequences of the character in a string composed by all the characters in your list. 您可以使用re模块查找由列表中所有字符组成的字符串中的所有字符序列。 Then just pick the largest string for a single character. 然后,只需为单个字符选择最大的字符串即可。

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)

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

For Python, Martijn Pieters' groupby is the best answer. 对于Python, Martijn Pieters的groupby是最好的答案。

That said, here is a 'basic' way to do it that could be translated to any language: 就是说,这是一种可以将其翻译成任何语言的“基本”方法:

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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM