
Code: Alles auswählen
def swapdict(dict):
"""swapdict(dict) -> dict
Vertauscht die Schlüssel und Werte eines Dictionaries. Wenn ein Wert mehr als einmal vorkommt werden die dazugehoerigen Schlüssel in einer Liste mit diesem Wert verbunden.
Turns value to key and the other way round. If a value appears more than one time the keys are set to a list and associated with the value."""
import types
redict={}
for i in dict.keys():
if dict[i] in redict.keys():
if type(redict[dict[i]])==type(types.TupleType()):
redict[dict[i]]+=(i,)
else:
redict[dict[i]]=(redict[dict[i]],i,)
else:
redict[dict[i]]=i
return redict