python 按从小到大的顺序组合成一个字典_python-根据匹配值组合2个字典列表,否则保持......

是否可以遍历字典的水果列表,然后查找是否有匹配的树,然后从水果中添加高度后输出字典?如果没有匹配项,我也想以原始形式包括字典.因此,在这种情况下,输出将是字典的组合列表.

所需的输出:

combined = [

{'Type':'Tree', 'Name':'Pear', 'Color':'Red,Green', 'Height':'25ft'},

{'Type':'Bush', 'Name':'Raspberry', 'Color':'Red,White'},

{'Type':'Tree', 'Name':'Apple', 'Color':'Red,Green', 'Height':'15ft'}

]

起始表:

fruit = [

{'Type':'Tree', 'Name':'Pear', 'Color':'Red,Green'},

{'Type':'Bush', 'Name':'Raspberry', 'Color':'Red,White'},

{'Type':'Tree', 'Name':'Apple', 'Color':'Red,Green'}

]

type = [

{'Type':'Tree', 'Fruit':'Pear', 'Height':'25ft'},

{'Type':'Tree', 'Fruit':'Apple', 'Height':'15ft'},

{'Type':'Root', 'Fruit':'Carrot', 'Height':'2ft'}

]

当我尝试执行此操作时,使用以下命令时,我将不断得到重复的输出.我认为是由于嵌套循环的缘故,但我不确定.

combined = []

for i in fruit:

for x in type:

if i['Name'] == x['Name']:

out = i

out['Height'] = x['Height']

combined.append(out)

else:

combined.append(i)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值