【发布时间】:2019-12-12 20:03:43
【问题描述】:
我正在尝试加入两个字典列表,其中一个字段包含另一个字段的一部分,如果没有匹配项则将其删除。
list_1 = [{
"bytes_in": 0,
"rsrq": -50,
"hostname": "AB9472"
},
{
"bytes_in": 0,
"rsrq": -90,
"hostname": "DF4845"
},
{
"bytes_in": 0,
"rsrq": "None",
"hostname": "FC4848"
}
]
list_2 = [{
"id": 249,
"ref_no": "AB9472 - 015632584",
"rssi": "-75.0",
"circuit_type": "4G"
},
{
"id": 17,
"ref_no": "DF4845 - 8984494",
"rssi": "-20.0",
"circuit_type": "4G"
}]
所以在上面的示例中,list_1 主机名字段将包含在 list_2 ref_no 中,并且将匹配前两条记录,然后在新的 list_3 中创建
我觉得我可以在 iterrtools 中做到这一点,但我不确定如何? 谢谢
想要的输出:
list_3 = [{
"id": 249,
"ref_no": "AB9472 - 015632584",
"rssi": "-75.0",
"circuit_type": "4G",
"bytes_in": 0,
"rsrq": -50,
"hostname": "AB9472"
},
{
"id": 17,
"ref_no": "DF4845 - 8984494",
"rssi": "-20.0",
"circuit_type": "4G",
"bytes_in": 0,
"rsrq": -90,
"hostname": "DF4845"
}]
【问题讨论】:
-
您能添加一个预期的输出吗?
-
嗨,我已经添加了,谢谢
-
而且一定要用itertools吗?
-
您知道相关字段的名称吗?即如果
id意外出现在hostname中怎么办?
标签: python