简体   繁体   English

如何将新的键值对添加到字典列表中?

[英]How do I add a new key value pair to a list of dictionaries?

Hi new python coder here, let's say I have a list of dictionaries:嗨,这里有新的 python 编码器,假设我有一个字典列表:

data = [{'stat_1': 'name_1', 'stat_2': 10}, {'stat_1': 'name_2', 'stat_2': 12}]

Now I want to add to this list of dictionaries a stat_3 that has the corresponding values for each dictionary item of 100 and 120.现在我想在这个字典列表中添加一个 stat_3,它具有每个字典项 100 和 120 的对应值。

My intended result is:我的预期结果是:

updated_data = [{'stat_1': 'name_1', 'stat_2': 10, 'stat_3': 100}, {'stat_1': 'name_2', 'stat_2': 12, 'stat_3: 120}]

I know you can append to one dictionary item but how would I go about this with two dictionary items in a list?我知道你可以 append 到一个字典项目,但我将如何 go 与列表中的两个字典项目有关? Is a for loop required?是否需要 for 循环? Appreciate the help!感谢帮助!

updated_data = data

updated_data[0].update({'stat_3': 100})

updated_data[1].update({'stat_3': 120})

Will sort you, matey.会给你排序的,伙计。

This should work fine:这应该可以正常工作:

data[0]['stat_3'] = 100
data[1]['stat_3'] = 120

if you have a lot of dictionaries in your data list i would do this:如果您的data列表中有很多字典,我会这样做:

for i in range(len(data)):
   data[i]['stat_3'] = value #(whatever value you want)   

Here's a one-liner:这是一个单行:

data = [{'1': 1, '2': 2}, {'11': 11, '22': 22}]
append = {'C': 'c', 'D': 'd', 'E': 'e'}

[d.update(append) for d in data]

print(data)

As a side-effect, this creates a list full of None as long as len(data) .作为副作用,只要len(data) ,这将创建一个完整的list None However, no reference to it is saved, so it should be garbage collected immediately.但是,没有保存对它的引用,因此应该立即对它进行垃圾回收。

Output: Output:

[{'1': 1, '2': 2, 'C': 'c', 'D': 'd', 'E': 'e'},
{'11': 11, '22': 22, 'C': 'c', 'D': 'd', 'E': 'e'}]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将新的键值对添加到词典列表中? - How to add new key value pair to a list of dictionaries? 如何根据匹配的键:值对组合字典列表中的 N 个字典? - how do I combine N dictionaries in list of dictionaries based on matching key:value pair? 在字典列表中添加键/值对(使用计算) - Add a key/value pair(With caculation) in a list of dictionaries 如何根据字典列字段列表中的键值对过滤 DataFrame 行? - How do I filter DataFrame rows based on a key value pair from a list of dictionaries column field? 如何从包含键作为字典列表的字典中获取第一个键值对(即“类型”:45) - How do i fetch first key value pair (i.e., 'type': 45) from a dictionary which contains key as list with dictionaries 如果键已包含在字典中,如何以智能方式将新的键值对添加到字典中? - How do I add a new key-value pair in a smart way to a dict if the key is already contained in the dict? 根据键值对匹配,添加两个字典列表的元素 - Add elements of two list of dictionaries based on a key value pair match 如何将新的键值对添加到字典列表中的现有键值对中? - How to add a new key value pair to existing key value pair from list of dicts? 字典列表 Python:如果现有的键、值对匹配,则将新的键、值对附加到字典中 - List of Dictionaries Python: Append a new key, value pair to dictionary if existing key, value pair matches 如果字典的键相同,如何添加字典列表的值(列表)? - How do I add values (list) of a list of dictionaries if their key is same?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM