类型错误:“元组”对象不支持非元组对象上的项目分配

莱拉·奥威尔

此代码采用有序(从最高分到最低分)的元组列表,并收集最高、第二高和第三高得分者的姓名和得分。如果他们是平局,则两个名字都将附加到同一个列表中。

myresults=[('Raven', '18'), ('Cobra', '8'), ('Lion', '6'), ('Otter', '2')]


FirstScore=myresults[0][1]
SecondHighestScore=0
ThirdHighestScore=0
for i in myresults:
    if i[1]==FirstScore:
        FirstPlacePatrols.append(i[0])
for i in myresults:
    print(i[1])
    print(repr(i[1]))
    if int(i[1])<int(FirstScore):
        if int(i[1])>=SecondHighestScore:
            print(i[1])
            i[1]=SecondHighestScore
            SecondPlacePatrols.append(i[0])
for i in myresults:
    if int(i[1])<SecondHighestScore:
        if int(i[1])>=ThirdHighestScore:
            i[0]=ThirdHighestScore
            ThirdPlacePatrols.append(i[0])
print(FirstPlacePatrols)
print(FirstScore)
print(SecondPlacePatrols)
print(SecondHighestScore)
print(ThirdPlacePatrols)
print(ThirdHighestScore)

然而,

i[1]=SecondHighestScore

产量,

TypeError: 'tuple' object does not support item assignment

尽管,

print(repr(i[1]))

屈服,

'18'

这显然不是元组。

文卡塔查拉姆

这是我的解决方案:

from collections import defaultdict
given_list = [('Raven', '18'), ('Cobra', '8'), ('Lion', '6'), ('Python', '6'),('Otter', '2')]
reversed_dict = defaultdict(list)
for key,value in given_list:
    reversed_dict[int(value)].append(key)

for k in reversed(sorted(reversed_dict)[-3:]):
     print(k,reversed_dict[k])

输出:

18 ['Raven']
8 ['Cobra']
6 ['Lion', 'Python']

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

列表对象上的“类型错误:‘元组’对象不支持项目分配”

元组作为字典的键说:“元组”对象不支持项目分配

类型错误:“图像”对象不支持项目分配

对象不支持项目分配错误

跟踪:sys.argv [:] = args; TypeError:“元组”对象不支持项目分配

列表元组的突变获取“'tuple'对象不支持项目分配”

Python TypeError:“类型”对象不支持项目分配

TypeError:“类型”对象不支持项目分配

TypeError:“ int”对象不支持项目分配错误

错误:“str”对象不支持项目分配

错误:“浮动”对象不支持项目分配

数组操作:错误:“int”对象不支持项目分配

错误:“int”对象不支持项目分配

类型错误:“str”对象不支持迭代中的项目分配

类型错误:“str”对象不支持项目分配熊猫添加列

'tuple'对象不支持项目分配

“'str'对象不支持项目分配”

“方法”对象不支持项目分配

类型错误:“ builtin_function_or_method”对象不支持项目分配。我该如何解决?

TypeError /数组索引;'int'对象不支持项目分配

Python Azure Databrick:“ DataFrame”对象不支持项目分配

Python 脚本... TypeError: 'method' 对象不支持项目分配

bluedata mlops 预测 - “NoneType”对象不支持项目分配

TypeError:“ NodeView”对象不支持项目分配-NetworkX

TypeError:'Tensor'对象不支持TensorFlow中的项目分配

TypeError:“范围”对象不支持项目分配

'str'对象不支持Python中的项目分配

'str'对象不支持Python中的项目分配

'numpy.float64' 对象不支持项目分配