0
0

More than 3 years have passed since last update.

辞書型のメソッド

Posted at
>>> d = {'x': 10, 'y': 20}
>>> d
{'x': 10, 'y': 20}
>>> d.keys()
dict_keys(['x', 'y'])
>>> d.values()
dict_values([10, 20])
>>> d2 = {'x': 1000,'j': 500}
>>> d.update(d2)
>>> d
{'x': 1000, 'y': 20, 'j': 500}
>>> d.get('x')
1000
>>> d['z']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'z'
>>> d.get('z')
>>> r=d.get('z')
>>> r
>>> type(r)
<class 'NoneType'>
>>> d.pop('x')
1000
>>> d
{'y': 20, 'j': 500}
>>> del d['y']
>>> d
{'j': 500}
>>> del d
>>> d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'd' is not defined
>>> d = {'a': 100, 'b': 200}
>>> d
{'a': 100, 'b': 200}
>>> d.clear()
>>> d
{}
>>> d = {'a': 100, 'b': 200}
>>> 'a' in d
True
>>> 'j' in d
False
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0