Mark Jackson wrote:
> "Ric Da Force" <[EMAIL PROTECTED]> writes:
> 
>> It is hard to explain but this is what I mean:
>> 
>> Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This is 
>> not'}
>> 
>> I want this to return a new dict with string keys and lists containing the 
>> previous keys for repeated values.
>> 
>> NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']}
> 
> NewDict = {}
> for x in Dict.keys():
>       try:
>               NewDict[Dict[x]].append(x)
>       except KeyError:
>               NewDict[Dict[x]] = [x]

Or, more up-to-date:

NewDict = {}
for key, val in Dict.iteritems():
    NewDict.setdefault(val, []).append(key)

Reinhold
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to