Python ディクショナリ

Pythonでディクショナリを使用するサンプルです。

# -*- coding: Shift_JIS -*
dictionary = {'ONE':'1', 'TWO':'2', 'THREEE':'3'}
print('ディクショナリの中身を表示')
print(dictionary)
print('\n')

print('for文で1つずつ表示')
for i in dictionary:
    print(i)
    print(dictionary[i])
print('\n')

print('要素の追加')
dictionary['FOUR'] = '4'
dictionary['FIVE'] = '5'
print(dictionary)
print('\n')

print('要素の削除')
del dictionary['FOUR']
print('\n')

print('KEY、VALUEのみ表示')
print(dictionary.keys())
print(dictionary.values())
print('\n')

print('KEY、VALUEの取得')
for key, value in dictionary.items():
    print(key, value)
print('\n')

print('KEYの存在確認')
print('FOUR' in dictionary)
print('FIVE' in dictionary)

実行結果1


Bookmark this on Yahoo Bookmark
Bookmark this on Google Bookmarks
Share on LinkedIn
LINEで送る
Pocket

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>