未分類

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

python で文字列の長さを表示する方法

len を使う。 例 length = len(‘文字列あいうえお’) print(str(length)) 結果 8

no image

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

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

no image

tex で文字の上の点を入力する方法

テキストモードでは \. 数式モードでは \dot{} を使う。 例 \.{a}$\dot{x}$ 結果

no image

tex で文字列を変数として設定する方法

\newcommand を使って指定する。入力の数は自由に指定できる。 例 \newcommand{\variable}[2]{Hello #1. Good #2.} \variable{Taro}{ …

no image

python3 でリストの偶数番目・奇数番目の項目を出力する

リストに対して、::2 を使えば良い。 例 list1 = [‘日’,’月’,’火’,’水’,’木’,’金’,’土’] list2 = list1[::2] list3 = list1[1::2] p …