简体   繁体   English

Python字典列出唯一项

[英]Python dictionary list unique items

I have below input我有以下输入

 [{"ip": "1.2.3.4", "bytes": 10}, 
  {"ip": "2.3.4.10", "bytes": 10}, 
  {"ip": "5.6.2.3", "bytes": 10},
  {"ip": "1.2.3.4", "bytes": 20}, 
  {"ip": "1.2.3.4", "bytes": 5}, 
  {"ip": "2.3.4.10", "bytes": 1},
  {"ip": "10.20.30.40", "bytes": 0}, 
  {"ip": "0.0.0.0", "bytes": 10}, 
  {"ip": "2.3.4.10", "bytes": 6}]

Output as unique ip address with bytes added for duplicated ip addresses输出为唯一的 IP 地址,并为重复的 IP 地址添加字节

[{'ip': '0.0.0.0', 'bytes': 10}, 
 {'ip': '10.20.30.40', 'bytes': 0}, 
 {'ip': '2.3.4.10', 'bytes': 17}, 
 {'ip': '5.6.2.3', 'bytes': 10}, 
 {'ip': '1.2.3.4', 'bytes': 35}]

I wrote a code like this in Python我在 Python 中写了这样的代码

import json
logs = """[{"ip": "1.2.3.4", "bytes": 10}, {"ip": "2.3.4.10", "bytes": 10}, {"ip": "5.6.2.3", "bytes": 10},
           {"ip": "1.2.3.4", "bytes": 20}, {"ip": "1.2.3.4", "bytes": 5}, {"ip": "2.3.4.10", "bytes": 1},
           {"ip": "10.20.30.40", "bytes": 0}, {"ip": "0.0.0.0", "bytes": 10}, {"ip": "2.3.4.10", "bytes": 6}]"""

logs_json = json.loads(logs)


ips_unique = set(ip.get("ip") for ip in logs_json)


ip_unique_list = []
for ip in ips_unique:
        ip_dict = {"ip": ip, "bytes": 0}
        ip_unique_list.append(ip_dict)

for ip_unique_sep in ip_unique_list:
        for log in logs_json:
                if log["ip"] == ip_unique_sep["ip"]:
                        ip_unique_sep["bytes"] += log["bytes"]

print(ip_unique_list)

Is there any better and efficient way to achieve the same?有没有更好更有效的方法来实现同样的目标?

You can use defaultdict initialized with int (to produce zeros by default), and just loop over the parsed input and sum the bytes from the IPs.您可以使用用int初始化的defaultdict (默认情况下生成零),然后遍历解析的输入并对来自 IP 的字节求和。 Something like:就像是:

result = defaultdict(int)
for item in json_logs:
    result[item.get('ip')] += item['bytes']
print(result)

Using a simple loop:使用一个简单的循环:

out = {}
for d in logs_json:
    if d['ip'] in out:
        out[d['ip']]['bytes'] += d['bytes']
    else:
        out[d['ip']] = d.copy()

result = list(out.values())

Output:输出:

[{'ip': '1.2.3.4', 'bytes': 35},
 {'ip': '2.3.4.10', 'bytes': 17},
 {'ip': '5.6.2.3', 'bytes': 10},
 {'ip': '10.20.30.40', 'bytes': 0},
 {'ip': '0.0.0.0', 'bytes': 10}]
out = dict.fromkeys((x['ip'] for x in logs_json), 0)
for x in logs_json:
    out[x['ip']] += x['bytes']

out = [{'ip':ip,'bytes':bytes} for ip, bytes in out.items()]
print(out)

# Output:

[{'ip': '1.2.3.4', 'bytes': 35}, 
{'ip': '2.3.4.10', 'bytes': 17}, 
{'ip': '5.6.2.3', 'bytes': 10}, 
{'ip': '10.20.30.40', 'bytes': 0}, 
{'ip': '0.0.0.0', 'bytes': 10}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM