1

I want to sort the numbers in a list based on the last digit.

Here is my code:

import sys
list1 = sys.argv[1]
list1_split = list1.split(",")

sorted_numbers = sorted(list1_split, key=lambda x: x%10)
print("Sorted numbers are: ", sorted_numbers)

This is what I get on the command prompt:

C:\Users\john\pythonprojects\test1> testing.py 45,60,51,24,62,49
Traceback (most recent call last):
  File "C:\Users\john\pythonprojects\test1\testing.py", line 6, in <module>
    sorted_numbers = sorted(list1_split, key=lambda x: x % 10)
  File "C:\Users\john\pythonprojects\test1\testing.py", line 6, in <lambda>
    sorted_numbers = sorted(list1_split, key=lambda x: x % 10)
TypeError: not all arguments converted during string formatting

The expected output should be: [60, 51, 62, 24, 45, 49]

I can't figure out how to solve this error

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
John
  • 29
  • 5
  • 1
    `sys.argv[1]` is a string. `list1` is then also a string. `list1_split` then becomes a list of strings. while sorting, each `x` is then also a string. `x % 10` then is calling the `%` operator on a string - which just happens to be the formatting operator for strings. Ans since `x` is not a format string - you get that error. – rdas Oct 20 '20 at 11:24
  • Not an exact duplicate (because of the list), but related: https://stackoverflow.com/questions/45499191/not-all-arguments-converted-during-string-formatting-no-variables – Tomerikoo Oct 20 '20 at 12:06

4 Answers4

3

list1_split is a list of strings - you need to make them ints. A straightforward way of doing this is to use map.

sorted_numbers = sorted(map(int, list1.split(",")), key=lambda x: x % 10)
Alexandru Dinu
  • 1,159
  • 13
  • 24
1

You're mixing different format there.

Split method resulting list of strings. If you want to perform modulo operation, typecasting the string into integer would work.

sorted_numbers = sorted(list1_split, key=lambda x: int(x)%10)
0

If the input numbers are guaranteed to be non-negative, the numeric computation x % 10 is equivalent to a string operation x[-1]. Besides converting all strings to numbers, you can also change the lambda function to lambda x: x[-1] to make the program work.

GZ0
  • 4,055
  • 1
  • 10
  • 21
0

The argument you are taking from command line will be stored as string. So all the elements in the list1_split list are strings. You cannot perform modulo on them. You can just convert the elements into integer format in the lambda key itself.

like here sorted_numbers = sorted(list1_split, key=lambda x: int(x)%10)

Im getting this output.

enter image description here

Edit: Note that the output is a list of strings. As we have not explicitly converted them to integers.

Sudheer
  • 23
  • 6
  • 1
    Worth noting that (as can be seen from the screenshot) `sorted_numbers` will be a list of **strings** this way – Tomerikoo Oct 20 '20 at 12:45
  • @Tomerikoo yeah sorry my bad, should use a map or list comprehension to make it int. – Sudheer Oct 20 '20 at 12:59
  • It's OK there are others answers for that. I just think it should be added in the answer as a note for clarification – Tomerikoo Oct 20 '20 at 13:01