[英]Updating values from specific key in list of dictionaries based on a separate list
I have a list of dictionaries with identical keys. 我有一个具有相同键的词典列表。 Some keys have integer values, a few have string values, and one key has a set value for the first dictionary and None for all the remaining dictionaries.
有些键有整数值,少数有字符串值,以及一个键具有第一字典和无 ,所有剩余的字典设定值。
Example of the first dictionary in the list of dictionaries is found here: 字典列表中第一个词典的示例在这里找到:
dicts = [{'foul': 1, 'lineup': set(['Player 1', 'Player 2']), 'Date': '11/12/2016}...
Example of the second and third dictionary in the list of dictionaries: 词典列表中第二和第三本词典的示例:
...{'foul': 6, 'lineup': None, 'Date': '11/19/2016}, {'foul': 12, 'lineup': None, 'Date': '11/22/2016}]
Here is an example list of values I want to use to update these three dictionaries: 这是我要用来更新这三个字典的值的示例列表:
listofvals = [set('Player 4', 'Player 2'), set('Player 2', 'Player 3'), set('Player 6', 'Player 1')]
Note that regardless of what the value is in the first dictionary, I want to update all dictionaries in the list of dicts in the order of the list above. 请注意,无论第一个字典中的值是多少,我都想按照上面列表的顺序更新字典列表中的所有字典。
Such that the list of dictionaries returned is as follows: 这样返回的词典列表如下:
desired_product =
[{'foul': 1, 'lineup': set(['Player 4', 'Player 2']), 'Day': '11/12/2016},
{'foul': 6, 'lineup': set(['Player 2', 'Player 3']), 'Day': '11/19/2016},
{'foul': 12, 'lineup': set(['Player 6', 'Player 1']), 'Day': '11/22/2016}]
I have tried variations on the following: 我尝试了以下变化:
for index, d in enumerate(dicts):
stnt.update((k, listofvals[idx]) for k, v in stnt.iteritems() if v == None or type(v) == 'set')
Another thing I've tried: 我尝试过的另一件事:
for index, d in enumerate(dicts):
for key, val in d.interitems():
setattr(d["lineup"], key, listofvals[idx])
for index,d in enumerate(dicts):
d["lineup"] = listofvals[index]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.