Example:
tuple = ("python", "includehelp", 43, 54.23)
List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered set of values enclosed in square brackets [].
Example:
list = [3 ,1, 5, 7]
A list of tuples is a list whose each element is a tuple.
Example:
upList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]
Sorting tuples by frequency of their absolute difference
We will be sorting all the tuples of the list based on the absolute difference of the elements of the tuples. We will put tuples with a single frequency of absolute difference 1 than 2 and so on.
Input:
[(4,6), (1, 3), (6, 8), (4, 1), (5, 2)]
Output:
[(4, 1), (5, 2), (4, 6), (1, 3), (6, 8)]
We need to find the absolute difference of all elements of the tuple and count the frequency occurrence of the absolute difference and sort the list accordingly.
A way to sort the list of tuples using absolute difference is by first computing the absolute difference of each tuple using the abs() method. Then sorting the tuples using the sorted() method and the count() method to sort the tuples based on the absolute difference count.
Program:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer