我的数据如下:
buffer = [{"siteid": 1 , "distance": 2, "codes": "1|b|c", "urv": "545"}, {"siteid": 2 , "distance": 2, "codes": "1|b|c", "urv": "55"}, {"siteid": 2 , "distance": 2, "codes": "1|b|c", "urv": "55"}, {"siteid": 3 , "distance": 2, "codes": "1|b|c", "urv": "546"}]
layer = [{"siteid": 2 }, {"siteid": 4 }, {"siteid": 3 }]
我希望能够返回siteid相同的缓冲区列表的所有成员。
结果将是:
[{"siteid": 2 , "distance": 2, "codes": "1|b|c", "urv": "55"}, {"siteid": 2 , "distance": 2, "codes": "1|b|c", "urv": "55"}, {"siteid": 3 , "distance": 2, "codes": "1|b|c", "urv": "546"}]
TIA, 克里斯
答案 0 :(得分:1)
我不知道有任何内置可以做到这一点,但你可以尝试编写自己的函数来找到你的两个dicts的交叉点:
def find_intersection(buffer, layer):
siteids = [a['siteid'] for a in layer]
return [item for item in buffer if item['siteid'] in siteids]
答案 1 :(得分:1)
这样的东西?
buffer = [{"siteid": 1 , "distance": 2, "codes": "1|b|c", "urv": "545"}, {"siteid": 2 , "distance": 2, "codes": "1|b|c", "urv": "55"}, {"siteid": 2 , "distance": 2, "codes": "1|b|c", "urv": "55"}, {"siteid": 3 , "distance": 2, "codes": "1|b|c", "urv": "546"}]
layer = [{"siteid": 2 }, {"siteid": 4 }, {"siteid": 3 }]
ids = [l['siteid'] for l in layer]
print [b for b in buffer if b['siteid'] in ids]
答案 2 :(得分:1)
intersection = set(x['sideid'] for x in buffer) & set(x['siteid'] for x in layer)
return [x for x in buffer if x['sideid'] in intersection]