'Is there a good way to remove a element from a map?
I made an map called mymap where I would like to remove every element in it that is the biggest in order to find the second biggest. Is there a good way to remove the elements in the map. This is what I have done so far:
n = int(input())
mymap = map(int, input().split())
biggestnum = max(mymap)
mymap.(biggestnum)
secondbiggestnum = max(mymap)
print(secondbiggestnum)
What would be an efficient and easy way to remove all instances of biggestnum?
Solution 1:[1]
You've made this more complicated than it needs to be. Just sort the list, and the biggest will be at the end.
mymap = list(sorted(set(map(int, input().split()))))
print('biggest',mymap[-1])
print('second',mymap[-2])
If you still need the original list, you'd want to separate this into two statement: one to do the map, and one to do the extraction.
Solution 2:[2]
map is a function in python. It takes two parameters:
map(function, iterables). The first parameter is a function and the second is an iterables. The function returns a map class object.
You can convert the map object to list or set.
mymap = map(int, input().split())
mylist = sorted(list(set(mymap)))
# if getting the same element as largest or second largest is not an issue
# then remove the set part and make it mylist = sorted(list(mymap))
print("max:", mylist[-1])
print("second max:", mylist[-2])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | ksohan |
