例如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}
你应该读一下字典是什么(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
您可以使用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来说,martinjn 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}