我有以下元组列表列表:
[[("AA","AA"),("QQ","")],[("CC",""),("QQ","")],...]
我想得到当元组的第二项为""
时,每个值显示为元组的第一项的频率。
以上示例中的内容如下:
{"QQ":2, "CC":1}
答案 0 :(得分:6)
我会使用collections.Counter
+ itertools.chain
:
>>> data = [[("AA","AA"),("QQ","")],[("CC",""),("QQ","")]]
>>> from itertools import chain
>>> from collections import Counter
>>> Counter(left for left, right in chain.from_iterable(data) if not right)
Counter({'QQ': 2, 'CC': 1})
这是有效的,因为Counter
计算它所喂食的东西:
>>> Counter(["QQ", "QQ", "AA", "CC"])
Counter({'QQ': 2, 'AA': 1, 'CC': 1})
itertools.chain.from_iterable
可用于展平data
:
>>> list(chain.from_iterable(data))
[('AA', 'AA'), ('QQ', ''), ('CC', ''), ('QQ', '')]
我们使用genexp只选择第二个为空的术语(对于字符串只能写成if not somestring
):
>>> list(left for left, right in chain.from_iterable(data) if not right)
['QQ', 'CC', 'QQ']