Pythonicな辞書の使い方

Python のデータ型

Jason Myers

Instructor

辞書をよりPythonicに扱う

  • .items() メソッドはイテレート可能なオブジェクトを返す
for gallery, phone_num in art_galleries.items():
    print(gallery)
    print(phone_num)
'Miakey Art Gallery'
'(718) 686-0788'
'Morning Star Gallery Ltd'
'(212) 334-9330'}
'New York Art Expo Inc'
'(212) 363-8280'
Python のデータ型

辞書のデータ確認

  • .get() はキーの確認に多くの処理が必要
  • in 演算子はより効率的で明確
'11234' in art_galleries
False
if '10010' in art_galleries:
    print('I found: %s' % art_galleries['10010'])
else:
    print('No galleries found.')
I found: {'Nyabinghi Africian Gift Shop': '(212) 566-3336'}
Python のデータ型

では、練習しましょう!

Python のデータ型

Preparing Video For Download...