python键值两个dict匹配

时间:2013-05-01 17:17:21

标签: python python-3.x

我正在通过循环来匹配两个sperate键的两个dicts的值 - 希望if i in line_aum['id_class'] == line_investor['id_class']成为True,然后下一个总和将起作用:

它会产生不同的结果

到目前为止,我有:

for line_aum in aum_obj:
    for line_investor in investor_obj:
        if i in line_aum['id_class'] == line_investor['id_class']:
            total = (sum,line_investor['amount'], line_aum['value'])
            amount = line['id_class']
            print(amount,total)

示例数据:

{'fund_name': '', 'fund_code': 'LFE', 'aumc': '406.37', 'value': '500', 'ddate': '2013-01-01', 'id_fund': '165', 'currency': 'EUR', 'nav': '24.02', 'shares': '16.918', 'estimate': '0', 'id_class': '4526', 'class_name': 'LTD - CLASS B (EUR)'}

1 个答案:

答案 0 :(得分:2)

如果itertools.productaum_obj都是列表,请使用investor_obj代替嵌套循环:

from itertools import product

for line_aum, line_investor in product(aum_obj, investor_obj):
    if line_aum['id_class'] == line_investor['id_class']:
        # `line_aum` and `line_investor` have matching values for the `id_class` keys.