Python program to find the redundancy rates for each row of a matrix

PythonServer Side ProgrammingProgramming

When it is required to find the redundancy rates for every row of a matrix, a simple iteration and the ‘append’ method can be used.

Example

Below is a demonstration of the same

my_list = [[91, 52, 12, 29, 33], [54, 54, 54, 54, 54], [11, 22, 33, 59, 95]]

print("The list is :")
print(my_list)

my_result = []
for sub in my_list:
   my_result.append(1 - len(set(sub)) / len(sub))

print("The result is :")
print(my_result)

Output

The list is :
[[91, 52, 12, 29, 33], [54, 54, 54, 54, 54], [11, 22, 33, 59, 95]]
The result is :
[0, 1, 0]

Explanation

  • A list of list is defined and is displayed on the console.

  • An empty list is created.

  • The original list is iterated over, and when a condition is met, it is appended to the empty list.

  • This is displayed as the output on the console.

raja
Published on 15-Sep-2021 11:38:00
Advertisements