词典列表在 Python 中引发了关键错误
Dictionary list raised a key error in Python
我有一个字典列表,我想分别计算“增加”和“减少”键的值。我的脚本返回了一个关键错误,这可能是因为并非所有词典都有“增加”和“减少”两者。但我不知道如何解决它。作为 Python 初学者,我们将不胜感激。
list_of_dicts = [{"decreasing": 1}, {"increasing": 4}, {"decreasing": 1}, {"increasing": 3},{"decreasing": 1},
{"increasing": 1}]
values1 = [a_dict["decreasing"] for a_dict in list_of_dicts]
values2 = [a_dict["increasing"] for a_dict in list_of_dicts]
print(values1)
print(values2)
预期结果是:
[1,1,1]
[4,3,1]
您可以使用 sum()
+ dict.get
和默认值 0
:
values1 = sum(d.get("increasing", 0) for d in list_of_dicts)
values2 = sum(d.get("decreasing", 0) for d in list_of_dicts)
print("Increasing:", values1)
print("Decreasing:", values2)
打印:
Increasing: 29
Decreasing: 9
编辑:获取值:
values1, values2 = [], []
for d in list_of_dicts:
if "increasing" in d:
values1.append(d["increasing"])
elif "decreasing" in d:
values2.append(d["decreasing"])
print("Increasing:", values1)
print("Decreasing:", values2)
打印:
Increasing: [4, 3, 1, 5, 11, 1, 4]
Decreasing: [1, 1, 1, 1, 2, 1, 2]
在列表理解中添加一个if
,这样可以保留好的
values1 = [a_dict["decreasing"] for a_dict in list_of_dicts if "decreasing" in a_dict]
values2 = [a_dict["increasing"] for a_dict in list_of_dicts if "increasing" in a_dict]
print(values1) # [1, 1, 1, 1, 2, 1, 2]
print(values2) # [4, 3, 1, 5, 11, 1, 4]
在处理有问题的列表理解时,将它们分解。
首先可以分解为:
values1 = list()
for a_dict in list_of_dicts:
if a_dict.get('decreasing'):
values1.append(a_dict.get('decreasing'))
values2 = list()
for a_dict in list_of_dicts:
if a_dict.get('increasing'):
values2.append(a_dict.get('increasing'))
然后再将它们带回领悟中。
list_of_dicts = [{"decreasing": 1}, {"increasing": 4}, {"decreasing": 1}, {"increasing": 3},{"decreasing": 1},
{"increasing": 1},{"decreasing": 1}, {"increasing": 5}, {"decreasing": 2}, {"increasing": 11},
{"decreasing": 1}, {"increasing": 1},{"decreasing": 2}, {"increasing": 4}]
values1 = [a_dict.get('decreasing') for a_dict in list_of_dicts if a_dict.get('decreasing')]
values2 = [a_dict.get('increasing') for a_dict in list_of_dicts if a_dict.get('increasing')]
values11 = list()
for a_dict in list_of_dicts:
if a_dict.get('decreasing'):
values11.append(a_dict.get('decreasing'))
values22 = list()
for a_dict in list_of_dicts:
if a_dict.get('increasing'):
values22.append(a_dict.get('increasing'))
print(values1)
print(values2)
print(values1 == values11)
print(values2 == values22)
结果
$ python testing.py
[1, 1, 1, 1, 2, 1, 2]
[4, 3, 1, 5, 11, 1, 4]
True
True
我有一个字典列表,我想分别计算“增加”和“减少”键的值。我的脚本返回了一个关键错误,这可能是因为并非所有词典都有“增加”和“减少”两者。但我不知道如何解决它。作为 Python 初学者,我们将不胜感激。
list_of_dicts = [{"decreasing": 1}, {"increasing": 4}, {"decreasing": 1}, {"increasing": 3},{"decreasing": 1},
{"increasing": 1}]
values1 = [a_dict["decreasing"] for a_dict in list_of_dicts]
values2 = [a_dict["increasing"] for a_dict in list_of_dicts]
print(values1)
print(values2)
预期结果是:
[1,1,1]
[4,3,1]
您可以使用 sum()
+ dict.get
和默认值 0
:
values1 = sum(d.get("increasing", 0) for d in list_of_dicts)
values2 = sum(d.get("decreasing", 0) for d in list_of_dicts)
print("Increasing:", values1)
print("Decreasing:", values2)
打印:
Increasing: 29
Decreasing: 9
编辑:获取值:
values1, values2 = [], []
for d in list_of_dicts:
if "increasing" in d:
values1.append(d["increasing"])
elif "decreasing" in d:
values2.append(d["decreasing"])
print("Increasing:", values1)
print("Decreasing:", values2)
打印:
Increasing: [4, 3, 1, 5, 11, 1, 4]
Decreasing: [1, 1, 1, 1, 2, 1, 2]
在列表理解中添加一个if
,这样可以保留好的
values1 = [a_dict["decreasing"] for a_dict in list_of_dicts if "decreasing" in a_dict]
values2 = [a_dict["increasing"] for a_dict in list_of_dicts if "increasing" in a_dict]
print(values1) # [1, 1, 1, 1, 2, 1, 2]
print(values2) # [4, 3, 1, 5, 11, 1, 4]
在处理有问题的列表理解时,将它们分解。
首先可以分解为:
values1 = list()
for a_dict in list_of_dicts:
if a_dict.get('decreasing'):
values1.append(a_dict.get('decreasing'))
values2 = list()
for a_dict in list_of_dicts:
if a_dict.get('increasing'):
values2.append(a_dict.get('increasing'))
然后再将它们带回领悟中。
list_of_dicts = [{"decreasing": 1}, {"increasing": 4}, {"decreasing": 1}, {"increasing": 3},{"decreasing": 1},
{"increasing": 1},{"decreasing": 1}, {"increasing": 5}, {"decreasing": 2}, {"increasing": 11},
{"decreasing": 1}, {"increasing": 1},{"decreasing": 2}, {"increasing": 4}]
values1 = [a_dict.get('decreasing') for a_dict in list_of_dicts if a_dict.get('decreasing')]
values2 = [a_dict.get('increasing') for a_dict in list_of_dicts if a_dict.get('increasing')]
values11 = list()
for a_dict in list_of_dicts:
if a_dict.get('decreasing'):
values11.append(a_dict.get('decreasing'))
values22 = list()
for a_dict in list_of_dicts:
if a_dict.get('increasing'):
values22.append(a_dict.get('increasing'))
print(values1)
print(values2)
print(values1 == values11)
print(values2 == values22)
结果
$ python testing.py
[1, 1, 1, 1, 2, 1, 2]
[4, 3, 1, 5, 11, 1, 4]
True
True