繁体   English   中英

比较 Python 列表中的字典并附加结果

[英]Compare dictionaries in a Python list and append results

我试图遍历字典数组,比较它们之间的每个值,然后将结果附加到字典中。

例如,如果我们有以下数组:

epidemics = [{"Disease": "B", "year": "1980"},
             {"Disease": "C", "year": "1975"},
             {"Disease": "B", "year": "1996"},
             {"Disease": "E", "year": "2000"},
             {"Disease": "B", "year": "2020"}]

如果同一种疾病的流行也发生在其他年份,我想要达到的结果是:

 epidemics = [{"Disease": "B", "year": "1980", "occurredIn": ["1996", "2020"]},
             {"Disease": "C", "year": "1975"},
             {"Disease": "B", "year": "1996", "occurredIn": ["1980", "2020"]},
             {"Disease": "E", "year": "2000"},
             {"Disease": "B", "year": "2020", "occurredIn": ["1980", "1996"]}]

这是我到目前为止的地方:

for index, a in enumerate(epidemics):
  v_b = []

  for b in epidemics[index+1:]:
    if a['Disease'] == b['Disease']:
        v_b.append(b["year"])
        a['occurredIn'] = v_b

它打印我:

[{'Disease': 'B', 'year': '1980', 'occurredIn': ['1996', '2020']},
 {'Disease': 'C', 'year': '1975'},
 {'Disease': 'B', 'year': '1996', 'occurredIn': ['2020']},
 {'Disease': 'E', 'year': '2000'}, 
 {'Disease': 'B', 'year': '2020'}]

提前致谢

您可以构建一个临时字典并为每种疾病构建一组年份。 然后使用这本词典改革epidemics列表:

from collections import defaultdict

epidemics = [{"Disease": "B", "year": "1980"},
             {"Disease": "C", "year": "1975"},
             {"Disease": "B", "year": "1996"},
             {"Disease": "E", "year": "2000"},
             {"Disease": "B", "year": "2020"}]

d = defaultdict(set)
for dd in epidemics:
    disease = dd['Disease']
    d[disease].add(dd['year'])

for i,dd in enumerate(epidemics):
    years = d[dd['Disease']]-set([dd['year']])
    if years:
        epidemics[i]['occurredIn'] = years

print(epidemics)

输出:

[{'Disease': 'B', 'year': '1980', 'occurredIn': {'2020', '1996'}},
 {'Disease': 'C', 'year': '1975'},
 {'Disease': 'B', 'year': '1996', 'occurredIn': {'1980', '2020'}},
 {'Disease': 'E', 'year': '2000'},
 {'Disease': 'B', 'year': '2020', 'occurredIn': {'1980', '1996'}}]

您可以根据疾病发生的年份构建一个新字典,例如:

epidemics = [{"Disease": "B", "year": "1980"},
             {"Disease": "C", "year": "1975"},
             {"Disease": "B", "year": "1996"},
             {"Disease": "E", "year": "2000"},
             {"Disease": "B", "year": "2020"}]

output = []
for ep in epidemics:
    other_years = set(
        other_ep['year'] for other_ep in epidemics if (
            ep['Disease'] == other_ep['Disease'] and ep['year'] != other_ep['year']
            )
    )

    output.append(
        ep if not other_years else {**ep, 'occurredIn': list(other_years)}
    )

print(output)
>>> {'Disease': 'B', 'year': '1980', 'occurredIn': ['2020', '1996']}
>>> {'Disease': 'C', 'year': '1975'}
>>> {'Disease': 'B', 'year': '1996', 'occurredIn': ['1980', '2020']}
>>> {'Disease': 'E', 'year': '2000'}
>>> {'Disease': 'B', 'year': '2020', 'occurredIn': ['1980', '1996']}

暂无
暂无

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

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