Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Tuple - max() Method
Next article icon
In this article, we will see how we can sort a tuple (consisting of float elements) using its float elements. Here we will see how to do this by using the built-in method sorted() and how can this be done using in place method of sorting.Examples:
Input : tuple = [('lucky', '18.265'), ('nikhil', '14.107'),                   ('akash', '24.541'), ('anand', '4.256'), ('gaurav', '10.365')]Output : [('akash', '24.541'), ('lucky', '18.265'),           ('nikhil', '14.107'), ('gaurav', '10.365'), ('anand', '4.256')]Input : tuple = [('234', '9.4'), ('543', '16.9'), ('756', '5.5'),                   ('132', '4.2'), ('342', '7.3')]Output : [('543', '16.9'), ('234', '9.4'), ('342', '7.3'),           ('756', '5.5'), ('132', '4.2')]
We can understand this from the image shown below:

Method 1 : Use ofsorted() method

Sorted() sorts a tuple and always returns a tuple with the elements in a sorted manner, without modifying the original sequence. It takes three parameters from which two are optional, here we tried to use all of the three:
  1. Iterable :sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted.
  2. Key(optional) :A function that would server as a key or a basis of sort comparison.
  3. Reverse(optional) : If set true, then the iterable would be sorted in reverse (descending) order, by default it is set as false.
To sort this in ascending order we could have just ignored the third parameter.Python3
# Python code to sort the tuples using float element# Function to sort using sorted()defSort(tup):# reverse = True (Sorts in Descending order)# key is set to sort using float elements# lambda has been usedreturn(sorted(tup,key=lambdax:float(x[1]),reverse=True))# Driver Codetup=[('lucky','18.265'),('nikhil','14.107'),('akash','24.541'),('anand','4.256'),('gaurav','10.365')]print(Sort(tup))
Output:
[('akash', '24.541'), ('lucky', '18.265'), ('nikhil', '14.107'), ('gaurav', '10.365'), ('anand', '4.256')]

Method 2 : In-place way of sorting using sort():

While sorting via this method the actual content of the tuple is changed, while in the previous method the content of the original tuple remained the same.Python3
# Python code to sort the tuples using float element# Inplace way to sort using sort()defSort(tup):# reverse = True (Sorts in Descending order)# key is set to sort using float elements# lambda has been usedtup.sort(key=lambdax:float(x[1]),reverse=True)print(tup)# Driver Codetup=[('lucky','18.265'),('nikhil','14.107'),('akash','24.541'),('anand','4.256'),('gaurav','10.365')]Sort(tup)
Output:
[('akash', '24.541'), ('lucky', '18.265'), ('nikhil', '14.107'),  ('gaurav', '10.365'), ('anand', '4.256')]
For more reference visit:sorted() in Pythonlambda in Python

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp