Помощь в сортировке

Я бы рекомендовал вместо этого использовать метод setdefault. Похоже, он сделает все, что вы захотите.

>>> d = {'foo':'bar'}
>>> q = d.setdefault('foo','baz') #Do not override the existing key
>>> print q #The value takes what was originally in the dictionary
bar
>>> print d
{'foo': 'bar'}
>>> r = d.setdefault('baz',18) #baz was never in the dictionary
>>> print r #Now r has the value supplied above
18
>>> print d #The dictionary's been updated
{'foo': 'bar', 'baz': 18}
1
задан HMW 6 October 2010 в 19:25
поделиться