python列表添加列表_python – 从列表中的值向列表中的dicts添加键

如果dict包含某个键,如何从列表中的值中添加键到列表中的dict?

我有一个dicts列表.这些dicts只包含一个键(‘review’)或两个键(‘review’和’response’).当dict包含键’response’时,我想添加两个键,其中包含两个列表中的值.

data = [{'response': 'This is a response',

'review': 'This is a review'},

{'review': 'This is only a review'},

{'response': 'This is also a response',

'review': 'This is also a review'}]

date = ['4 days ago',

'3 days ago']

responder = ['Manager',

'Customer service']

我尝试了以下内容,但由于每个包含密钥“响应”的dict我只想从每个列表的值中添加1,我不知道如何做到这一点.

for d in data:

if 'response' in d:

for i in date:

d['date'] = i

for i in responder:

d['responder'] = i

输出显示我当然只添加列表的最后一个值,因为我循环遍历列表.我怎样才能解决这个问题?

[{'date': '3 days ago',

'responder': 'Customer service',

'response': 'This is a response',

'review': 'This is a review'},

{'review': 'This is only a review'},

{'date': '3 days ago',

'responder': 'Customer service',

'response': 'This is also a response',

'review': 'This is also a review'}]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值