Panupat
Panupat

Reputation: 462

Python: create dictionary with multi list comprehension

Is there a way I can use list comprehension answered in this thread to create dictionary?

listA = [
    "apple_v001",
    "apple_v002",
    "banana_v001",
    "orange_v001",
]
keywords = ["apple", "banana", "orange"]
[[item for item in listA if kw in item] for kw in keywords]
# Result: [['apple_v001', 'apple_v002'], ['banana_v001'], ['orange_v001']] # 

What I'm trying to do is create a dictionary using keywords as key out of this result. So

dictA["apple"] = ['apple_v001', 'apple_v002']

and so forth. I tried to do the dict = {key, value for ...(iteration) } but always get a syntax error. I really don't know how to start, any help appreciate.

Upvotes: 1

Views: 1241

Answers (4)

John La Rooy
John La Rooy

Reputation: 304413

{kw: [item for item in listA if kw in item] for kw in keywords}

But this doesn't seem a particularly efficient way to create such a dict

For example this doesn't need the list of keywords in advance and is reasonably efficient

>>> from itertools import groupby
>>> {k:list(g) for k,g in groupby(sorted(listA), key=lambda x:x.partition('_')[0])}
{'orange': ['orange_v001'], 'apple': ['apple_v001', 'apple_v002'], 'banana': ['banana_v001']}

For Python2.6 the equivalent is

dict((kw, [item for item in listA if kw in item]) for kw in keywords)

and

>>> from itertools import groupby
>>> dict((k,list(g)) for k,g in groupby(sorted(listA), key=lambda x:x.partition('_')[0]))
{'orange': ['orange_v001'], 'apple': ['apple_v001', 'apple_v002'], 'banana': ['banana_v001']}

Upvotes: 2

dawg
dawg

Reputation: 104082

You can use a regex:

>>> import re
>>> listA = [
...     "apple_v001",
...     "apple_v002",
...     "banana_v001",
...     "orange_v001",
... ]
>>> keywords = ["apple", "banana", "orange"]
>>> s=' '.join(listA)
>>> dict([(e,re.findall(r'{}_v\d+'.format(e),s)) for e in keywords])
{'orange': ['orange_v001'], 'apple': ['apple_v001', 'apple_v002'], 'banana': ['banana_v001']}

Or (post Python 2.7) a dict comprehension:

>>> {e:re.findall(r'{}_v\d+'.format(e),s) for e in keywords}
{'orange': ['orange_v001'], 'apple': ['apple_v001', 'apple_v002'], 'banana': ['banana_v001']}

Upvotes: 0

jfs
jfs

Reputation: 414745

In the comments you mentioned that you use Python 2.6. There is no dict comprehension in Python 2.6, you could use dict() with a generator expresion instead:

d = dict((kw, [item for item in listA if kw in item]) for kw in keywords)

Here's a possibly more efficient version:

import re
from collections import defaultdict

search_word = re.compile("(%s)" % "|".join(map(re.escape, keywords))).search

d = defaultdict(list)
for item in listA:
    m = search_word(item)
    if m:
       d[m.group(1)].append(item)

If listA is always in the format given in the question:

from collections import defaultdict

keywords = set(keywords)
d = defaultdict(list)
for item in listA:
    word = item.partition("_")[0]
    if word in keywords:
       d[word].append(item)

If listA doesn't contain items that are not in keywords:

from collections import defaultdict

d = defaultdict(list)
for item in listA:
    d[item.partition('_')[0]].append(item)

Upvotes: 1

avasal
avasal

Reputation: 14864

if you don't want to go for one liner solution, check this

In [58]: d
Out[58]: defaultdict(<type 'list'>, {})

In [59]: for elem in keywords:
   ....:     for item in listA:
   ....:         if item.startswith(elem):
   ....:             d[elem].append(item)
   ....:

In [60]: d
Out[60]: defaultdict(<type 'list'>, {'orange': ['orange_v001'], 'apple': ['apple_v001', 'apple_v002'], 'banana': ['banana_v001']})

Upvotes: 2

Related Questions