Python Program to assign each list element value equal to its magnitude order
Last Updated :
08 Mar, 2023
Given a list, the task is to write a Python program to assign each element value equal to its magnitude order.
Input : test_list = [8, 3, 5, 8, 1, 5, 4]
Output : [4, 1, 3, 4, 0, 3, 2]
Explanation : 8 is 5th maximum hence 4 [starting from 0th index] is assigned to it.
Input : test_list = [3, 2, 0]
Output : [2, 1, 0]
Explanation : 3 is 3rd maximum as 2 is assigned.
Method 1 : Using set() + zip() + dict() + list comprehension
In this, we perform task of mapping sorted index of set converted list to values using zip(), and then convert to dictionary of value mapped to values. Then list comprehension is used to ordering in list.
Python3
# Python3 code to demonstrate working of
# Relative Size Ordering
# Using set() + zip() + sorted() + dict() + list comprehension
# initializing list
test_list = [8, 3, 5, 8, 1, 5, 4]
# printing original list
print("The original list is : " + str(test_list))
# assigning order to each value
ord_dict = dict(zip(list(set(test_list)),
range(len(set(test_list)))))
# mapping element with ordered value
res = [ord_dict[ele] for ele in test_list]
# printing result
print("Relative size ordered list : " + str(res))
Output:
The original list is : [8, 3, 5, 8, 1, 5, 4]
Relative size ordered list : [4, 1, 3, 4, 0, 3, 2]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using sorted() + set() + index() + list comprehension
In this, we perform task of conversion to set and then sort, index mapping to order is done using index() and list comprehension.
Python3
# Python3 code to demonstrate working of
# Relative Size Ordering
# Using sorted() + set() + index() + list comprehension
# initializing list
test_list = [8, 3, 5, 8, 1, 5, 4]
# printing original list
print("The original list is : " + str(test_list))
# getting order
ord_dict = list(set(test_list))
# mapping index
res = [ord_dict.index(ele) for ele in test_list]
# printing result
print("Relative size ordered list : " + str(res))
Output:
The original list is : [8, 3, 5, 8, 1, 5, 4]
Relative size ordered list : [4, 1, 3, 4, 0, 3, 2]
Time Complexity: O(n*nlogn) where n is the number of elements in the list “test_list”. The sorted() + set() + index() + list comprehension is used to perform the task and it takes O(n*nlogn) time.
Auxiliary Space: O(n) additional space of size O(n) is required
Similar Reads
Python program to a Sort Matrix by index-value equality count Given a Matrix, the task is to write a Python program that can sort its rows or columns on a measure of the number of values equal to its index number. For each row or column, count occurrences of equality of index number with value. After computation of this count for each row or column, sort the m
6 min read
Python program to Convert a elements in a list of Tuples to Float Given a Tuple list, convert all possible convertible elements to float. Input : test_list = [("3", "Gfg"), ("1", "26.45")] Output : [(3.0, 'Gfg'), (1.0, 26.45)] Explanation : Numerical strings converted to floats. Input : test_list = [("3", "Gfg")] Output : [(3.0, 'Gfg')] Explanation : Numerical str
5 min read
Python program to compute the power by Index element in List Given a list, the task is to write a Python program to compute the power of each element by its index value. Input : test_list = [6, 9, 1, 8, 4, 7]Output : [1, 9, 1, 512, 256, 16807]Explanation : 8 * 8 * 8 = 512, as 8 is at 3rd index. Input : test_list = [6, 9, 1, 8]Output : [1, 9, 1, 512]Explanatio
6 min read
Python - Operation to each element in list Given a list, there are often when performing a specific operation on each element is necessary. While using loops is a straightforward approach, Python provides several concise and efficient methods to achieve this. In this article, we will explore different operations for each element in the list.
2 min read
Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple - Python The task of sorting a list of tuples in Python based on the last element of each tuple is a common task when working with structured data. This involves arranging the tuples in increasing order according to their second or last value, often for easier data analysis or searching. For example, given a
2 min read
Python | Assign value to unique number in list We can assign all the numbers in a list a unique value that upon repetition it retains that value retained to it. This is a very common problem that is faced in web development when playing with id's. Let's discuss certain ways in which this problem can be solved. Method #1: Using enumerate() + list
7 min read