Hi,
I cannot seem to find this without the use of numpy.
I don’t want to use numpy.
Could anyone please help me?
Thanks in advance.
Hi,
I cannot seem to find this without the use of numpy.
I don’t want to use numpy.
Could anyone please help me?
Thanks in advance.
what do you mean by dict(list) ? Are you looking for solutions for both dict and list?
UPDATE : now includes negative values, finds the lowest
mylist = [0,3,2,-1,0,5]
target = min([i for i in mylist if i != 0])
print mylist.index(target)
mydict = {k:v for k, v in zip('PEYCHV', mylist)}
target = min([i for i in mydict.itervalues() if i != 0])
for k,v in mydict.iteritems():
if v == target:
print k
break
yes, as an alternative, whatever is easier, quicker.
I came up with this one for dictionary :
import operator
scrambled_dict = {'a':2,'b':0,'c':1,'d':3}
sorted_scrambled_dict = sorted(scrambled_dict.items(), key=operator.itemgetter(1))
#sorted_scrambled_dict = sorted(scrambled_dict.items(), key=lambda kv: kv[1])
for item in sorted_scrambled_dict:
if item[1] != 0:
print item[0]
break
OK - done, above
Thanks @Dancergraham
Very welcome
I spent so much time last night and this morning that I forgot what I needed that for. I’ll have to look around at my scripts.
I think it was when I was working on solution for this thread:
So did you do it?
Or maybe for conciseness:
a = dict(nine=9, one=1, two=2, three=3, four=4, zilch=0, filth=5, sing=6, devil=0)
print a
b = KeyOfMinNonZero(a)
print b
def KeyOfMinNonZero(dict):
d = [(v, k) for (k, v) in dict.items() if v != 0]
return min(d)[1]
Regards
Jeremy
I just understood the algorithm to get the min non-zero index.
And I’m at work.
Perhaps tonight I’ll try to solve it.
Nice one @jeremy5
I’m not very good at creating these “one-liners” yet.
If I want to add IF or lambda and IF. and it fails whatever I do, I get mad and do the normal iteration