[英]average of specific values of list of dictionary values python
Hey I'm trying to get the average of values from a list the list has values from a dictionary I keep returning this error嘿,我正在尝试从列表中获取值的平均值,列表中包含来自字典的值,我一直在返回此错误
all_students_avg = sum(numbers_lists) / len(numbers_lists)
TypeError: unsupported operand type(s) for +: 'int' and 'list'类型错误:不支持 + 的操作数类型:'int' 和 'list'
I don't understand, the values I'm calling are are integers already and I've tried converting them to integers and float.我不明白,我调用的值已经是整数,我已经尝试将它们转换为整数和浮点数。
Bob = {'name': 'bob', 'assignments': 80.0, 'presentation': 80.0, 'lab tasks': 60.0}
Geoff = {'name': 'Geoff', 'assignments': 90.0, 'presentation': 90.0, 'lab tasks': 90.0}
numbers_lists = [Bob['assignments'], Bob['presentation'], Bob['lab tasks']], [Geoff['assignments'], "\n",
Geoff['presentation'],
Geoff['lab tasks']]
all_students_avg = sum(numbers_lists) / len(numbers_lists)
print("This is all the students average score across all subjects: ", all_students_avg, )
Python list has a format like: l = [element1, element2, element3]
. Python 列表的格式如下: l = [element1, element2, element3]
。 Your list was looking like: numbers_lists = [[element1, element2], "\\n", [elem1, elem2]]
because your list not only include two lists but it also contains a newline character that can't be summed with integer.您的列表看起来像: numbers_lists = [[element1, element2], "\\n", [elem1, elem2]]
因为您的列表不仅包含两个列表,而且还包含一个不能与整数相加的换行符。
I made a little modification to your code and this should work:我对您的代码进行了一些修改,这应该可以工作:
Bob = {'name': 'bob', 'assignments': 80.0, 'presentation': 80.0, 'lab tasks': 60.0}
Geoff = {'name': 'Geoff', 'assignments': 90.0, 'presentation': 90.0, 'lab tasks': 90.0}
bob_scores = [Bob['assignments'], Bob['presentation'], Bob['lab tasks']]
geoff_scores = [Geoff['assignments'], Geoff['presentation'], Geoff['lab tasks']]
numbers_lists = bob_scores + geoff_scores
all_students_avg = sum(numbers_lists) / len(numbers_lists)
print("This is all the students average score across all subjects: ", all_students_avg)
output:输出:
This is all the students average score across all subjects: 81.66666666666667
The reason you're getting an error is because the variable numbers_lists
is in list format.您收到错误的原因是变量numbers_lists
是列表格式。
You may try this你可以试试这个
print(numbers_lists)
Inorder to handle this problem, you can convert all elements to integers using map()
.为了解决这个问题,您可以使用map()
将所有元素转换为整数。
num_list = list(map(int,numbers_lists))
Your list structure is not going to work as you expect it to.您的列表结构不会像您期望的那样工作。 The list you have will need to be unpacked as seen here https://www.w3schools.com/python/python_tuples_unpack.asp .您拥有的列表需要解压缩,如此处所示https://www.w3schools.com/python/python_tuples_unpack.asp 。
You could add extra brackets to the ends to make a list of multiple lists.您可以在末尾添加额外的括号以制作多个列表的列表。
Bob = {'name': 'bob', 'assignments': 80.0, 'presentation': 80.0, 'lab tasks': 60.0}
Geoff = {'name': 'Geoff', 'assignments': 90.0, 'presentation': 90.0, 'lab tasks': 90.0}
# if you want 2 seperate lists you could "unpack" them like this
numbers_lists1, number_lists2 = [Bob['assignments'], Bob['presentation'], Bob['lab tasks']], [Geoff['assignments'], "\n",
Geoff['presentation'],
Geoff['lab tasks']]
print(numbers_lists1)
print(number_lists2)
# if you want a list with 2 lists in it you will need to add extra [] on the ends
fixed_numbers_lists = [[Bob['assignments'], Bob['presentation'], Bob['lab tasks']], [Geoff['assignments'], "\n",
Geoff['presentation'],
Geoff['lab tasks']]]
print(fixed_numbers_lists)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.