我需要制作一个新字典,它使用与第一个字典相同的键,而原始字典的值是平均值。到目前为止,这些值没有给我带来麻烦,但我不知道如何解决“unhable type”错误。我应该提到原始字典使用元组作为键和值。你知道吗
studentPerf =
{('Jeffery','male','junior'):[0.81,0.75,0.74,0.8],
('Able','male','senior'):[0.87,0.79,0.81,0.81],
('Don','male','junior'):[0.82,0.77,0.8,0.8],
('Will','male','senior'):[0.86,0.78,0.77,0.78]}
dictAvgGrade = {studentPerf.keys():[(sum(grade)/4) for grade in studentPerf.values()]}
使用
dictionary comprehension
:输出:
根据this
这意味着键不能是列表,这就是听写键()返回。所以你可以使用字典理解,就像我之前提到的。应该是这样的。你知道吗
这样就可以了。希望有帮助:)
你的词典理解不正确。试试这个:
出现错误的部分原因是试图使用
studentPerf.keys()
作为字典键,因为它要么是python3中的迭代器,要么是python3中的and list,这两者都不可散列。你知道吗相关问题 更多 >
编程相关推荐