[英]how to add many values for a single key in dictionary - python
i have a dictionary : table name :我有一本字典:表名:
{
column 1:
{
value 1 : [data,data,data]
}
column 2:
{
value 1 : [data,data,data]
}
}
now i want to add value 2 inside each column and those values are stored inside a list现在我想在每列中添加值 2,这些值存储在列表中
list1 = [{value2 : [data,data]}, {value2 : [data,data]}
table name :表名:
{
column 1:
{
value 1 : [data,data,data]
value 2 : [data,data]
}
column 2:
{
value 1 : [data,data,data]
value 2 : [data,data]
}
}
Its not clear , I hope you are asking adding key and list of value into dictionary inside a dictionary不清楚,我希望您要求在字典中将键和值列表添加到字典中
You can do that like this:你可以这样做:
tablename[column 1][newKey] = [list of values]
example例子
tablename= {
'col1' : {
'val1':[1,2,44,5],
},
'col2' : {
'val1' : [2,3,47,70]
}
}
Output will be输出将是
{'col1': {'val1': [1, 2, 44, 5]}, 'col2': {'val1': [2, 3, 47, 70]}}
Adding new key:添加新密钥:
tablename['col1']['val2'] = [1,2]
tablename['col2']['val2'] = [3,4]
This output will be此输出将是
{'col1': {'val1': [1, 2, 44, 5], 'val2': [1, 2]}, 'col2': {'val1': [2, 3, 47, 70], 'val2': [3, 4]}}
I hope this will helps.我希望这会有所帮助。
You can try this as well你也可以试试这个
tablename= {
'col1' : {
'val1':[1,2,44,5],
},
'col2' : {
'val1' : [2,3,47,70]
}
}
then add new key like this然后像这样添加新密钥
val2 = [1,2,3,4]
tablename['col1']['val2'] = val2
tablename['col2']['val2'] = val2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.