[Tutor] How to print corresponding keys in Dictionary
Dave Angel
d at davea.name
Mon Oct 24 18:52:24 CEST 2011
(You forgot to include the list on your reply. Easiest way is to do a
reply-all when you're replying)
On 10/24/2011 12:21 PM, Praveen Singh wrote:
> On Mon, Oct 24, 2011 at 9:18 AM, Dave Angel<d at davea.name> wrote:
>
>> <SNIP>
>>
>> def getkey(dictionary, value):
>> for key, val in dictionary.items():
>> if val == value:
>> return key
>>
>>
>> Note that if there are multiple keys with the same value, my function would
>> get only the first. It wouldn't be hard to modify the function to return a
>> list of matching keys.
>>
>> <SNIP >
>>
>>
> Thanks DaveA!! i made this programme to return a list of matching
> keys...thanks for this outline...
> Can you hint me something in this problem??This is another problem..
>
>
>>>> invertDictionary({'a':1, 'b':2, 'c':3, 'd':2})
> {1: ['a'], 2: ['b', 'd'], 3: ['c']}
>
> def reverseLook(dictionary,value):
> a=[]
> d={}
> for key,values in dictionary.iteritems():
>
> d.update({values:a.append(key)}) // this line is not working//
> print d
>
No idea what you're trying to do here. But maybe if you start by
showing the getkeys() function you wrote for the last assignent, we can
see if it's useful for doing this one.
Also when you have multiple runtime errors in your program, and don't
show us the stacktraces, we have to assume you haven't even tried the code.
If I were to run that, I'd get an error calling a non-existent
invertDictionary().
What do you mean "this line is not working" ? Is it crashing the
machine, getting an error (show stacktrace), or just not doing the thing
you expect?
--
DaveA
More information about the Tutor
mailing list