이 문제에서 float 요소를 사용하여 튜플(float 요소로 구성됨)을 정렬하는 작업입니다. 여기에서 우리는 내장된 sorted() 메소드를 사용하고 정렬의 제자리 메소드를 사용하여 이것이 어떻게 수행될 수 있습니까?
예시
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 element def 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')]