满足列表列表内条件的元组中值的频率分布

时间:2013-11-20 18:28:22

标签: python list

我有以下元组列表列表:

[[("AA","AA"),("QQ","")],[("CC",""),("QQ","")],...]

我想得到当元组的第二项为""时,每个值显示为元组的第一项的频率。

以上示例中的内容如下:

{"QQ":2, "CC":1}

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']