私信  •  关注

no comment

no comment 最近创建的主题
no comment 最近回复了
3 年前
回复了 no comment 创建的主题 » 如何使用列表在python中添加新键和值?[闭门]

压缩并分配:

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'}]