从两个词典创建一个新列表

时间:2016-08-31 09:19:19

标签: python dictionary

这是关于Python的问题。我有以下词典列表:

listA = [
          {"t": 1, "tid": 2, "gtm": 3, "c1": 4, "id": "111"},
          {"t": 3, "tid": 4, "gtm": 3, "c1": 4, "c2": 5, "id": "222"},
          {"t": 1, "tid": 2, "gtm": 3, "c1": 4, "c2": 5, "id": "333"},
          {"t": 5, "tid": 6, "gtm": 3, "c1": 4, "c2": 5, "id": "444"}
        ]

和我想要比较的词典:

dictA = {"t": 1, "tid": 2, "gtm": 3}

我想创建一个与 listA dictA 中的所有项目匹配的词典列表,并包含" id"领域:

listB = [
          {"t": 1, "tid": 2, "gtm": 3, "id": "111"},
          {"t": 1, "tid": 2, "gtm": 3, "id": "333"}
        ]

我试过这样做:

for k in listA:
    for key, value in k.viewitems() & dictA.viewitems():
        print key, value

但它匹配 dictA 中的任何项目。

4 个答案:

答案 0 :(得分:3)

您需要检查交叉点的长度,只需检查if dct.viewitems() & dictA.viewitems()将为任何交叉点评估为True:

[dct for dct in listA if len(dct.viewitems() & dictA.viewitems()) == len(dictA)]

或者只检查一个子集,如果来自dictA的项目是每个dict的subset

[dct for dct in listA if dictA.viewitems() <= dct.viewitems()]

或者反转寻找superset的逻辑:

 [dct for dct in listA if dct.viewitems() >= dictA.viewitems()]

答案 1 :(得分:2)

您可以使用dictionary view

listA = [
          {"t": 1, "tid": 2, "gtm": 3, "c1": 4, "id": "111"},
          {"t": 3, "tid": 4, "gtm": 3, "c1": 4, "c2": 5, "id": "222"},
          {"t": 1, "tid": 2, "gtm": 3, "c1": 4, "c2": 5, "id": "333"},
          {"t": 5, "tid": 6, "gtm": 3, "c1": 4, "c2": 5, "id": "444"}
        ]

dictA = {"t": 1, "tid": 2, "gtm": 3}

for k in listA:
    if dictA.viewitems() <= k.viewitems():
        print k

对于python 3使用:

if dictA.items() <= k.items():
    print(k)

答案 2 :(得分:1)

对于python 2.7:

listA = [
              {"t": 1, "tid": 2, "gtm": 3, "c1": 4, "id": "111"},
              {"t": 3, "tid": 4, "gtm": 3, "c1": 4, "c2": 5, "id": "222"},
              {"t": 1, "tid": 2, "gtm": 3, "c1": 4, "c2": 5, "id": "333"},
              {"t": 5, "tid": 6, "gtm": 3, "c1": 4, "c2": 5, "id": "444"}
            ]
dictA = {"t": 1, "tid": 2, "gtm": 3}
for k in listA:
    if all(x in k.viewitems() for x in dictA.viewitems()):
        print k

它输出为:

{'tid': 2, 'c1': 4, 'id': '111', 't': 1, 'gtm': 3}
{'gtm': 3, 't': 1, 'tid': 2, 'c2': 5, 'c1': 4, 'id': '333'}

如果您想创建列表而不是print,请将字典添加到列表中,如下所示:

listA = [
          {"t": 1, "tid": 2, "gtm": 3, "c1": 4, "id": "111"},
          {"t": 3, "tid": 4, "gtm": 3, "c1": 4, "c2": 5, "id": "222"},
          {"t": 1, "tid": 2, "gtm": 3, "c1": 4, "c2": 5, "id": "333"},
          {"t": 5, "tid": 6, "gtm": 3, "c1": 4, "c2": 5, "id": "444"}
        ]
dictA = {"t": 1, "tid": 2, "gtm": 3}
ans =[]
for k in listA:
    if all(x in k.viewitems() for x in dictA.viewitems()):
        ans.append(k)
        #print k
print ans

它提供输出:

[{'tid': 2, 'c1': 4, 'id': '111', 't': 1, 'gtm': 3}, {'gtm': 3, 't': 1, 'tid': 2, 'c2': 5, 'c1': 4, 'id': '333'}]

答案 3 :(得分:1)

试试这个,all会检查dictA中是否存在listA

[i for i in listA if all(j in i.items() for j in dictA.items())]

结果

[{'c1': 4, 'gtm': 3, 'id': '111', 't': 1, 'tid': 2},
 {'c1': 4, 'c2': 5, 'gtm': 3, 'id': '333', 't': 1, 'tid': 2}]