Skip to main content

Python | Index minimum value Record

In Python, we can bind structural information in the form of tuples and then can retrieve the same, and has manyfold applications. But sometimes we require the information of a tuple corresponding to a minimum value of another tuple index. This functionality has many applications such as ranking. Let us discuss certain ways in which this can be achieved. 

Method #1 : Using min() + operator.itemgetter()

We can get the minimum of the corresponding tuple index from a list using the key ‘itemgetter’ index provided and then mention the index information required using index specification at the end.

Python3




# Python 3 code to demonstrate
# Index minimum value Record
# using min() + itemgetter()
 
from operator import itemgetter
 
# initializing list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
 
# printing original list
print("Original list : " + str(test_list))
 
# using min() + itemgetter()
# Index minimum value Record
res = min(test_list, key=itemgetter(1))[0]
 
# Printing result
print("The name with minimum score is : " + res)

Output : 
Original list : [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
The name with minimum score is : Varsha

Method #2: Using min() + lambda 

This method is almost similar to the method discussed above, just the difference is the specification and processing of the target tuple index for minimum is done by lambda function. This improved readability of code. 

Python3




# Python 3 code to demonstrate
# Index minimum value Record
# using min() + lambda
 
# initializing list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
 
# printing original list
print("Original list : " + str(test_list))
 
# Index minimum value Record
# using min() + lambda
res = min(test_list, key=lambda i: i[1])[0]
 
# printing result
print("The name with minimum score is : " + res)

Output : 
Original list : [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
The name with minimum score is : Varsha

Time Complexity: O(n)
Auxiliary Space: O(n), where n is the length of the list.

Method #3: Using a for loop to iterate through the list and keep track of the minimum value record

Approach:

  1. Initialize a variable min_record to be the first element of the list, assuming it to be the minimum value record initially.
  2. Loop through the remaining elements of the list using a for loop, starting from the second element.
  3. For each element, compare its second value (i.e., the score) with the second value of min_record using an if statement. If the current element has a lower score than min_record, update min_record to be the current element.
  4. After the loop finishes, min_record will contain the element with the lowest score. Extract the name from min_record using indexing and assign it to a variable res.
  5. Print the result.

Below is the implementation of the above approach:

Python3




# Python 3 code to demonstrate
# Index minimum value Record
# using a for loop
 
# initializing list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
 
# printing original list
print("Original list: " + str(test_list))
 
# using a for loop
# Index minimum value Record
min_record = test_list[0]
 
for record in test_list[1:]:
    if record[1] < min_record[1]:
        min_record = record
res = min_record[0]
 
# printing result
print("The name with minimum score is: " + res)

Output
Original list: [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
The name with minimum score is: Varsha

Time complexity: O(n) where n is the length of the input list. This is because we need to iterate through each element of the list once.
Auxiliary space: O(1). We are only using a constant amount of extra space to store min_record and res.

Method #4: Usingbuilt-in sorted() function

This method sorts the list in ascending order based on the score and returns the name of the first record (with the lowest score).

Python3




# Using the built-in sorted() function
 
# Input list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
 
print("Original list: " + str(test_list))
 
# Sorting list
sorted_list = sorted(test_list, key=lambda x: x[1])
 
res = sorted_list[0][0]
 
# Printing list
print("The name with minimum score is: " + res)

Output
Original list: [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
The name with minimum score is: Varsha

Time complexity: O(n*log(n)) where n is the length of the input list. This is because we need to iterate through each element of the list once.
Auxiliary space: O(1)

https://neveropen.tech/python-index-minimum-value-record/?feed_id=22&_unique_id=683cae47b43dc

Comments

Popular posts from this blog

Bare Metal Billing Client Portal Guide

Contents Order a Bare Metal Server My Custom / Contract Pricing View Contract Details Location Management Order History & Status View Order Details Introduction The phoenixNAP Client Portal allows you to purchase bare metal servers and other phoenixNAP products and services. Using the intuitive interface and its essential tools, you can also easily manage your infrastructure. This quick guide will show you how to use the new form to order a bare metal server and how to navigate through new bare metal features within the phoenixNAP Client Portal. Order a Bare Metal Server An order form is an accordion-based process for purchasing phoenixNAP products. Our order form allows you to view the pricing and order multiple products from the same category at the same time. Note: The prices on the form are per month . A contract is not required. However, if you want a contracted price, you may be eligible for a discount depending on the quantity and ...

Add an element in Array to make the bitwise XOR as K

Given an array arr[] containing N positive integers, the task is to add an integer such that the bitwise Xor of the new array becomes K. Examples: Input: arr[] = 1, 4, 5, 6, K = 4 Output: 2 Explanation: Bit-wise XOR of the array is 6.  And bit-wise XOR of 6 and 2 is 4. Input: arr[] = 2, 7, 9, 1, K = 5 Output: 8   Approach: The solution to the problem is based on the following idea of bitwise Xor: If for two numbers X and Y , the bitwise Xor of X and Y is Z then the bitwise Xor of X and Z is Y. Follow the steps to solve the problem: Let the bitwise XOR of the array elements be X .  Say the required value to be added is Y such that X Xor Y = K . From the above observation, it is clear that the value to be added (Y) is the same as X Xor K . Below is the implementation of the above approach: C++ // C++ code to implement the above approach   #include using namespace std;   // Function to find the required value int find_...

Mahotas – Template Matching

In this article we will see how we can do template matching in mahotas. Template is basically a part or structure of image. In this tutorial we will use “lena” image, below is the command to load it.   mahotas.demos.load('lena') Below is the lena image      In order to do this we will use mahotas.template_match method Syntax : mahotas.template_match(img, template) Argument : It takes image object and template as argument Return : It returns image object    Note : Input image should be filtered or should be loaded as grey In order to filter the image we will take the image object which is numpy.ndarray and filter it with the help of indexing, below is the command to do this   image = image[:, :, 0] Below is the implementation    Python3 # importing required libraries import mahotas import mahotas.demos from pylab import gray, imshow, show import numpy as np import matplotlib.pyplot as plt      # loading image ...