0
사용자로부터 입력을 받아 사전 목록을 초기화하고 싶습니다. 잘 작동하는 다음 블록이 있습니다.사전 목록 초기화
people = []
for p in range(3):
cell = {"name": "","age" : 0, "education" : "","height" : 0}
cell["name"] = input("name:")
cell["age"] = int(input("age:"))
cell["education"] = input("education:")
cell["height"] = float(input("height:"))
people.append(cell)
다음과 같은 이유 때문에 다음 블록이 작동하지 않습니다. 내가이 줄을 사용할 때 반복의 끝에서 나는 모든 3 번, 내 말은 최신의 입력으로 초기화 목록을 왜
people = []
cell = {"name": "","age" : 0, "education" : "","height" : 0}
for p in range(0,3):
cell["name"] = input("name:")
cell["age"] = int(input("age:"))
cell["education"] = input("education:")
cell["height"] = float(input("height:"))
people.append(cell)
이해가 안 :
cell["name"] = input("name:")
은 안 이전 값이 새 값으로 대체 되었습니까?
여기cell = {"name": "","age" : 0, "education" : "","height" : 0}
당신이 사전을 만들고 두 번째 예에서와 같이 (루프 전에이 작업을 수행하는 경우 cell
아래에 대한 참조를 저장 :
같은'cell' 개체를 수정하고'people' 목록에 추가하십시오. –
밀접하게 관련된 참고 사항 : http : // stackoverflow.com/questions/240178/python-list-of-lists-changes-across-sublists-unexpectedly –