user2950162
user2950162

Reputation: 1011

Frequency distribution of values in tuples meeting a condition inside a list of lists

I have the following list of list of tuples:

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

I would like to get the frequency each value appears as the first item of the tuple when the second item of the tuple is "".

Something like this for the above example:

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

Upvotes: 2

Views: 819

Answers (1)

DSM
DSM

Reputation: 353339

I'd do it using 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})

This works because Counter counts things that it's fed:

>>> Counter(["QQ", "QQ", "AA", "CC"])
Counter({'QQ': 2, 'AA': 1, 'CC': 1})

itertools.chain.from_iterable can be used to flatten data:

>>> list(chain.from_iterable(data))
[('AA', 'AA'), ('QQ', ''), ('CC', ''), ('QQ', '')]

And we use the genexp to select only the terms where the second is empty (which for strings can be written simply if not somestring):

>>> list(left for left, right in chain.from_iterable(data) if not right)
['QQ', 'CC', 'QQ']

Upvotes: 7

Related Questions