简体   繁体   中英

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. You could create a new one. Or you could use itertools.groupby to group your tuples together and do some selective output:

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 :

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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