做dict理解的更好方法

时间:2016-08-25 08:48:20

标签: python dictionary dictionary-comprehension

UG=[
    {"group_member":"myGroup1","user_name":"tom"},
    {"group_member":"myGroup2","user_name":"wilson"},
    {"group_member":"myGroup1","user_name":"kevin"},
    {"group_member":"myGroup2","user_name":"donna"},
    {"group_member":"myGroup3","user_name":"john"},
    {"group_member":"myGroup1","user_name":"steve"},
    {"group_member":"myGroup2","user_name":"jose"},
    {"group_member":"myGroup3","user_name":"jags"}]
PG=[
    {"group_member":"myGroup1","device_name":"device1"},
    {"group_member":"myGroup1","device_name":"device2"},
    {"group_member":"myGroup2","device_name":"device1"},
    {"group_member":"myGroup1","device_name":"device2"},
    {"group_member":"myGroup1","device_name":"device3"},
    {"group_member":"myGroup3","device_name":"device1"}]
DG=[
    {"device_name":"device1","server":"server1"},
    {"device_name":"device2","server":"server2"},
    {"device_name":"device3","server":"server3"},
    {"device_name":"device4","server":"server4"},
    {"device_name":"device5","server":"server5"},
    {"device_name":"device6","server":"server6"}
    ]

我需要比较列表并准备一个具有以下条件的字典列表

UG[i]['group_member'] == PG[j]['group_member'] && PG[j]['device_name'] == UG[k]['device_name']

这是我的实现

# output array
output=[]
for i in DG:
    for j in PG:
        if i["device_name"] == j["device_name"]:
            for k in UG:
                if k["group_member"] == j["group_member"]:
                   output.append({"user_name":k["user_name"],"group_member":k["group_member"],"device_name":j["device_name"],"server":i["server"]})

for m in output:
  print m

期望的输出============

[
    {'server': 'server1', 'user_name': 'tom', 'group_member': 'myGroup1', 'device_name': 'device1'}, 
    {'server': 'server1', 'user_name': 'kevin', 'group_member': 'myGroup1', 'device_name': 'device1'},
    {'server': 'server1', 'user_name': 'steve', 'group_member': 'myGroup1', 'device_name': 'device1'},
    {'server': 'server1', 'user_name': 'wilson', 'group_member': 'myGroup2', 'device_name': 'device1'}
    {'server': 'server1', 'user_name': 'donna', 'group_member': 'myGroup2', 'device_name': 'device1'},
    {'server': 'server1', 'user_name': 'jose', 'group_member': 'myGroup2', 'device_name': 'device1'}, 
    {'server': 'server1', 'user_name': 'john', 'group_member': 'myGroup3', 'device_name': 'device1'}, 
    {'server': 'server1', 'user_name': 'jags', 'group_member': 'myGroup3', 'device_name': 'device1'}, 
    {'server': 'server2', 'user_name': 'tom', 'group_member': 'myGroup1', 'device_name': 'device2'}, 
    {'server': 'server2', 'user_name': 'kevin', 'group_member': 'myGroup1', 'device_name': 'device2'},
    {'server': 'server2', 'user_name': 'steve', 'group_member': 'myGroup1', 'device_name': 'device2'},
    {'server': 'server2', 'user_name': 'tom', 'group_member': 'myGroup1', 'device_name': 'device2'}, 
    {'server': 'server2', 'user_name': 'kevin', 'group_member': 'myGroup1', 'device_name': 'device2'},
    {'server': 'server2', 'user_name': 'steve', 'group_member': 'myGroup1', 'device_name': 'device2'},
    {'server': 'server3', 'user_name':'tom', 'group_member': 'myGroup1', 'device_name': 'device3'}, 
    {'server': 'server3', 'user_name': 'kevin', 'group_member': 'myGroup1', 'device_name': 'device3'},
    {'server': 'server3', 'user_name': 'steve', 'group_member': 'myGroup1', 'device_name': 'device3'}
]

我如何改进我的实施?

2 个答案:

答案 0 :(得分:2)

是的,可以使用列表理解来完成。但首先让我们创建一个函数dict更新函数,因为dict的更新方法不返回新的dict(而是更新当前的dict):

def updt(d1, d2):
    d3 = d1.copy()
    d3.update(d2)
    return d3

现在让我们了解列表理解:

dictlist = [updt(updt(ug, pg), dg) for ug in UG for pg in PG for dg in DG if ug['group_member'] == pg['group_member'] and pg['device_name'] == dg['device_name']]

结果如下:

for d in dictlist:
    print(d)            

你有:


{'user_name': 'tom', 'server': 'server1', 'group_member': 'myGroup1', 'device_name': 'device1'}
{'user_name': 'tom', 'server': 'server2', 'group_member': 'myGroup1', 'device_name': 'device2'}
{'user_name': 'tom', 'server': 'server2', 'group_member': 'myGroup1', 'device_name': 'device2'}
{'user_name': 'tom', 'server': 'server3', 'group_member': 'myGroup1', 'device_name': 'device3'}
{'user_name': 'wilson', 'server': 'server1', 'group_member': 'myGroup2', 'device_name': 'device1'}
{'user_name': 'kevin', 'server': 'server1', 'group_member': 'myGroup1', 'device_name': 'device1'}
{'user_name': 'kevin', 'server': 'server2', 'group_member': 'myGroup1', 'device_name': 'device2'}
{'user_name': 'kevin', 'server': 'server2', 'group_member': 'myGroup1', 'device_name': 'device2'}
{'user_name': 'kevin', 'server': 'server3', 'group_member': 'myGroup1', 'device_name': 'device3'}
{'user_name': 'donna', 'server': 'server1', 'group_member': 'myGroup2', 'device_name': 'device1'}
{'user_name': 'john', 'server': 'server1', 'group_member': 'myGroup3', 'device_name': 'device1'}
{'user_name': 'steve', 'server': 'server1', 'group_member': 'myGroup1', 'device_name': 'device1'}
{'user_name': 'steve', 'server': 'server2', 'group_member': 'myGroup1', 'device_name': 'device2'}
{'user_name': 'steve', 'server': 'server2', 'group_member': 'myGroup1', 'device_name': 'device2'}
{'user_name': 'steve', 'server': 'server3', 'group_member': 'myGroup1', 'device_name': 'device3'}
{'user_name': 'jose', 'server': 'server1', 'group_member': 'myGroup2', 'device_name': 'device1'}
{'user_name': 'jags', 'server': 'server1', 'group_member': 'myGroup3', 'device_name': 'device1'}

这似乎是你想要的,虽然顺序不同。

答案 1 :(得分:1)

如果你有数千条记录,那么就是发电机

from itertools import product

def produce():
    for dg, pg, ug in product(DG, PG, UG):
        if pg['device_name'] == dg['device_name'] and ug['group_member'] == pg['group_member']:
            item = dg.copy()
            item.update(pg)
            item.update(ug)
            yield item