Learning Python: List Copy

Python can be very easy to use slices to copy the list, but there is a misunderstanding is easy to beginners to make mistakes, that slice copy list.

book = ['a','b','c','d']
#将book列表中的a、b、c复制到itbook中
itBook = book[0:2]
print(book)
print(itBook)

#将book关联到itbook1,两者指向同一个列表
itbook1 = book
book.append('e')
print(book)
print(itbook1)

#输出
['a', 'b', 'c', 'd']
['a', 'b']
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e']

 

Published 22 original articles · won praise 3 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41623154/article/details/82427389