我正试图循环遍历一组字典,比较它们之间的每个值,然后将结果附加到字典中。
例如,如果我们有以下数组:
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']}