0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonの辞書(dictionary)の操作と初期化

Last updated at Posted at 2023-10-29

はじめに

Pythonの辞書(dictionary)の操作と初期化をまとめる。

辞書の特徴

  • keyとvalueから構成 {key1:value1, key2:value2, key3:value3 }
  • keyは不可変な型しか設定できない。keyにリストや辞書を設定できない。valueは制約ない。
  • keyは重複できない。valueは重複できる。

辞書の操作

操作 コマンド
dict(),zip()で辞書作成 dic=dict( zip(key_list, val_list) )
辞書追加
(既に存在するkeyの場合はvalue上書き)
dic[key] = value
辞書抜き出し dic.pop(key)
辞書初期化 dic.clear()
辞書結合 dic.update(dic2)
辞書探索
(存在しないKeyの場合はエラーになる)
dic[key]
辞書探索
(エラー発生させない)
dic.get(key,err)

errは、存在しないkeyの場合に返答するパラメータ。errなしにするとNoneを返答する。
keyのリスト dic.keys()
valueのリスト dic.values()
keyとvalueのリスト dic.items()
keyとvalueがあるか確認 if key in dict:
if value in dict.values():

辞書操作と初期化の例

dic_test.py
#----------------------------------
#空辞書作成
#----------------------------------
dic_test = {}

print('# type:',type(dic_test),'length:',len(dic_test),dic_test)
# type: <class 'dict'> length: 0 {}

#----------------------------------
#辞書作成
#----------------------------------
dic_test = {'':0, '':1,'':2, '':3, '':4, '':5, '':6, '':7, '':8, '':9}

print('# type:',type(dic_test),'length:',len(dic_test),dic_test)
# type: <class 'dict'> length: 10 {'零': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9}

#----------------------------------
#zip()とdict()で辞書作成
#----------------------------------
key_list = ['','','','','','','','','','']
val_list = [0,1,2,3,4,5,6,7,8,9]
dic_test=dict(zip(key_list,val_list))

print('# type:',type(dic_test),'length:',len(dic_test),dic_test)
# type: <class 'dict'> length: 10 {'零': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9}

#----------------------------------
#2次元配列をdict()使って辞書作成
#----------------------------------
dic_test = [['A',0], ['B',1],['C',2], ['D',3], ['E',4], ['F',5], ['G',6], ['H',7], ['I',8], ['j',9]]
dic_test=dict(dic_test)

print('# type:',type(dic_test),'length:',len(dic_test),dic_test)
# type: <class 'dict'> length: 10 {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'j': 9}

#----------------------------------
#辞書追加
#----------------------------------
dic_test = {'':0, '':1,'':2, '':3, '':4, '':5, '':6, '':7, '':8, '':9}
dic_test[''] = 10	# dic[key] = value 
dic_test[''] = 10	# dic[key] = value keyが同じだと、上書きされる

print('# type:',type(dic_test),'length:',len(dic_test),dic_test)
# type: <class 'dict'> length: 11 {'零': 10, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9, '十': 10}

#----------------------------------
#辞書削除
#----------------------------------
dic_test.pop('')

print('# type:',type(dic_test),'length:',len(dic_test),dic_test)
# type: <class 'dict'> length: 10 {'零': 10, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9}

#----------------------------------
#辞書全クリア
#----------------------------------
dic_test.clear()

print('# type:',type(dic_test),'length:',len(dic_test),dic_test)
# type: <class 'dict'> length: 0 {}

#----------------------------------
#辞書結合
#----------------------------------
dic_test = {'':0, '':1,'':2, '':3, '':4, '':5, '':6, '':7, '':8, '':9}
dic_test2 = {'A':0, 'B':1,'C':2, 'D':3, 'E':4, 'F':5, 'G':6, 'H':7, 'I':8, 'j':9}
dic_test.update(dic_test2)

print('# type:',type(dic_test),'length:',len(dic_test),dic_test)
# type: <class 'dict'> length: 20 {'零': 0, '一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9, 'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'j': 9}

#----------------------------------
# keyのリスト
#----------------------------------
key_list = list(dic_test.keys())

print('# type:',type(key_list),'length:',len(key_list),key_list)
# type: <class 'list'> length: 20 ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'j']

#----------------------------------
# valueのリスト
#----------------------------------
val_list = list(dic_test.values())

print('# type:',type(val_list),'length:',len(val_list),val_list)
# type: <class 'list'> length: 20 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

#----------------------------------
# key,valueのリスト
#----------------------------------
key_list=[]
val_list=[]
for k,v in dic_test.items():
	key_list.append(k)
	val_list.append(v)

print('# type:',type(key_list),'length:',len(key_list),key_list)
print('# type:',type(val_list),'length:',len(val_list),val_list)
# type: <class 'list'> length: 20 ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'j']
# type: <class 'list'> length: 20 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

#----------------------------------
# key,valueのタプル
#----------------------------------
kv_list=[]
for kv in dic_test.items():
	kv_list.append(kv)

print('# type:',type(kv_list),'length:',len(kv_list),kv_list)
# type: <class 'list'> length: 20 [('零', 0), ('一', 1), ('二', 2), ('三', 3), ('四', 4), ('五', 5), ('六', 6), ('七', 7), ('八', 8), ('九', 9), ('A', 0), ('B', 1), ('C', 2), ('D', 3), ('E', 4), ('F', 5), ('G', 6), ('H', 7), ('I', 8), ('j', 9)]

#----------------------------------
#辞書探索
#----------------------------------
print(dic_test['A'])
# 0

try:
	print(dic_test['Z'])
except:
	print('key is empty.')
# key is empty.

print(dic_test.get('Z'))
# None

print(dic_test.get('Z','key is empty.'))
# key is empty.

#----------------------------------
#辞書探索
#----------------------------------
if 'A' in dic_test:
	print('# key:A は存在する')
# key:A は存在する

if 0 in dic_test.values():
	print('# value:0 は存在する')
# value:0 は存在する

以上

0
1
2

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?