我有一个字典,对于一个特定的键,我有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函数来实现这一目标。
提前感谢。