未分類

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

matplotlib で x軸・y軸ラベルをつける

plt.xlabel と plt.ylabel を使う。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, …

no image

python3 で range からリストを生成する方法

list をrange を指定して初期化すれば良い。数を1つ飛ばしのリストも次のようにして作れる。 例 list1 = list(range(1,10,2)) print(list1) 結果 [1, …

no image

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

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

no image

tex で かけ算の中点を入力する方法

かけ算に使うような中点を入力するには、 cdot を使う。 例 a=bc 結果

no image

python3 でカンマの入った数値の文字列を数値に変換する方法

replace と int 変換を組み合わせる。 例 str1 = ‘1,234,567’ int1 = int(str1.replace(‘,’,”)) print(int1) 結果 123456 …

S