我有一本字典和一个特定的键,我说5个可能的新值。所以我试图通过使用一个简单的lambda函数来创建原始字典的5个副本,该函数将替换该特定键的值并返回主字典的副本。返回字典修改
# This is the master dictionary.
d = {'fn' : 'Joseph', 'ln' : 'Randall', 'phone' : '100' }
# Joseph has got 4 other phone numbers
lst = ['200', '300', '400', '500']
# I want 4 copies of the dictionary d with these different phone numbers
# Later I would want to do some processing with those dictionary without affecting d
所以我想这样做:
# y is the list I want to hold these copies of dictionaries with modified values
i = d.copy()
y = map(lambda x : (i.update({'phone' : x})) and i, lst)
我认为这将返回字典列表和他们每个人都会有电话号码分别变为200,300,400和500。我可以放置一个循环并创建副本,并使用一种天真的方法改变它们,但我想探索并知道如何利用lambda来实现这一点。
在此先感谢。
你还试过了什么? – Marcin 2012-03-06 18:14:09
@Marcin:“我想探索并知道如何利用它这是一个很棒的网站! – Benjamin 2012-03-06 18:16:48