+ 1
How to find max in values of a dictionary with a range?
mydict={"cat":10, "dog":14, "donky":15, "zebra":20} print(max(mydict.values())) how can find the max num of mydict between 10 and 20??
13 Answers
+ 7
I think you're looking for something like this:
mydict={"cat":12, "dog":14, "donkey": 15, "zebra":20}
print(mydict[max(filter(lambda x: 10<mydict[x]<20, mydict))])
or you can replace it with:
print(mydict[max(filter(lambda x: mydict[x] in range(10, 20), mydict))])
if you definitely MUST use range() ;)
EDIT: since the question was changed, I edited my answer accordingly.
+ 4
Works alright, gives 15 now.
+ 4
mydict={"cat":12,"dog":14, "donkey": 15, "zebra":20}
print(mydict[max(filter(lambda x: 10<mydict[x]<20, mydict))])
>>>
15
+ 3
# Or, in a less shorthand way ( often more readable/understandable to everyone's level ^^ ):
max = None
for item in mydict.keys():
if max == None or mydict[item] > max:
max = mydict[item]
print(max);
# You can store the key value as well, if you need to get it too:
max = None
kmx = None
for item in mydict.keys():
if max == None or mydict[item] > max:
max = mydict[item]
kmx = item
print(kmx,max); # FIX: print(kmx,max) instead print(item,max)
# And only the key:
kmx = None
for item in mydict.keys():
if kmx == None or mydict[item] > mydict[kmx]:
kmx = item
print(kmx); # FIX: print(kmx) instead print(item)
+ 3
Mine works, at least outputs the correct answer:
mydict={"cat":12,"dog":14, "zebra":20}
print(mydict[max(filter(lambda x: 10<mydict[x]<15, mydict))])
>>>
14
+ 3
# Well, that's you want the max value between 10 and 20 NOT INCLUDED, and that's you've change the rules since the first question ^^
# Just add a few lines:
mydict={"cat":10, "dog":14, "donky":15, "zebra":20}
imn = 10
imx = 20
max = None
for item in mydict.keys():
if imn < mydict[item] < imx:
if max == None or mydict[item] > max:
max = mydict[item]
print(max);
# You can store the key value as well, if you need to get it too:
max = None
kmx = None
for item in mydict.keys():
if imn < mydict[item] < imx:
if max == None or mydict[item] > max:
max = mydict[item]
kmx = item
print(kmx,max); # FIX: print(kmx,max) instead print(item,max)
# And only the key:
kmx = None
for item in mydict.keys():
if imn < mydict[item] < imx:
if kmx == None or mydict[item] > mydict[kmx]:
kmx = item
print(kmx); # FIX: print(kmx) instead print(item)
+ 2
Mine is working also... except a mistake in the print statement of two last lines results:
I suggest 3 differents solutions, so I output 3 different lines of result:
20
cat 20
cat
... as correct answer is 'zebra', if you replace 'item' variable in call ro print() function, by 'kmx', the variable where is stored the key of the max value ( else, item is the index-counter, and will always return the same value ^^
+ 2
Of course it works: but you need to provide the array definition at start ^^
0
thats codes didnt work
0
see the new dic that I define in question please
0
it not work :(
0
can you write complete code that output be 15??
0
who can help me in skype for some question??