如何使用列表在 python 中添加新键和每个键的值?
How to add new keys and value of each of them in python with list?
我有字典和清单。
我想向字典中添加一个新键,以便每个键的值都等于数组元素之一。我在下面的示例中对此进行了更多解释
我的字典
[{'url': 'https://test.com/find/city-1', 'tit': 'title1', 'val': 1},
{'url': 'https://test.com/find/city-2', 'tit': 'title1', 'val': 2},
{'url': 'https://test.com/find/city-3', 'tit': 'title1', 'val': 3}
]
我的清单
['a','b','c']
我想要的:
[{'url': 'https://test.com/find/city-1', 'tit': 'title1', 'val': 1 , 'content' ='a'},
{'url': 'https://test.com/find/city-2', 'tit': 'title1', 'val': 2, 'content' ='b'},
{'url': 'https://test.com/find/city-3', 'tit': 'title1', 'val': 3, 'content' ='c'}
]
您可以使用 zip 将字典与内容字符串配对并为每个字典设置 'content' 键:
dicList = [{'url': 'https://test.com/find/city-1', 'tit': 'title1', 'val': 1},
{'url': 'https://test.com/find/city-2', 'tit': 'title1', 'val': 2},
{'url': 'https://test.com/find/city-3', 'tit': 'title1', 'val': 3}
]
contents = ['a','b','c']
for d,c in zip(dicList,contents):
d['content'] = c
print(dicList)
[{'url': 'https://test.com/find/city-1', 'tit': 'title1', 'val': 1, 'content': 'a'},
{'url': 'https://test.com/find/city-2', 'tit': 'title1', 'val': 2, 'content': 'b'},
{'url': 'https://test.com/find/city-3', 'tit': 'title1', 'val': 3, 'content': 'c'}]
或者,如果您想要新列表中的结果,您可以使用列表理解来构建扩充词典:
dicList2 = [{**d, 'content':c} for d,c in zip(dicList,contents)]
压缩并分配:
for d, d['content'] in zip(dicts, contents):
pass
演示 (Try it online!):
import pprint
dicts = [{'url': 'https://test.com/find/city-1', 'tit': 'title1', 'val': 1},
{'url': 'https://test.com/find/city-2', 'tit': 'title1', 'val': 2},
{'url': 'https://test.com/find/city-3', 'tit': 'title1', 'val': 3}
]
contents = ['a','b','c']
for d, d['content'] in zip(dicts, contents):
pass
pprint.pp(dicts)
输出:
[{'url': 'https://test.com/find/city-1',
'tit': 'title1',
'val': 1,
'content': 'a'},
{'url': 'https://test.com/find/city-2',
'tit': 'title1',
'val': 2,
'content': 'b'},
{'url': 'https://test.com/find/city-3',
'tit': 'title1',
'val': 3,
'content': 'c'}]
我有字典和清单。 我想向字典中添加一个新键,以便每个键的值都等于数组元素之一。我在下面的示例中对此进行了更多解释
我的字典
[{'url': 'https://test.com/find/city-1', 'tit': 'title1', 'val': 1},
{'url': 'https://test.com/find/city-2', 'tit': 'title1', 'val': 2},
{'url': 'https://test.com/find/city-3', 'tit': 'title1', 'val': 3}
]
我的清单
['a','b','c']
我想要的:
[{'url': 'https://test.com/find/city-1', 'tit': 'title1', 'val': 1 , 'content' ='a'},
{'url': 'https://test.com/find/city-2', 'tit': 'title1', 'val': 2, 'content' ='b'},
{'url': 'https://test.com/find/city-3', 'tit': 'title1', 'val': 3, 'content' ='c'}
]
您可以使用 zip 将字典与内容字符串配对并为每个字典设置 'content' 键:
dicList = [{'url': 'https://test.com/find/city-1', 'tit': 'title1', 'val': 1},
{'url': 'https://test.com/find/city-2', 'tit': 'title1', 'val': 2},
{'url': 'https://test.com/find/city-3', 'tit': 'title1', 'val': 3}
]
contents = ['a','b','c']
for d,c in zip(dicList,contents):
d['content'] = c
print(dicList)
[{'url': 'https://test.com/find/city-1', 'tit': 'title1', 'val': 1, 'content': 'a'},
{'url': 'https://test.com/find/city-2', 'tit': 'title1', 'val': 2, 'content': 'b'},
{'url': 'https://test.com/find/city-3', 'tit': 'title1', 'val': 3, 'content': 'c'}]
或者,如果您想要新列表中的结果,您可以使用列表理解来构建扩充词典:
dicList2 = [{**d, 'content':c} for d,c in zip(dicList,contents)]
压缩并分配:
for d, d['content'] in zip(dicts, contents):
pass
演示 (Try it online!):
import pprint
dicts = [{'url': 'https://test.com/find/city-1', 'tit': 'title1', 'val': 1},
{'url': 'https://test.com/find/city-2', 'tit': 'title1', 'val': 2},
{'url': 'https://test.com/find/city-3', 'tit': 'title1', 'val': 3}
]
contents = ['a','b','c']
for d, d['content'] in zip(dicts, contents):
pass
pprint.pp(dicts)
输出:
[{'url': 'https://test.com/find/city-1',
'tit': 'title1',
'val': 1,
'content': 'a'},
{'url': 'https://test.com/find/city-2',
'tit': 'title1',
'val': 2,
'content': 'b'},
{'url': 'https://test.com/find/city-3',
'tit': 'title1',
'val': 3,
'content': 'c'}]