【问题标题】:average of specific values of list of dictionary values python字典值列表python的特定值的平均值
【发布时间】:2021-10-31 04:30:09
【问题描述】:

嘿,我正在尝试从列表中获取值的平均值,该列表具有字典中的值我一直返回此错误

all_students_avg = sum(numbers_lists) / len(numbers_lists)

TypeError:+ 的不支持的操作数类型:“int”和“list”

我不明白,我调用的值已经是整数,我已经尝试将它们转换为整数和浮点数。

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, )

【问题讨论】:

  • 一个列表有这种格式,numbers_lists = [item1, item2, item3, item4, item5, item6, ...]你有吗?
  • numbers_lists 是一个列表元组,这就是您收到错误的原因。您需要将您的元组扁平化为数字列表,以便sum() 工作。
  • numbers = int(numbers_list) and.. avg = sum(numbers) / sum(numbers)(我不确定)
  • 这不正确@httpanand
  • 哦好的@ddejohn

标签: python list dictionary


【解决方案1】:

Python 列表的格式类似于:l = [element1, element2, element3]。您的列表看起来像:numbers_lists = [[element1, element2], "\n", [elem1, elem2]],因为您的列表不仅包含两个列表,而且还包含一个不能与整数相加的换行符。

我对您的代码做了一些修改,这应该可以工作:

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)


输出:

This is all the students average score across all subjects:  81.66666666666667

【讨论】:

  • 我建议使用bob_scores 和geoff_scores 作为变量,或者使用其他更能区分Bob 和Geoff 的变量。我建议还解释一下 OP 的代码有什么问题。
  • @ddejohn 我已经按照您的建议更新了变量。谢谢!
  • 好的,所以我的错误是列表中有换行符,而 numbers_lists 中有 2 个列表
  • 是的,python sum 函数接受一个列表、元组、字典、集合和数值范围。
【解决方案2】:

您收到错误的原因是变量 numbers_lists 是列表格式。
你可以试试这个

print(numbers_lists)

为了处理这个问题,您可以使用map() 将所有元素转换为整数。

num_list = list(map(int,numbers_lists))

【讨论】:

    【解决方案3】:

    您的列表结构不会像您期望的那样工作。您需要解压缩您拥有的列表,如下所示https://www.w3schools.com/python/python_tuples_unpack.asp。

    您可以在末尾添加额外的括号以创建一个包含多个列表的列表。

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多