In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to sort Python in descending or ascending order" with detailed content, clear steps and proper handling of details. I hope this article "how to sort Python in descending or ascending order" can help you solve your doubts.
In Python, you can use the sorted () method or the sort () method to sort the data.
Python sort () method
This method accepts a list and sorts it. This method does not return a value.
In this example, we have a list of numbers, and we can use the sort () method to sort the list in ascending order.
My_list = [67,2,999,1,15] # this prints the unordered listprint ("Unordered list:", my_list) # sorts the list in placemy_list.sort () # this prints the ordered listprint ("Ordered list:", my_list)
If the list is sorted, it will return None in the console.
My_list = [6,7,8,9,10] # this will return None because the list is already sortedprint (my_list.sort ())
The sort () method accepts two optional parameters, called key and reverse.
Key has the value of the function that will be called on each item in the list.
In this example, we can use the len () function as the value of the key parameter. Key=len tells the computer to sort the list of names from the smallest to the largest in length.
Names = [Jessica "," Ben "," Carl "," Jackie "," Wendy "] print (" Unsorted: ", names) names.sort (key=len) print (" Sorted: ", names)
Reverse is a Boolean value of True or False.
In this example, reverse=True will tell the computer to sort the list in reverse alphabetical order.
Names = [Jessica "," Ben "," Carl "," Jackie "," Wendy "] print (" Unsorted: ", names) names.sort (reverse=True) print (" Sorted: ", names)
Python sorted () method
This method returns a new sorted list from the iterable object. Examples of iterable objects include lists, strings, and tuples.
One of the main differences between sort () and sorted () is that sorted () returns a new list, while sort () sorts the list.
In this example, we have a list of numbers that will be sorted in ascending order.
Sorted_numbers = sorted ([77, 22, 9,-6, 4000]) print ("Sorted in ascending order:", sorted_numbers)
The sorted () method also accepts optional key and reverse parameters.
In this example, we have a list of numbers sorted in descending order. Reverse=True tells the computer to reverse the list from maximum to minimum.
Sorted_numbers = sorted ([77, 22, 9,-6, 4000], reverse=True) print ("Sorted in descending order:", sorted_numbers)
The other major difference between sorted () and sort () is that the sorted () method accepts any iterable object, while the sort () method applies only to lists.
In this example, we use the split () method to decompose the string into a single word. Then we use sorted () to sort the words from minimum to maximum length.
My_sentence = "Jessica found a dollar on the ground" print ("Original sentence:", my_sentence) print (sorted (my_sentence.split (), key=len))
We can also modify this example to include the key and reverse parameters.
This modified example will now sort the list from maximum to minimum.
My_sentence = "Jessica found a dollar on the ground" print ("Original sentence:", my_sentence) print (sorted (my_sentence.split (), key=len, reverse=True))
We can also use the method tuples on sorted ().
In this example, we have a tuples that represents a collection of band students' names, ages, and instruments.
Band_students = [('Danny', 17,' Trombone'), ('Mary', 14,' Flute'), ('Josh', 15,' Percussion')]
We can use the sorted () method to sort the data by the age of the students. The key can be passed in a lambda function that tells the computer to sort in ascending order by age.
A lambda function is an anonymous function without a name. You can use the lambda keyword to define this type of function.
Lambda student: student [1]
To access the value tuple in a, use parenthesis notation and the index number to access. Since we count from scratch, the age value is [1].
This is a complete example.
Band_students = [('Danny', 17,' Trombone'), ('Mary', 14,' Flute'), ('Josh', 15,' Percussion')] print (sorted (band_students, key=lambda student: student [1]))
We can modify this example and sort the data by instrument instead. We can use reverse's reverse alphabetical order to sort the tools.
Band_students = [('Danny', 17,' Trombone'), ('Mary', 14,' Flute'), ('Josh', 15,' Percussion')] print (sorted (band_students, key=lambda student: student [2], reverse=True)). This article "how to sort Python in descending or ascending order" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.