0

I have a mapping keywords like this.

categories_mapping = {
        'comics': 'Comic Books',
        'cartoons': 'Comic Books',
        'manga': 'Comic Books',
        'video and computer games': 'Video Games',
        'role playing games': 'Video Games',
        'immigration': 'Immigration',
        'police': 'Police',
        'environmental': 'Environment',
        'celebrity fan and gossip': 'Celebrity',
        'space and technology': 'NASA / Space',
        'movies and tv': 'TV and Movies',
        'elections': 'Elections',
        'referendums': 'Elections',
        'sex': 'Sex',
        'music': 'Music',
        'technology and computing': 'Technology'}

and a list like this.

labels = ['technology and computing', 'arts and technology']

I want to return the value of the dictionary if any words in the list is in the key of the dictionary.

This is what I've come up with but I think that's not very pythonic.

cats = []
for k,v in categories_mapping.items():
    for l in labels:
        if k in l:
            cats.append(v)
return cats

The result I want is ['Technology']

Any better way of doing this?

toy
  • 11,711
  • 24
  • 93
  • 176
  • 1
    I'm sorry, what is `labels` here? – BlackVegetable Oct 27 '15 at 16:36
  • 2
    Can you show your expected output? – Anand S Kumar Oct 27 '15 at 16:37
  • You might want to consider using frozensets for your dictionary keys. It will make the lookup faster, and better represent the data. – Chad S. Oct 27 '15 at 16:39
  • Do you mean `for l in labels: if k in l` or do you just mean `if k in labels` ? – khelwood Oct 27 '15 at 16:40
  • Possible duplicate of [Check if a given key already exists in a dictionary](http://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary) – Shahriar Oct 27 '15 at 16:41
  • Are you looking for any _single word_, e.g. `movies` to find the key `'movies and tv'`, or are you looking for exact matches? – TigerhawkT3 Oct 27 '15 at 16:42
  • It cannot be exact match. It can be partially match. – toy Oct 27 '15 at 16:44
  • Okay. Should it ignore case? Is it looking for exact matches of single words (separated by whitespace)? Should it match parts of words? Should it match similar words? – TigerhawkT3 Oct 27 '15 at 16:46
  • Your edit just invalidated every single answer. At this point, I'd recommend rolling back your edits, accepting an answer that was appropriate to the original question, and asking a new question that includes all relevant information. – TigerhawkT3 Oct 27 '15 at 16:49
  • Just rolled back my question and I'll create a new question. – toy Oct 27 '15 at 16:54

5 Answers5

6

You can use intersection of your labels and dictionary keys:

cats = [categories_mapping[key] for key in set(labels).intersection(categories_mapping)]

Update for partial match:

cats = [categories_mapping[key] for key in categories_mapping if any(label.lower() in key.lower() for label in labels)]
Eugene Soldatov
  • 9,755
  • 2
  • 35
  • 43
1
>>> [categories_mapping[l] for l in labels if l in categories_mapping]
['Technology']
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

You can get rid of the outer loop:

for l in labels:
    if l in categories_mapping:
        cats.append(categories_mapping[l])

Or as a list comp:

 cats = [v for k, v in categories_mapping if k in l]
btmcnellis
  • 662
  • 4
  • 10
0
>>> categories_mapping = {
        'comics': 'Comic Books',
        'cartoons': 'Comic Books',
        'manga': 'Comic Books',
        'video and computer games': 'Video Games',
        'role playing games': 'Video Games',
        'immigration': 'Immigration',
        'police': 'Police',
        'environmental': 'Environment',
        'celebrity fan and gossip': 'Celebrity',
        'space and technology': 'NASA / Space',
        'movies and tv': 'TV and Movies',
        'elections': 'Elections',
        'referendums': 'Elections',
        'sex': 'Sex',
        'music': 'Music',
        'technology and computing': 'Technology'}
>>> labels = ['technology and computing', 'arts and technology']
>>> cats = []
>>> for l in labels:
    if l in categories_mapping:
        cats.append(categories_mapping[l])
>>> cats
['Technology']
R Nar
  • 5,465
  • 1
  • 16
  • 32
0
cats = [v for k, v in categories_mapping.items() if k in labels]
Mayur Koshti
  • 1,794
  • 15
  • 20