我有一本字典和一個特定的鍵,我說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