Open In App

Python – Index Ranks of Elements

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of elements, our task is to get the index ranks of each element. 

Index Rank of Number = (Sum of occurrence indices of number) / number

Input : test_list = [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9]

Output : [(1, 16.0), (2, 10.0), (3, 9.333333333333334), (4, 1.5), (5, 0.6), (6, 0.3333333333333333), (8, 1.25), (9, 2.2222222222222223)]

Explanation : 1 occurs in list at index 7 and 9. 9 + 7 = 16 / 1 = 16. 

Input : test_list = [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9]

Output : [(1, 16.0), (2, 10.0), (3, 9.333333333333334), (4, 1.5), (5, 0.6), (6, 0.3333333333333333), (8, 1.25), (9, 2.2222222222222223)]

Explanation : 5 occurs at index 3. 3 / 5 = 0.6.

Example : Using loop + filter() + list comprehension + set() + sum() + loop

In this, the indices of elements are extracted using filter() and list comprehension. The set() is used to get unique numbers that are present in the list. The sum() is used to compute the summation of all indices. 

Python3

# Python3 code to demonstrate working of
# Index Ranks of Elements
# Using loop + filter() + list comprehension. + set() + sum() + loop
 
# initializing list
test_list = [3, 4, 6, 5, 3, 4, 9,
             1, 2, 1, 8, 3, 2, 3, 9]
              
# printing original list
print("The original list is : " + str(test_list))
 
res = []
all_ele = set(test_list)
for ele in all_ele:
     
    # getting indices of each element
    indices = list(filter(lambda sub: test_list[sub] == ele, range(len(test_list))))
     
    # index rank
    idx_rank = sum(indices) / ele
    res.append((ele, idx_rank))
 
# printing result
print("Index rank of each element : " + str(res))

                    

Output:

The original list is : [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9]

Index rank of each element : [(1, 16.0), (2, 10.0), (3, 9.333333333333334), (4, 1.5), (5, 0.6), (6, 0.3333333333333333), (8, 1.25), (9, 2.2222222222222223)]

Time Complexity: O(n*n)
Auxiliary Space: O(n)

Method 2 :  using dictionary, list comprehension and lambda function

Step-by-Step Approach:

Initialize the input list test_list.
Print the original list.
Using set() function, get a set of unique elements in the list.
Use dictionary comprehension to get index rank of each element in the list. Inside the comprehension, the lambda function filters out indices of the current element from the list and then the sum of those indices is divided by the element to get the index rank.
Sort the dictionary by value using the sorted() function with the key parameter set to lambda function.
Print the result.

Python3

# initializing list
test_list = [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using dictionary comprehension to find index ranks of elements
res = {ele: sum(filter(lambda sub: test_list[sub] == ele, range(len(test_list))))/ele for ele in set(test_list)}
 
# sorting the dictionary by value
res = sorted(res.items(), key=lambda x: x[1])
 
# printing result
print("Index rank of each element : " + str(res))

                    

Output
The original list is : [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9]
Index rank of each element : [(6, 0.3333333333333333), (5, 0.6), (8, 1.25), (4, 1.5), (9, 2.2222222222222223), (3, 9.333333333333334), (2, 10.0), (1, 16.0)]

Time complexity: O(n^2), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads