Python程序通过其float元素对元组进行排序
在此问题中,我们的任务是使用其float元素对元组(由float元素组成)进行排序。在这里,我们使用内置的sorted()方法,以及如何使用place方法进行排序。
示例
Input:tuple = [('AAA', '10.265'), ('BBB', '24.107'), ('CCC', '26.541'), ('DDD', '14.256'), ('EEE', '11.365')]
Output:
[('CCC', '26.541'), ('BBB', '24.107'), ('DDD', '14.256'), ('EEE', '11.365'), ('AAA', '10.265')]
算法
Step 1: Given a list.Step 2: To sort using sorted ().
Step 3: Sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted.
Step 4: Key(optional) is a function that would serve as a key or a basis of sort comparison.
Step 5: If Reverse (optional) is true, then the iterable would be sorted in reverse (descending) order, by default it is set as false.
范例程式码
# Python code to sort a tuples by its float elementdef tuplesort(A):
return(sorted(A, key = lambda x: float(x[1]), reverse = True))
# Driver Code
A = [('Adwaita', '19.215'), ('Aadrika', '10.117'), ('Babai', '14.589'), ('Mona', '14.216'), ('Sanj', '8.365')]
print("Sort of Tuples By Its Float Number ::",tuplesort(A))
输出结果
Sort of Tuples By Its Float Number :: [('Adwaita', '19.215'), ('Babai', '14.589'), ('Mona', '14.216'), ('Aadrika', '10.117'), ('Sanj', '8.365')]
以上是 Python程序通过其float元素对元组进行排序 的全部内容, 来源链接: utcz.com/z/326708.html