0

I want to select the minimum value of a nested list of the second element (the float part).

Code

n=int(input("Enter the number: "))
arr=[[input(),float(input())] for _ in range(0,n)]
arr.sort(key=lambda x: (x[1],x[0]))
min_val=min(arr)
print(min_val)

Input

Enter the number: 3
arp
5
grp
4
drp
3

Output

['arp', 5.0]

Expected output

['drp',3.0]
4
  • The min(..) does not care about the key, it sorts the tuples like tuples are sorted by default. Commented Aug 29, 2018 at 20:05
  • 1
    Its a sorted list, just use the first element in it ... arr[0] Commented Aug 29, 2018 at 20:05
  • @PatrickArtner I intended to remove all the minimum elements off the list, that's the reason for it. Commented Aug 29, 2018 at 20:07
  • "remove all the minimum elements"? Commented Aug 29, 2018 at 20:08

3 Answers 3

3

You calculate the minimum with:

n = int(input("Enter the number: "))
arr = [[input(),float(input())] for _ in range(0,n)]
arr.sort(key=lambda x: (x[1],x[0]))
min_val=min(arr)
print(min_val)

This means that you will perform a loop over the list, and obtain the smallest element. Since you did not provide a key, Python will sort, like it sorts tuples by default: first by first element, and in case of a tie by second element and so on.

In case you want the minimum according to a specific key, you need to use the key parameter of the min function:

n = int(input("Enter the number: "))
arr = [[input(),float(input())] for _ in range(0,n)]
min_val=min(arr, key=lambda x: (x[1],x[0]))
print(min_val)

Note that obtaining the minimum is usually faster than sorting the list. If you do not need to sort the list, you can drop it, and simply use min(..) (like here).

In case you need to sort the list anyway, the smallest element is the first element of the list, so you can obtain it with:

n = int(input("Enter the number: "))
arr = [[input(),float(input())] for _ in range(0,n)]
arr.sort(key=lambda x: (x[1],x[0]))
min_val=arr[0]
print(min_val)
Sign up to request clarification or add additional context in comments.

Comments

1

Use min with key as Willem Van Onsem suggested and apply filter to your sequence:

arr=[("a",2),("b","5"),("c",1),("a",2),("b","5"),("c",1)]  
# get minimal value
min_val=min(arr, key= lambda x:(x[1],x[0]))
# filter all with 2nd element euqal to minimals 2nd elemenbt
withoutMin = filter(lambda x: x[1] != min_val[1], arr)

# or use equivalent list comp:
# without = [x for x in arr if x[1] != min_val[x]] 
print(arr)

print(withoutMin)

Output:

[('a', 2), ('b', '5'), ('c', 1), ('a', 2), ('b', '5'), ('c', 1)]
[('a', 2), ('b', '5'), ('a', 2), ('b', '5')] # filtered

Comments

0

since the array is already sorted:

print(arr[0])

OR

min_val=min(arr)

will sort by the first element. To sort by second element, change it to:

min_val=min(arr, key = lambda t: t[1])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.