unsupported operand type(s) for +: 'int' and 'tuple'

Classydogg

I need to complete a task where I am given a list of names and grades for each name and I need to sort these out by highest to lowest grade. However if two names have the same grades then sort those names out in alphabetical order. This is the problem I am presented with. I am required to keep the sort function on one line.

a = [('Tim Jones', 54), ('Anna Smith', 56), ('Barry Thomas', 88)]
sum(sorted(a,key=lambda x: x[1]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

Any idea on how this can be resolved? I've tried to work it out for many days now.

UPDATED

Ok thanks for helping me work out that one, however there is another scenario where I need to resolve as well.

a = [('Tim Jones', 'C'), ('Anna Smith', 'B'), ('Barry Thomas', 'A')]
sorted(a,key=lambda x: -x[1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
TypeError: bad operand type for unary -: 'str'

So this is the current situation basically all I need to do now is organise the list so then it goes from highest grade to lowest grade.

Bill Lynch

Let's note that sorted() will return a copy of your original list, but sorted:

>>> a = [('Tim Jones', 54), ('Anna Smith', 56), ('Barry Thomas', 88)]
>>> sorted(a,key=lambda x: x[1])
[('Tim Jones', 54), ('Anna Smith', 56), ('Barry Thomas', 88)]

Python itself will fail when attempting to sum these three tuples, because it doesn't really have any clear meaning.

('Tim Jones', 54)
('Anna Smith', 56)
('Barry Thomas', 88)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

unsupported operand types for //: 'str', 'int'

From Dev

python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'

From Dev

What does "unsupported operand type(s) for -: 'int' and 'tuple'" means?

From Dev

python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'

From Dev

Unsupported operand types 'NoneType'

From Dev

Django unsupported operand types

From Dev

TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'

From Dev

unsupported operand types for * : 'float' and 'decimal'

From Dev

CuPy and Dirichlet gives me TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

From Dev

CuPy and Dirichlet gives me TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

From Dev

Trying to average a list, but I don't know what is meant by the error: unsupported operand type(s) for +: 'int' and 'tuple'

From Dev

TypeError: unsupported operand type(s) for |: 'tuple' and 'bool'

From Dev

TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

From Dev

TypeError: unsupported operand type(s) for %: 'Text' and 'tuple'

From Dev

Python: unsupported operand type(s) for /: 'tuple' and 'float'

From Dev

Unsupported operand types for bitwise exclusive OR in Python

From Dev

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

From Dev

unsupported operand type(s) for +: 'NoneType' and 'int'

From Dev

Unsupported operand type(s) for +: 'int' and 'str'

From Dev

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

From Dev

Unsupported operand type(s) for /: 'unicode' and 'int'

From Dev

TypeError: unsupported operand int and NoneType? Python

From Dev

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

From Dev

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

From Dev

TypeError: unsupported operand type(s) for |: 'int' and 'str'

From Dev

Unsupported operand type(s) for +: 'dict' and 'int'

From Dev

Python - Unsupported Operand type(s) for +: 'int' and 'list'

From Dev

TypeError: unsupported operand type(s) for +: 'int' and 'Element'

From Dev

Python - Unsupported Operand Type(s) for -: 'int' and 'str'

Related Related

  1. 1

    unsupported operand types for //: 'str', 'int'

  2. 2

    python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'

  3. 3

    What does "unsupported operand type(s) for -: 'int' and 'tuple'" means?

  4. 4

    python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'

  5. 5

    Unsupported operand types 'NoneType'

  6. 6

    Django unsupported operand types

  7. 7

    TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'

  8. 8

    unsupported operand types for * : 'float' and 'decimal'

  9. 9

    CuPy and Dirichlet gives me TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

  10. 10

    CuPy and Dirichlet gives me TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

  11. 11

    Trying to average a list, but I don't know what is meant by the error: unsupported operand type(s) for +: 'int' and 'tuple'

  12. 12

    TypeError: unsupported operand type(s) for |: 'tuple' and 'bool'

  13. 13

    TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

  14. 14

    TypeError: unsupported operand type(s) for %: 'Text' and 'tuple'

  15. 15

    Python: unsupported operand type(s) for /: 'tuple' and 'float'

  16. 16

    Unsupported operand types for bitwise exclusive OR in Python

  17. 17

    TypeError: unsupported operand type(s) for +=: 'int' and 'list'

  18. 18

    unsupported operand type(s) for +: 'NoneType' and 'int'

  19. 19

    Unsupported operand type(s) for +: 'int' and 'str'

  20. 20

    TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

  21. 21

    Unsupported operand type(s) for /: 'unicode' and 'int'

  22. 22

    TypeError: unsupported operand int and NoneType? Python

  23. 23

    TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

  24. 24

    TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

  25. 25

    TypeError: unsupported operand type(s) for |: 'int' and 'str'

  26. 26

    Unsupported operand type(s) for +: 'dict' and 'int'

  27. 27

    Python - Unsupported Operand type(s) for +: 'int' and 'list'

  28. 28

    TypeError: unsupported operand type(s) for +: 'int' and 'Element'

  29. 29

    Python - Unsupported Operand Type(s) for -: 'int' and 'str'

HotTag

Archive