Given a list of numbers.
lst=[1,1,2,4,8,2]
find the unique minimum value
Ideally the answer should be 4, But how do we get to 4?
Given a list of numbers.
lst=[1,1,2,4,8,2]
find the unique minimum value
Ideally the answer should be 4, But how do we get to 4?
This is one approach.
Ex:
lst=[1,1,2,4,8,2]
print(min([i for i in set(lst) if lst.count(i) == 1]))
# --> 4
lst and filtering out the elements that have a count not equal to 1 and then applying min.Basically, how you do this is you first sort the list. And then you check from the lowest if the count == 1. If the count != 1 keep checking until you find the 'unique' number.
def get_lowest_and_unique(lst):
for x in sorted(list(set(lst))):
if lst.count(x) == 1:
return x
print("Could not find the correct element!")
I would say that the best option is to use collections.Counter:
from collections import Counter
lst=[1,1,2,4,8,2]
c = Counter(lst)
min(v for v, n in c.items() if n == 1)
4
try
t = list(set(lst))
list.sort(t)
unique_min = t[0]
min(lst)set(lst) makes all elements unique... You need to first filter-out the duplicates and then look for the minimum. @Philipp well, min(sorted(lst)) and min(lst) should probably be the same thing...