简体   繁体   中英

Pythonic way to achieve a list/dict comprehension

Let's say, I have a list of tuples:

lst = [('NP', (2, 4)), ('VP', (1, 4)), ('VP', (0, 4)), 
       ('S-SBJ', (0, 4)), ('ADJP-PRD', (5, 7)), ('VP', (4, 7))]

What would be the most standard/pythonic way to get the following:

out = {(2, 4): 1, (1, 4): 1, (0, 4): 2, (5, 7): 1, (4, 7): 1}

I tried to do:

from collections import defaultdict

d = defaultdict(list)
for item in lst:
    d[item[1]].append(item[0])

dct = {}
for k, v in d.items():
    dct[k] = len(v)

print(dct)
# {(2, 4): 1, (1, 4): 1, (0, 4): 2, (5, 7): 1, (4, 7): 1}

I would use a Counter :

>>> from collections import Counter
>>> lst = [('NP', (2, 4)), ('VP', (1, 4)), ('VP', (0, 4)),
...        ('S-SBJ', (0, 4)), ('ADJP-PRD', (5, 7)), ('VP', (4, 7))]
>>> c = Counter(item[1] for item in lst)
>>> c
Counter({(0, 4): 2, (2, 4): 1, (1, 4): 1, (5, 7): 1, (4, 7): 1})

You can use itertools.groupby :

from itertools import groupby as gb
lst = [('NP', (2, 4)), ('VP', (1, 4)), ('VP', (0, 4)), ('S-SBJ', (0, 4)), ('ADJP-PRD', (5, 7)), ('VP', (4, 7))]
r = {j:i for _, b in gb(lst, key=lambda x:x[0]) for i, j in enumerate(b, 1)}

Output:

{('NP', (2, 4)): 1, ('VP', (1, 4)): 1, ('VP', (0, 4)): 2, ('S-SBJ', (0, 4)): 1, ('ADJP-PRD', (5, 7)): 1, ('VP', (4, 7)): 1}

If you're looking for a pure comprehension:

>>> lst = [('NP', (2, 4)), ('VP', (1, 4)), ('VP', (0, 4)), 
...        ('S-SBJ', (0, 4)), ('ADJP-PRD', (5, 7)), ('VP', (4, 7))]
>>> {tup[1]: sum([1 for itup in lst if itup[1] == tup[1]]) for tup in lst}
{(2, 4): 1, (1, 4): 1, (0, 4): 2, (5, 7): 1, (4, 7): 1}
>>>  

But the above comprehension is rather terse. The readable way would be:

>>> d = {};
>>> for x, y in lst:
...     d[y] = 1 if y not in d else d[y] + 1
... 
>>> d
{(2, 4): 1, (1, 4): 1, (0, 4): 2, (5, 7): 1, (4, 7): 1}
>>> 

Yet alternatively, the OP's defaultdict solution is readable too; but I'd use 0 as the default value and then increment by 1 on each match. That way, you won't need the extra loop for computing len .

The simplest way I see is:

from collections import Counter

d = dict(Counter([i[1] for i in lst]))

It gives exactly the output you asked:

{(2, 4): 1, (1, 4): 1, (0, 4): 2, (5, 7): 1, (4, 7): 1}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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