여기에 여러분을위한 기본적인 질문이 있습니다. 저는 코드 작성에 상당히 새로운 사람입니다.이 코드를 보았을 때, 나는 그것을 이해할 수 없었습니다. 다음은 질문입니다. 왜 특정 루프에 profile[key] = value
입니까? 이 코드가 사전 key
을 value
으로 만들어 내 머리에서 이해가되지 않는 것 같습니다. 어떤 설명이 좋을 것입니다! 코드 :user_profile 사전 루프
def build_profile(first, last, **user_info):
"""Build a dictionary containing everything we know about a user"""
profile = {}
profile["first_name"] = first
profile["last_name"] = last
for key, value in user_info.items():
profile[key] = value # Why is this converting the key of the dictionary into a value?
return profile
user_profile = build_profile("albert", "einstein",
location="princeton",
field="physics")
print(user_profile)
P.S. 이것은 "Python Crash Course"의 153 페이지에 있습니다. 설명을 주었지만 이해가되지 않습니다. 죄송합니다.
오, 와우, 방금 코드를 훨씬 더 깨끗하게 만들었습니다. 감사합니다! 나는 "profile [key]"를 변수로 보았을 것입니다. profile [key]는 'value'가 위치하는 'box'입니다. 그러나 여러분이 "profile [ key] 변수 "value"를 보유하고 있지 않지만 사전 구문의 일부일뿐입니다. 맞습니까? – User31899
맞습니다. –