왜 python print [...]

아니스 헤잠

누군가가 왜 내 파이썬 코드가 이것을 인쇄하는지 설명 할 수 있습니까? [...] 그것은 무엇을 의미합니까? 감사합니다

내 코드 :

def foo(x,y): 
    x[0]= y[1]
    x,y=y,x
    return x+y
z=[1,2]
z2=[z,z]
t=foo(z,z2)
print(z)
print(z2)
print(t)
shaktimaan

이것은 다음을 수행 할 때 z목록이 [0]위치에서 자체를 참조 하기 때문입니다 .

def foo(x,y): 
    x[0]= y[1]
    x,y=y,x
    return x+y
z=[1,2]
#Here you have created a list containing 1,2 
z2=[z,z]
#here is not creating two lists in itself, but it is referencing z itself(sort of like pointer), you can verify this by:
In [21]: id(z2[0])
Out[21]: 57909496
In [22]: id(z2[1])
Out[22]: 57909496
#see here both the location have same objects
t=foo(z,z2)
#so when you call foo and do x[0]= y[1], what actually happens is  
# z[0] = z2[0] , which is z itself
# which sort of creates a recursive list
print(z)
print(z2)
print(t)
#you can test this by
In [17]: z[0]
Out[17]: [[...], 2]
In [18]: z[0][0]
Out[18]: [[...], 2]
In [19]: z[0][0][0]
Out[19]: [[...], 2]
In [20]: z[0][0][0][0]
Out[20]: [[...], 2]
#you can carry on forever like this, but since it is referencing itself, it wont see end of it

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Lua에서 왜`local print = print`입니까?

분류에서Dev

왜 print ( 'a'> 'b') False이고 print ( 'a'> 'A') True입니까?

분류에서Dev

왜 //////////////////////////////////// ...

분류에서Dev

Python 2.7 print strings

분류에서Dev

Python if / elif 구문 오류 ... 왜

분류에서Dev

regex (vim) for print ... to print (...) for python2 to python3

분류에서Dev

왜 print ( "\ 0007")가 신호음을 내지 않습니까?

분류에서Dev

Python can't print as a string

분류에서Dev

왜 System.out.print 출력 (삼항 연산자) 인쇄 플로트?

분류에서Dev

왜 print와 f-string은 다른 시간에 평가를 수행합니까?

분류에서Dev

Python 3 execution order quirk with print?

분류에서Dev

Python : print () 내부의 for 루프

분류에서Dev

Python : print () 문의 % 연산자

분류에서Dev

Print float (x,y) data in a graph with Python

분류에서Dev

python check if item exist then print item

분류에서Dev

python loop only print values once

분류에서Dev

Coding in Python, print command does not act as expected

분류에서Dev

python print dictionary tuples in order grid format

분류에서Dev

Python does not show result of print statements in class

분류에서Dev

print tag value from xml using python

분류에서Dev

How to print out an answer in decimal using python?

분류에서Dev

클래스의 인스턴스를 만들지 않았는데 왜 클래스에서 print (x)합니까?

분류에서Dev

왜 max_n이 base :: print 문서에 문서화되어 있지 않습니까?

분류에서Dev

C 프로그래밍-왜 입력이 print 문 앞에 먼저 있는지 확실하지 않습니다.

분류에서Dev

왜 '안녕하세요'에코가 | awk '/ hello \ s / {print $ 0}'아무것도 생성하지 않습니까?

분류에서Dev

NullPointerException 왜?

분류에서Dev

python 왜 객체 속성이 서로 누출됩니까?

분류에서Dev

Python에서 언제 / 왜 함수를 반환해야합니까?

분류에서Dev

OpenCV Python에서 OCR에 대한 90도 왜곡 보정

Related 관련 기사

  1. 1

    Lua에서 왜`local print = print`입니까?

  2. 2

    왜 print ( 'a'> 'b') False이고 print ( 'a'> 'A') True입니까?

  3. 3

    왜 //////////////////////////////////// ...

  4. 4

    Python 2.7 print strings

  5. 5

    Python if / elif 구문 오류 ... 왜

  6. 6

    regex (vim) for print ... to print (...) for python2 to python3

  7. 7

    왜 print ( "\ 0007")가 신호음을 내지 않습니까?

  8. 8

    Python can't print as a string

  9. 9

    왜 System.out.print 출력 (삼항 연산자) 인쇄 플로트?

  10. 10

    왜 print와 f-string은 다른 시간에 평가를 수행합니까?

  11. 11

    Python 3 execution order quirk with print?

  12. 12

    Python : print () 내부의 for 루프

  13. 13

    Python : print () 문의 % 연산자

  14. 14

    Print float (x,y) data in a graph with Python

  15. 15

    python check if item exist then print item

  16. 16

    python loop only print values once

  17. 17

    Coding in Python, print command does not act as expected

  18. 18

    python print dictionary tuples in order grid format

  19. 19

    Python does not show result of print statements in class

  20. 20

    print tag value from xml using python

  21. 21

    How to print out an answer in decimal using python?

  22. 22

    클래스의 인스턴스를 만들지 않았는데 왜 클래스에서 print (x)합니까?

  23. 23

    왜 max_n이 base :: print 문서에 문서화되어 있지 않습니까?

  24. 24

    C 프로그래밍-왜 입력이 print 문 앞에 먼저 있는지 확실하지 않습니다.

  25. 25

    왜 '안녕하세요'에코가 | awk '/ hello \ s / {print $ 0}'아무것도 생성하지 않습니까?

  26. 26

    NullPointerException 왜?

  27. 27

    python 왜 객체 속성이 서로 누출됩니까?

  28. 28

    Python에서 언제 / 왜 함수를 반환해야합니까?

  29. 29

    OpenCV Python에서 OCR에 대한 90도 왜곡 보정

뜨겁다태그

보관