0

I'd like to create a dictionary inside a dictionary in python using function setdefault(). I'm trying to make a list of names and dates of birth using fallow dictionary.

names = {'Will': 'january', 'Mary': 'february', 'George': 'march', 'Steven': 'april', 'Peter': 'may'}
dates = {'Will': '7/01', 'George': '21/03', 'Steven': '14/03', 'Mary': '2/02'}

I was tring to use set to achieve this:

res_dict = dict()
for v, k in names.items():
    for v1, k1 in dates.items():
        res_dict.setdefault(v, {}).append(k)
        res_dict.setdefault(v1, {}).append(k1)
return res_dict

but it give me an error.

The result should be:

res_dict = {'Will': {'january': '7/01'}, 'Mary' : {'february': '2/02'} ,'George': {'march': '21/03'}, 'Steven': {'april': '14/03'}, 'Peter': {'may': ''}}

How can I get the desired result using setdefault()?

4

3 回答 3

2

You could try this:

In [17]: results = {}

In [18]: for k, v in names.iteritems():
    results[k] = {v: dates.setdefault(k, '')}
   ....:
   ....:

In [20]: results
Out[20]: 
{'George': {'march': '21/02'},
 'Mary': {'february': '2/02'},
 'Peter': {'may': ''},
 'Steven': {'april': '14/03'},
 'Will': {'january': '7/01'}}

And as to your comment regarding adding month and day, you can add them similarly:

In [28]: for k, v in names.iteritems():
    results[k] = {'month': v, 'day': dates.setdefault(k, '')}
   ....:
   ....:

In [30]: results
Out[30]:
{'George': {'day': '21/02', 'month': 'march'},
 'Mary': {'day': '2/02', 'month': 'february'},
 'Peter': {'day': '', 'month': 'may'},
 'Steven': {'day': '14/03', 'month': 'april'},
 'Will': {'day': '7/01', 'month': 'january'}}

And if you want to omit day completely in the case where a value doesn't exist:

In [8]: results = {}

In [9]: for k, v in names.iteritems():
   ...:     results[k] = {'month': v}
   ...:     if dates.has_key(k):
   ...:         results[k]['day'] = dates[k]
   ...:
   ...:

In [10]: results
Out[10]:
{'George': {'day': '21/03', 'month': 'march'},
 'Mary': {'day': '2/02', 'month': 'february'},
 'Peter': {'month': 'may'},
 'Steven': {'day': '14/03', 'month': 'april'},
 'Will': {'day': '7/01', 'month': 'january'}}

And in the odd case where you know the date but not the month, iterating through the set of the keys (as @KayZhu suggested) with a defaultdict may be the easiest solution:

In [1]: from collections import defaultdict

In [2]: names = {'Will': 'january', 'Mary': 'february', 'George': 'march', 'Steven': 'april', 'Peter': 'may'}

In [3]: dates = {'Will': '7/01', 'George': '21/03', 'Steven': '14/03', 'Mary': '2/02', 'Marat': '27/03'}

In [4]: results = defaultdict(dict)

In [5]: for name in set(names.keys() + dates.keys()):
   ...:     if name in names:
   ...:         results[name]['month'] = names[name]
   ...:     if name in dates:
   ...:         results[name]['day'] = dates[name]
   ...:
   ...:

In [6]: for k, v in results.iteritems():
   ...:     print k, v
   ...:
   ...:
George {'day': '21/03', 'month': 'march'}
Will {'day': '7/01', 'month': 'january'}
Marat {'day': '27/03'}
Steven {'day': '14/03', 'month': 'april'}
Peter {'month': 'may'}
Mary {'day': '2/02', 'month': 'february'}
于 2012-10-27T07:33:18.467 回答
1

A simple one-liner:

In [38]: names = {'Will': 'january', 'Mary': 'february', 'George': 'march', 'Steven': 'april', 'Peter': 'may'}

In [39]: dates = {'Will': '7/01', 'George': '21/03', 'Steven': '14/03', 'Mary': '2/02'}

In [40]: dict((name,{names[name]:dates.get(name,'')}) for name in names)

out[40]: 
    {'George': {'march': '21/03'},
     'Mary': {'february': '2/02'},
     'Peter': {'may': ''},
     'Steven': {'april': '14/03'},
     'Will': {'january': '7/01'}}
于 2012-10-27T08:31:51.857 回答
0

You will need get the superset keys from names and dates first:

>>> for k in set(names.keys() + dates.keys()):
...     res_dict[k] = {names.setdefault(k, ''): dates.setdefault(k, None)}
...     
... 
>>> res_dict
{'Will': {'january': '7/01'}, 'Steven': {'april': '14/03'}, 'Peter': {'may': None},
 'Mary': {'february': '2/02'}, 'George': {'march': '21/03'}}

Otherwise, you will miss out results whose keys are in dates but not in names.

于 2012-10-27T08:06:03.530 回答