I want to filter a list of dictionarys by key "IP" and value of that key. This is my list for example.
**
a=[{'IP': '192.168.0.5', 'Temp1': ['58.0']},
{'IP': '192.168.0.6', 'Temp1': ['59.1']},
{'IP': '192.168.0.5', 'Temp2': ['59.1']},
{'IP': '192.168.0.6', 'Temp2': ['60.7']}]
**
Is it possible to make it look like this?
I want to filter by "IP" so it looks like this:
**
filtered=[{'IP': '192.168.0.5', 'Temp1': ['58.0'],'Temp2': ['59.1']},
{'IP': '192.168.0.6', 'Temp1': ['59.1'],'Temp2': ['60.7']}]
**
But I dont know how to exactly do it, can somebody please explain it to me?
CodePudding user response:
This should do it:
a=[{'IP': '192.168.0.5', 'Temp1': ['58.0']},
{'IP': '192.168.0.6', 'Temp1': ['59.1']},
{'IP': '192.168.0.5', 'Temp2': ['59.1']},
{'IP': '192.168.0.6', 'Temp2': ['60.7']}]
filtered=[{'IP': '192.168.0.5', 'Temp1': ['58.0'],'Temp2': ['59.1']},
{'IP': '192.168.0.6', 'Temp1': ['59.1'],'Temp2': ['60.7']}]
import collections
b = collections.defaultdict(dict)
for d in a:
for k, v in d.items():
b[d['IP']][k] = v
c = list(b.values())
print(c)
Output is:
[{'IP': '192.168.0.5', 'Temp1': ['58.0'], 'Temp2': ['59.1']}, {'IP': '192.168.0.6', 'Temp1': ['59.1'], 'Temp2': ['60.7']}]
CodePudding user response:
Here's a method where we build the dict of IP: dict
via a comprehension before doing the update
loop:
>>> a=[{'IP': '192.168.0.5', 'Temp1': ['58.0']},
... {'IP': '192.168.0.6', 'Temp1': ['59.1']},
... {'IP': '192.168.0.5', 'Temp2': ['59.1']},
... {'IP': '192.168.0.6', 'Temp2': ['60.7']}]
>>> by_ip = {ip: {'IP': ip} for ip in {d['IP'] for d in a}}
>>> for d in a:
... by_ip[d['IP']].update(d)
...
>>> filtered = list(by_ip.values())
>>> filtered
[{'IP': '192.168.0.5', 'Temp1': ['58.0'], 'Temp2': ['59.1']}, {'IP': '192.168.0.6', 'Temp1': ['59.1'], 'Temp2': ['60.7']}]
And here's a method where we can do it in one crazy nested comprehension via functools.reduce
:
>>> from functools import reduce
>>> filtered = [reduce(dict.__or__, (d for d in a if d['IP'] == ip), {'IP': ip}) for ip in {d['IP'] for d in a}]
>>> filtered
[{'IP': '192.168.0.5', 'Temp1': ['58.0'], 'Temp2': ['59.1']}, {'IP': '192.168.0.6', 'Temp1': ['59.1'], 'Temp2': ['60.7']}]