牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

Python Quick Reference をゆっくり参照してみた 04 辞書/マップ (dict 型) の演算

以下を参照。

辞書/マップ (dict 型) の演算
>>> d = {1:'first', 2:'second', 3:'third'}
>>> d
{1: 'first', 2: 'second', 3: 'third'}
>>> len(d)
3
>>> d = dict()
>>> d
{}
>>> def hoge(**key):
...     return dict(key)
... 
>>> keyword = hoge(a = 'one', b = 'two', c = 'three')
>>> keyword
{'a': 'one', 'c': 'three', 'b': 'two'}
>>> li = [[1,'first'], [2,'second'], [3,'third']]
>>> dict(li)
{1: 'first', 2: 'second', 3: 'third'}
>>> d = dict(keyword)
>>> d
{'a': 'one', 'c': 'three', 'b': 'two'}
>>> d['a']
'one'
>>> d['a'] = '1st'
>>> d
{'a': '1st', 'c': 'three', 'b': 'two'}
>>> del d['c']
>>> d
{'a': '1st', 'b': 'two'}
>>> d2 = d.copy()
>>> d2
{'a': '1st', 'b': 'two'}
>>> d.has_key('b')
True
>>> 'b' in d
True
>>> d.items()
[('a', '1st'), ('b', 'two')]
>>> d.keys()
['a', 'b']
>>> d1 = {}
>>> for k, v in d2.items(): d1[k] = v
... 
>>> d1
{'a': '1st', 'b': 'two'}
>>> d1 = {}
>>> d1.update(d2)
>>> d1
{'a': '1st', 'b': 'two'}
>>> d.values()
['1st', 'two']
>>> d.get('b')
'two'
>>> d.get('e')
>>> d
{'a': '1st', 'b': 'two'}
>>> d.get('e','hugehuge')
'hugehuge'
>>> d
{'a': '1st', 'b': 'two'}
>>> d.setdefault('a')
'1st'
>>> d.setdefault('g')
>>> d
{'a': '1st', 'b': 'two', 'g': None}
>>> d.setdefault('f','hogera')
'hogera'
>>> d
{'a': '1st', 'b': 'two', 'g': None, 'f': 'hogera'}
>>> d.iteritems()
<dictionary-itemiterator object at 0x78d5a0>
>>> d.iterkeys()
<dictionary-keyiterator object at 0x78d6a0>
>>> d.itervalues()
<dictionary-valueiterator object at 0x78d7a0>
>>> d.pop('f')
'hogera'
>>> d
{'a': '1st', 'b': 'two', 'g': None}
>>> d.pop('f')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'f'
>>> d.pop('f', 'foufoufou')
'foufoufou'
>>> d.popitem()
('a', '1st')
>>> d
{'b': 'two', 'g': None}
>>>