Quantcast
Channel: 初心者タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 21093

items()を使った辞書型の処理

$
0
0

items()をつかって、forループでひとつずつ取り出す。

sample.py
dict={"A":1,"B":2,"C":3}fordict_str,dict_numindict.items():print(dict_str,dict_num,sep="→")# 出力結果
# A→1
# B→2
# C→3

中身を見てみるとリストの中にタプルが入っている。

sample.py
print(dict.items())# 出力結果
# dict_items([('A', 1), ('B', 2), ('C', 3)])

これらのタプルがアンパックされて

A → dict_str
1 → dict_num

へと入っていることが読み取れる。


Viewing all articles
Browse latest Browse all 21093

Trending Articles