강의

멘토링

커뮤니티

Inflearn Community Q&A

kimkim0316534's profile image
kimkim0316534

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

for 문에서의 list 값 질문있습니다!!!

Written on

·

159

0

 
test=[0,0,0,0]
for i in range(4):
test2=test
test2[i]=1
print(test2)

 

 
다음과 같이 for문을 돌릴때마다 test2값을 test 리스르로 초기화 하면
제가 생각하는 예상값은
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]
 
인데
 
실제 결과값은 test2값을 test로 초기화하지 못하고
[1, 0, 0, 0]
[1, 1, 0, 0]
[1, 1, 1, 0]
[1, 1, 1, 1]
 
으로 나오는데.. 이것은 뭐때문에 그런건가요??
확인해보니 test값도 동일하게 변수값이 들어가네요 ㅠㅠ
 
그렇다고 append 함수를 써서 test값을 유지한다고 하면
 
test=[0,0,0,0]
for i in range(4):
test2.append(test)
test2[i]=1
print(test2)
 
결과값이
[1] [1, 1] [1, 1, 1] [1, 1, 1, 1]
처럼 나오네요.. 원하는 결과값은 얻는 방법이 무엇이고, 왜 이런 결과가 나오는지 알고싶어요

 
 
 
 
 
python코테 준비 같이 해요!

Answer

This question is waiting for answers
Be the first to answer!
kimkim0316534's profile image
kimkim0316534

asked

Ask a question