Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I realize that this sounds paltry however I didn't understand that the sort() function of Python was abnormal. I have a rundown/list of "numbers" that are really in string structure, so I first proselyte/convert them to ints, at that point endeavor a sort. 

list1=["1","10","3","22","23","4","2","200"]

for item in list1:

    item=int(item)

list1.sort()

print list1

Gives me:

['1', '10', '2', '200', '22', '23', '3', '4']

But, What I want is:

['1','2','3','4','10','22','23','200']

I've searched for a portion of the algorithms related to arranging numeric sets, however the ones I discovered all include arranging alphanumeric sets.

I realize this is likely an easy decision issue however google and my coursebook doesn't offer much else or less valuable than the .sort() function.

closed

4 Answers

0 votes
by (25.7k points)
selected by
 
Best answer
In your code, the issue arises because you are converting the items in the list to integers, but not updating the list itself with the converted values. Additionally, the sort() function performs a lexicographic sort by default, which means it treats the items as strings rather than numbers.

To achieve the desired sorting based on numerical values, you can use the key parameter of the sort() function. Here's an updated version of your code:

list1 = ["1", "10", "3", "22", "23", "4", "2", "200"]

list1 = [int(item) for item in list1]  # Convert items to integers

list1.sort(key=int)  # Sort the list based on integer values

print(list1)

Output:

[1, 2, 3, 4, 10, 22, 23, 200]

In this code, a list comprehension is used to iterate over list1 and convert each item to an integer. The list is then sorted using the sort() function with the key parameter set to int. This tells Python to perform the sorting based on the integer values of the items in the list.

By applying these changes, you will get the desired output with the list sorted in ascending order based on numerical values.
0 votes
by (26.4k points)

You haven't really changed your strings over to ints. Or then again rather, you did, yet then you didn't do anything with the outcomes. What you need is:

list1 = ["1","10","3","22","23","4","2","200"]

list1 = [int(x) for x in list1]

list1.sort()

In the event that for reasons unknown you need to keep strings rather than ints (typically an impractical notion, yet perhaps you need to save driving zeros or something), you can utilize a key function. sort takes a named boundary/parameter, key, which is a capacity/function that is approached every component before it is analyzed. The key capacity's return esteems are looked at as opposed to contrasting the rundown components straightforwardly:

list1 = ["1","10","3","22","23","4","2","200"]

# call int(x) on each element before comparing it

list1.sort(key=int)

Want to become an expert in Python? Join the python course fast!

0 votes
by (15.4k points)
In your code, the issue arises because you convert the items in the list to integers without updating the list itself with the converted values. Furthermore, the default behavior of the sort() function is to perform a lexicographic sort, treating the items as strings rather than numbers.

To achieve the desired sorting based on numerical values, you can utilize the key parameter of the sort() function. Here's an updated version of your code:

list1 = ["1", "10", "3", "22", "23", "4", "2", "200"]

list1 = [int(item) for item in list1]  # Convert items to integers

list1.sort(key=int)  # Sort the list based on integer values

print(list1)

Output:

[1, 2, 3, 4, 10, 22, 23, 200]

In this updated code, a list comprehension is used to iterate over list1 and convert each item to an integer. The list is then sorted using the sort() function with the key parameter set to int. By specifying key=int, Python understands that the sorting should be based on the integer values of the items in the list.

By implementing these changes, you will obtain the desired output, with the list sorted in ascending order according to the numerical values.
0 votes
by (19k points)
To sort the list numerically, you can use a combination of list comprehension and the sort() function with the key parameter:

list1 = ["1", "10", "3", "22", "23", "4", "2", "200"]

list1.sort(key=int)

print(list1)

Output:

['1', '2', '3', '4', '10', '22', '23', '200']

By converting the items in the list to integers using the int() function and specifying key=int in the sort() function, you achieve the desired numerical sorting.

Related questions

0 votes
1 answer
asked Aug 1, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 10, 2019 in Python by Sammy (47.6k points)
0 votes
4 answers
0 votes
2 answers
asked Jul 22, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...