How to find max in values of a dictionary with a range? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 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??

18th Feb 2017, 8:43 PM
hamid
hamid - avatar
13 Respuestas
+ 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.
18th Feb 2017, 9:02 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
Works alright, gives 15 now.
20th Feb 2017, 11:55 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
mydict={"cat":12,"dog":14, "donkey": 15, "zebra":20} print(mydict[max(filter(lambda x: 10<mydict[x]<20, mydict))]) >>> 15
20th Feb 2017, 2:01 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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)
19th Feb 2017, 6:54 AM
visph
visph - avatar
+ 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
20th Feb 2017, 11:47 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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)
20th Feb 2017, 1:29 PM
visph
visph - avatar
+ 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 ^^
20th Feb 2017, 12:10 PM
visph
visph - avatar
+ 2
Of course it works: but you need to provide the array definition at start ^^
20th Feb 2017, 12:45 PM
visph
visph - avatar
0
thats codes didnt work
20th Feb 2017, 11:41 AM
hamid
hamid - avatar
0
see the new dic that I define in question please
20th Feb 2017, 11:53 AM
hamid
hamid - avatar
0
it not work :(
20th Feb 2017, 12:41 PM
hamid
hamid - avatar
0
can you write complete code that output be 15??
20th Feb 2017, 1:18 PM
hamid
hamid - avatar
0
who can help me in skype for some question??
20th Feb 2017, 4:35 PM
hamid
hamid - avatar