私はPythonが初めてで、.formkeys()メソッドをリストで使用することに混乱しています。
以下は私のコードです:
# dictionary exercise 4: Initialize dictionary with default values
employees = ['Kelly', 'Emma', 'John']
defaults = {"designation": 'Application Developer', "salary": 8000}
def_dictionary = dict()
def_dictionary.setdefault("designation", "Application Developer")
def_dictionary.setdefault("salary", 8000)
print(def_dictionary)
res_dict = dict.fromkeys(employees[0], defaults)
print(res_dict)
print(res_dict)
ここで、出力は
{'K': {'designation': 'Application Developer', 'salary': 8000}, 'e': {'designation': 'Application Developer', 'salary': 8000}, 'l': {'designation': 'Application Developer', 'salary': 8000}, 'y': {'designation': 'Application Developer', 'salary': 8000}}
私がやりたいのは、従業員「Kelly」とデフォルト値の辞書をペアにすることですが、「K」、「E」、「L」、「Y」の文字列を res_dict のキーとして取得する理由がわかりません。
私は解決策があるべきであることを知っています
res_dict = dict.fromkeys(employees, defaults)
コードがケリーを「K」、「E」、「L」、「Y」に解析する理由が不思議です。
ありがとうございました