[Python] Find the second smallest value.

[Python] Find the second smallest value.

This is a personal memo.

· Nested list of given numbers and names ・ Extract the name of the person with the second smallest number in alphabetical order

▼sample input

5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39

▼my answer

if __name__ == '__main__':

    #Prepare an array to store the values
    nameScores=[]
    scores=[]

    for _ in range(int(input())):
        name = input()
        score = float(input())
    
        scores.append(score)
        nameScore=[name, score]
        nameScores.append(nameScore)
    
  #Sort by name alphabetically
    nameScores = sorted(nameScores) 
        
    #Find the second smallest value
    secondlow = sorted(set(scores))[1]
    
    #Extract the name with the minimum value
    names=[]
    for nameScore in nameScores:
        if nameScore[1]==secondlow:
            print(nameScore[0])

Array.append (value) Add value to array

Sorted Sort the array in ascending order (0th element in case of nesting) Alphabet = a ~ Numerical value = 0 ~

Descending order and key elements can be specified as options. ・ Descending order: reverse = True -Key specification: Specify with a lambda expression. key = lambda val: val [index number] key = lambda x: x [key name]

Set () Remove duplicate values set type

Recommended Posts

[Python] Find the second smallest value.
Find the maximum value python (fixed ver)
Find the maximum Python
Find the mood value with python (Rike Koi)
Find the divisor of the value entered in python
Find the difference in Python
Find the maximum python (improved)
Find the definition of the value of errno
Find the Levenshtein Distance with python
Solve the smallest value in Python (equivalent to paiza rank D)
Find the SHA256 value with R (with bonus)
Find the second derivative with JAX automatic differentiation
Find the solution of the nth-order equation in python
Find the geometric mean of n! Using Python
[Introduction to Algorithm] Find the shortest path [Python3]
[Python] Find the transposed matrix in a comprehension
Find the shortest path with the Python Dijkstra's algorithm
python xlwings: Find the cell in the last row
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
Find the cumulative distribution function by sorting (Python version)
How to retrieve the nth largest value in Python
Find out the location of Python class definition files.
[Python] Calculate the average value of the pixel value RGB of the object
the zen of Python
Find the part that is 575 from Wikipedia in Python
Get the value selected in Selenium Python VBA pull-down
Find the Hermitian matrix and its eigenvalues in Python
List find in Python
[Python] Split the date
I tried to find the entropy of the image with python
I want to initialize if the value is empty (python)
Find out the apparent width of a string in python
Inherit the standard library to find the average value of Queue
Write a python program to find the editing distance [python] [Levenshtein distance]
'De' =='De' is False! Actually, the second "de" was two letters. [Python]
Python --Find out number of groups in the regex expression
Find the index of the maximum value (minimum value) of a multidimensional array
Get the value while specifying the default value from dict in Python
How to get the last (last) value in a list in Python
Get index of nth largest / smallest value in list in Python
Find the diameter of the graph by breadth-first search (Python memory)
Find the eigenvalues of a real symmetric matrix in Python
Get index of nth largest / smallest value in list in Python
Extract the value closest to a value from a Python list element
[Python] Find out about pip
python3 Measure the processing speed.
Download the file in Python
[python] Value of function object (?)
Compare the Python array-like guys
About the Python module venv
Shapley value calculation in Python
About the ease of Python
About the enumerate function (python)
[Python] Adjusting the color bar
Find permutations / combinations in Python
Find the factorial prime factorization
[Python] Get the previous month
[Python] numpy.empty initial value setting
[Python 2/3] Parse the format string
Call the API with python3.
About the features of Python