未分類

python3 で辞書(dict) の for ループでキーと値を取得する

投稿日:

キーと値を取得するには次のようにする。
キーは dict のループ、値は dict の values() のループで取得できる。

mydictionary = {'color': '白', 'animal': '犬', 'name': 'ポチ' }

# 辞書のキーを取得する
for key in mydictionary:
print(key)

# 辞書の値を取得する
for value in mydictionary.values():
print(value)

結果

color
animal
name
白
犬
ポチ

-未分類

執筆者:


comment

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

関連記事

no image

tex で打ち消し線を入力する方法

次のように入力すればよい。打ち消し線は、英語で strikethrough と呼ぶ。 例 \usepackage[normalem]{ulem} \begin{document} Good \sout …

no image

python3 で文字列を区切り文字を指定してリストに変換する方法

split メソッドを使う。 例 str1 = ‘this, is, a, pen’ str2 = str1.split(‘,’) print(str2) 結果 [‘this’, ‘ is’, ‘ a …

no image

python3 で文字列のリストを結合する方法

list に対して、extend を使う。 例 list1 = [1,2,3] list2 = [4,5,6] list1.extend(list2) print(list1) 結果 [1, 2, 3 …

no image

python3 で、文字列を一文字ずつのリストに変換する方法

string を list() で変換すれば良い。 例 str1 = ‘Good morning.’ list1 = list(str1) print(list1) 結果 [‘G’, ‘o’, ‘o’ …

no image

python3 で文字列の最後の文字を削除する方法

文字列の最後に [:-1] を付けると、最後の文字を削除することができる。 例 res = ‘あいうえお'[:-1] print(res) 結果 あいうえ