nikinlpds
nikinlpds

Reputation: 411

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}

Upvotes: 0

Views: 47

Answers (4)

Sumukh Barve
Sumukh Barve

Reputation: 1444

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.

Upvotes: 1

alec_djinn
alec_djinn

Reputation: 10789

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}

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71451

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}

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336148

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})

Upvotes: 2

Related Questions