Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dict from 2 dictionaries in Python?

I'm dealing with Python dicts now. I wrote a code:

import random

categories = {1 : "Antics", 2 : "Tickets", 3: "Moviez",
              4 : "Music", 5 : "Photography", 6 : "Gamez", 7 : "Bookz",
              8 : "Jewelry", 9 : "Computers", 10 : "Clothes"}

items = {"Picture" : 1, "Clock" : 1, "Ticket for Mettalica concert" : 2,
         "Ticket for Iron Maiden concert" : 2, "Ticket for Placebo concert" : 2,
         "The pianist" : 3, "Batman" : 3, "Spider-Man" : 3,
         "WoW" : 6, "Cabal" : 6, "Diablo 3" : 6, "Diablo 2" : 6,
         "Thinking in Java" : 7, "Thinking in C++" : 7, "Golden ring" : 8,
         "Asus" : 10, "HP" : 10, "Shoes" : 11}

for key, val in categories :
    for k, v in items :
        if key == v :
            print(val, k)

I wanted to create a 3rd dict, where i would have sth like :

dictThe3rd = {"Antics" : "Picture", "Antics" : "Clock", "Tickets" : "Ticket for Mettalica concert", "Ticket" : "Ticket for Iron Maiden concert", "Ticket" : "Ticket for Placebo concert", ...} 

and so go on.

How to do this? My code shows:

test.py, line 14, in <module>
    for key, val in categories :
TypeError: 'int' object is not iterable
like image 897
Katie Avatar asked Aug 21 '12 11:08

Katie


2 Answers

If you use a dict as an iterator, it will yield only its keys, not tuples of keys and values. You would have to use categories.items() (or categories.iteritems() if you are only using Python 2.x) to get those tuples:

for key, val in categories.items() :
    for k, v in items.items() :
        if key == v :
            print(val, k)

You can use dict comprehensions to get a dict with category names as keys and all items in that category as values:

>>> dict3 = { catname : [ item for item, itemcat in items.items() if itemcat == cat  ] for cat, catname in categories.items()  }
>>> dict3
{'Antics': ['Picture', 'Clock'],
'Bookz': ['Thinking in C++', 'Thinking in Java'],
'Clothes': ['Asus', 'HP'],
'Computers': [],
'Gamez': ['Diablo 3', 'Diablo 2', 'Cabal', 'WoW'],
'Jewelry': ['Golden ring'],
'Moviez': ['The pianist', 'Batman', 'Spider-Man'],
'Music': [],
'Photography': [],
'Tickets': ['Ticket for Mettalica concert',
'Ticket for Placebo concert',
'Ticket for Iron Maiden concert']}

Explanation:

The inner comprehension in square brackets will list all items that have a category of cat. The outer comprehension will iterate over all category keys and names, setting the category name as the new dict key and the inner comprehension as a value.

like image 93
Stefan Seemayer Avatar answered Oct 25 '22 23:10

Stefan Seemayer


Change your code to:

for key, val in categories.iteritems():
    for k, v in items.iteritems():
        if key == v:
            print(val, k)

This way you can iterate over key/value pairs using an itemiterator which yields better performance than items() for large dictionaries.

What you want is possible like this:

import collections
dictThe3rd=collections.defaultdict(set)
for key, val in categories.iteritems():
    for k, v in items.iteritems():
        if key == v:
            dictThe3rd[val].add(k)
print dictThe3rd

which yields

{'Tickets': set(['Ticket for Mettalica concert', 'Ticket for Placebo concert',
    'Ticket for Iron Maiden concert']),
'Jewelry': set(['Golden ring']),
'Antics': set(['Picture', 'Clock']),
'Moviez': set(['Batman', 'Spider-Man', 'The pianist']),
'Bookz': set(['Thinking in Java', 'Thinking in C++']),
'Gamez': set(['Diablo 3', 'Diablo 2', 'WoW', 'Cabal']),
'Clothes': set(['Asus', 'HP'])}
like image 20
hochl Avatar answered Oct 25 '22 23:10

hochl