简体   繁体   English

TypeError:“元组”对象不支持项目分配在非元组对象上

[英]TypeError: 'tuple' object does not support item assignment On non tuple object

this code takes an ordered (highest score to lowest score) list of tuples and gathers the name and score of the highest, second highest and third highest scorers. 此代码采用有序的 (从最高分到最低分)元组列表,并收集最高,第二高和第三高得分者的姓名和得分。 If theirs a tie, both names are appended to the same list. 如果他们的关系相同,则两个名称都将附加到同一列表中。

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)

However, 然而,

i[1]=SecondHighestScore

Yields, 产量

TypeError: 'tuple' object does not support item assignment

Despite, 尽管,

print(repr(i[1]))

Yielding, 屈服

'18'

Which is clearly not a tuple. 显然这不是元组。

You can not change tuple()s - they are immutable. 您不能更改tuple() -它们是不可变的。 You could create a new one. 您可以创建一个新的。 Or you could use itertools.groupby to group your tuples together and do some selective output: 或者,您可以使用itertools.groupby将元组分组在一起,并执行一些选择性输出:

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

from itertools import groupby

grped = groupby(myresults, lambda x: int(x[1])) 

# create a dict for all results
result_dict = {}
for key in grped :
    result_dict[key[0]] = [value for value,_ in key[1]] 

# print top 3 results:
for k in sorted(result_dict,reverse=True):
    print(k)
    print(result_dict[k])

# whole dict
print(result_dict)

Output: 输出:

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

# whole dict
{18: ['Raven'], 8: ['Cobra'], 6: ['Lion', 'Swine'], 2: ['Otter']}            

Second wayy to solve that by using a collections.defaultdict : 通过使用collections.defaultdict解决此问题的第二种方法:

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

from collections import defaultdict

result_dict = defaultdict(list)

for value,key in myresults:
    result_dict[int(key)].append(value)

for k in sorted(result_dict,reverse=True):
    print(k)
    print(result_dict[k])

print(result_dict)


18
['Raven']
8
['Cobra']
6
['Lion', 'Swine']
2
['Otter']

# whole dict
defaultdict(<class 'list'>, {18: ['Raven'], 8: ['Cobra'], 
                              6: ['Lion', 'Swine'], 2: ['Otter']})

Doku: Doku:

Here is my solution: 这是我的解决方案:

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

output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM