簡體   English   中英

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

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

例如test_list:

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

對於此示例,我需要使用哪種工具或算法來獲得最大序列數:

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

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

這是直接的方法:

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}

你應該了解什么是字典( dictPython ),你怎么能存儲多少次有一個序列。

然后弄清楚如何編碼邏輯-

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

您可以使用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}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM