比较python中两个字典列表

时间:2019-11-04 07:17:05

标签: python list dictionary

我有两个字典列表,分别名为category和sub_category。

category = [{'cat_id':1,'total':300,'from':250},{'cat_id':2,'total':100,'from':150}]
sub_category = [{'id':1,'cat_id':1,'charge':30},{'id':2,'cat_id':1,'charge':20},{'id':3,'cat_id':2,'charge':30}]

如果charge的{​​{1}}的{​​{1}}的值相等,我想在sub_category中将0的值更改为total >= from。 / p>

预期结果是:

category

我设法通过此方法获得了结果

cat_id

但是我想知道更好的方法。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

这是一种方法。将类别更改为dict即可轻松循环播放。

例如:

category = [{'cat_id':1,'total':300,'from':250},{'cat_id':2,'total':100,'from':150}]
sub_category = [{'id':1,'cat_id':1,'charge':30},{'id':2,'cat_id':1,'charge':20},{'id':3,'cat_id':2,'charge':30}]

category = {i.pop('cat_id'): i for i in category}

for i in sub_category:
    if i['cat_id'] in category:
        if category[i['cat_id']]['total'] >= category[i['cat_id']]['from']:
            i['charge'] = 0
print(sub_category)  

输出:

[{'cat_id': 1, 'charge': 0, 'id': 1},
 {'cat_id': 1, 'charge': 0, 'id': 2},
 {'cat_id': 2, 'charge': 30, 'id': 3}]

答案 1 :(得分:1)

尝试一下:

我认为我的做法在某些情况下可能不适合。我喜欢使用列表理解功能。

category = [{'cat_id':1,'total':300,'from':250},{'cat_id':2,'total':100,'from':150}]
sub_category = [{'id':1,'cat_id':1,'charge':30},{'id':2,'cat_id':1,'charge':20},{'id':3,'cat_id':2,'charge':30}]
print [sub_cat if cat['cat_id'] == sub_cat['id'] and cat['total'] >= cat['from'] and not sub_cat.__setitem__('charge','0') else sub_cat for sub_cat in sub_category for cat in category]

Result:[{'cat_id': 1, 'charge': '0', 'id': 1}, {'cat_id': 1, 'charge': '0', 'id': 1}, {'cat_id': 1, 'charge': 20, 'id': 2}, {'cat_id': 1, 'charge': 20, 'id': 2}, {'cat_id': 2, 'charge': 30, 'id': 3}, {'cat_id': 2, 'charge': 30, 'id': 3}]

答案 2 :(得分:1)

您可以使用以下方法解决您的问题:

target_categories = set([elem.get('cat_id') for elem in category if elem.get('total', 0) >= elem.get('from', 0)])
if None in target_categories:
    target_categories.remove(None) # if there's no cat_id in one of the categories we will get None in target_categories. Remove it.
for elem in sub_category:
    if elem.get('cat_id') in target_categories:
        elem.update({'charge': 0})

与另一种方法的时间比较:

import numpy as np

size = 5000000
np.random.seed()

cat_ids = np.random.randint(50, size=(size,))
totals = np.random.randint(500, size=(size,))
froms = np.random.randint(500, size=(size,))

category = [{'cat_id': cat_id, 'total': total, 'from': from_} for cat_id, total, from_ in zip(cat_ids, totals, froms)]
sub_category = [{'id': 1, 'cat_id': np.random.randint(50), 'charge': np.random.randint(100)} for i in range(size)]

%%time
target_categories = set([elem.get('cat_id') for elem in category if elem.get('total', 0) >= elem.get('from', 0)])
if None in target_categories:
    target_categories.remove(None) # if there's no cat_id in one of the categories we will get None in target_categories. Remove it.
for elem in sub_category:
    if elem.get('cat_id') in target_categories:
        elem.update({'charge': 0})
# Wall time: 3.47 s

%%time
category = {i.pop('cat_id'): i for i in category}
for i in sub_category:
    if i['cat_id'] in category:
        if category[i['cat_id']]['total'] >= category[i['cat_id']]['from']:
            i['charge'] = 0
# Wall time: 5.73 s

答案 3 :(得分:0)

解决方案:

# Input
category = [{'cat_id':1,'total':300,'from':250},{'cat_id':2,'total':100,'from':150}]
sub_category = [{'id':1,'cat_id':1,'charge':30},{'id':2,'cat_id':1,'charge':20},{'id':3,'cat_id':2,'charge':30}]

# Main code
for k in sub_category:
    if k["cat_id"] in [i["cat_id"] for i in category if i["total"] >= i["from"]]:
        k["charge"] = 0
print (sub_category)

# Output
[{'id': 1, 'cat_id': 1, 'charge': 0}, {'id': 2, 'cat_id': 1, 'charge': 0}, {'id': 3, 'cat_id': 2, 'charge': 30}]