python - Finding keys for a value in a dictionary of integers -


the problem write python function returns list of keys in adict value target. keys , values in dictionary integers , keys in list return must in increasing order.

this work have far:

def keyswithvalue(adict, target):     '''     adict: dictionary     target: integer     '''     ans = []     if target not in adict.values():         return ans     else:         key in adict.keys():             if target in adict[key]:                 ans+=[key]         return ans.sort() 

i keep on getting:

"typeerror: argument of type 'int' not iterable"

but don't understand means, , how fix it. if help, i'd grateful!

the issue here

if target in adict[key]: 

you trying iterate on integer value, wont work.

you should instead use

if target == adict[key]: 

you can refactor code this. have made assumption input data looks like. if i'm wrong can adjust answer.

d = {1:1, 2:2, 3:3, 4:3, 5:3}  def keyswithvalue(adict, target):     ans = []     #for k, v in adict.iteritems():     k, v in adict.items():         if v == target:           ans.append(k)     return sorted(ans)  print(keyswithvalue(d, 3)) 

the commented line should used python 2.x instead of line below it.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -