简体   繁体   English

计算列表中的连续出现次数

[英]Counting sequential occurrences in a list and

I have 3 lists as follows: 我有3个列表如下:

L1 = ['H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'H', 'T']
L2 = ['H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'H', 'T' , 'T', 'H, 'T', 'T', 'T', 'H', 'H', 'H', 'T']
L3 = ['H', 'T', 'H', 'H']

I would like to count sequential occurrences of 'H' in each list and produce the following table showing the frequencies of these 'H' sequences: 我想计算每个列表中连续出现的'H'并生成下表,显示这些'H'序列的频率:

Length | L1 | L2 | L3
----------------------
1         0    1   1
2         1    1   1   
3         0    1   0
4         1    1   0
5         0    0   0

I know that doing the following gives me the frequnecies of a sequence in a list: 我知道执行以下操作可以获得列表中序列的频率:

from itertools import groupby
[len(list(g[1])) for g in groupby(L1) if g[0]=='H']
[2, 4]

But am in need of an elegant way to take this further over the remaining lists and ensuring that a '0' is placed for unobserved lengths. 但是我需要一种优雅的方法来对剩余的列表进行进一步的处理,并确保为未观察到的长度放置'0'。

You can use collections.Counter to create a frequency dict from a generator expression that outputs the lengths of sequences generated by itertools.groupby , and then iterate through a range of possible lengths to output the frequencies from the said dict, with 0 as a default value in absence of a frequency. 您可以使用collections.Counter从生成器表达式创建频率字典,该表达式输出itertools.groupby生成的序列长度,然后迭代range可能的长度以输出来自所述字典的频率,默认值为0没有频率的价值。

Using L1 as an example: L1为例:

from itertools import groupby
from collections import Counter
counts = Counter(sum(1 for _ in g) for k, g in groupby(L1) if k == 'H')
print([counts[length] for length in range(1, 6)])

This outputs: 这输出:

[0, 1, 0, 1, 0]

You can use itertools.groupby with collections.Counter : 您可以将itertools.groupbycollections.Counter一起使用:

import itertools as it, collections as _col
def scores(l):
  return _col.Counter([len(list(b)) for a, b in it.groupby(l, key=lambda x:x == 'H') if a])

L1 = ['H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'H', 'T']
L2 = ['H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'H', 'T' , 'T', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T']
L3 = ['H', 'T', 'H', 'H']
d = {'L1':scores(L1), 'L2':scores(L2), 'L3':scores(L3)}
r = '\n'.join([f'Length | {" | ".join(d.keys())} ', '-'*20]+[f'{i}          {"   ".join(str(b.get(i, 0)) for b in d.values())}' for i in range(1, 6)])
print(r)

Output: 输出:

Length | L1 | L2 | L3 
--------------------
1          0   1   1
2          1   1   1
3          0   1   0
4          1   1   0
5          0   0   0

This might work : 这可能有效:

from itertools import groupby
a = [len(list(v)) if k=='H' and v else 0 for k,v in groupby(''.join(L1))]

For a sample L4 = ['T', 'T'] where there is no 'H' item in list, it returns [0] . 对于样本L4 = ['T', 'T'] ,其中列表中没有'H'项,它返回[0] For L1 it returns [2, 0, 4, 0] . 对于L1它返回[2, 0, 4, 0] For L2 it returns [2, 0, 4, 0, 1, 0, 3, 0] . 对于L2它返回[2, 0, 4, 0, 1, 0, 3, 0] For L3 it returns [1, 0, 2] . 对于L3它返回[1, 0, 2] L3 [1, 0, 2]

请尝试max([len(x) for x in ''.join(y).split('T')])其中y是你的列表。

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

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