reversing a dictionary (newbie)
piet at cs.uu.nl
piet at cs.uu.nl
Wed Mar 21 07:04:28 EST 2001
>>>>> "Christopher Brewster" <C.Brewster at dcs.shef.ac.uk> (CB) writes:
CB> def revdict(diction):
CB> revdict = {}
CB> for wordpair in diction.keys():
CB> #print wordpair
CB> list = []
Why start with an empty list? See below
CB> freq = diction[wordpair]
CB> if revdict.has_key(freq):
CB> list.extend(revdict[freq])
The list was empty, so list=revdict[freq] would be faster
CB> list.append(wordpair)
CB> revdict[freq] = list
But of course revdict[freq].append(wordpair) would
be even better instead of the 3 lines above.
CB> else:
CB> list.append(wordpair)
Here the same. list=[wordpair] would be the same
CB> revdict[freq] = list
But then revdict[freq]=[wordpair] would be the fastest.
CB> return revdict
--
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl
More information about the Python-list
mailing list