[英]How to insert to a list of dictionaries values in a specific key
I have a list of dictionaries which is like this我有一个像这样的字典列表
mylist = [ {'Var1': 'one', 'Var2': 'two', 'Var3': '0'}, {'Var1': 'one', 'Var2': 'two', 'Var3': '0'} ]
What if i want to get the index for mylist[0]['Var1']??如果我想获取 mylist[0]['Var1'] 的索引怎么办? I need to get the index of it not change it manually.
我需要获取它的索引而不是手动更改它。 Im iterrating through my list with a for loop and i need to know in what index it belongs
我用 for 循环遍历我的列表,我需要知道它属于哪个索引
1- You should get your first dictionary from the list by mylist[0]
. 1-您应该从
mylist[0]
的列表中获取您的第一本词典。
2- and then nest it with accessing your desired key ['Var3']
. 2-然后通过访问您想要的键
['Var3']
嵌套它。
So the command will be: mylist[0]['Var3'] = value
所以命令将是:
mylist[0]['Var3'] = value
If you just want to work with the first dictionary in 'mylist', you can access it at:如果您只想使用“mylist”中的第一个字典,您可以在以下位置访问它:
mylist[0]
So adjusting its 'Var3's value would be所以调整它的'Var3'值将是
mylist[0]['Var3'] = 'something_new'
The value of the dictionary fields can be directly overwritten.可以直接覆盖字典字段的值。
Example: If you want to change 'Var3' key's value at the first dictionary, it would be like mylist[0]['Var3'] = 'something'
示例:如果您想在第一个字典中更改 'Var3' 键的值,则类似于
mylist[0]['Var3'] = 'something'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.