我正在尝试遍历字典数组,比较它们之间的每个值,然后将结果附加到字典中。
例如,如果我们具有以下数组:
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 = [{"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']}
您可以建立一个临时词典并为每种疾病建立一套年份。然后使用此字典重新构建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'}}]