Hướng dẫn can we assign a list to another list in python? - chúng ta có thể gán một danh sách cho một danh sách khác trong python không?

Trong Python, mọi thứ đều là một đối tượng, và điều đó có nghĩa là mọi thứ đều có được bộ nhớ của riêng nó.

Show

Khi bạn khởi tạo

listOne is listTwo
#Output: True
3, nó đã được cung cấp một địa chỉ bộ nhớ.

Sau đó, bạn đã sử dụng toán tử gán

listOne is listTwo
#Output: True
4 để gán vị trí bộ nhớ của
listOne is listTwo
#Output: True
5 cho
listOne is listTwo
#Output: True
6.

Vì vậy, nếu chúng tôi lấy ví dụ của bạn:

listOne = [1, 2, 3]
listTwo = listOne

Chúng tôi có thể làm:

listOne is listTwo
#Output: True

Ai đó đã đề cập rằng bạn có thể sử dụng toán tử lát cắt

listOne is listTwo
#Output: True
7 nhưng điều đó cung cấp cho bạn một bản sao nông. Nếu bạn không quen thuộc với sự khác biệt giữa bản sao nông và sâu, hãy đọc các tài liệu này. Về cơ bản, một bản sao nông là một đối tượng container bên ngoài mới (
listOne is listTwo
#Output: True
8 và các phần tử bên trong là các tham chiếu đến cùng một vị trí bộ nhớ. Một bản sao sâu cũng là một container bên ngoài mới nhưng có các bản sao hoàn toàn mới của các đối tượng bên trong (nghĩa là các bản sao giống hệt nhau của các đối tượng nhưng tại các vị trí bộ nhớ mới).

Tôi để lại cho bạn ví dụ này về một bản sao nông với các loại không có tiền giả (tức là không phải ____ 19 như của bạn):

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'

10. Sử dụng chức năng bản đồ copy lists in Python. These various ways of copying list take different execution times, so we can compare them on the basis of time. 

>>> class Sample: def __init__(self, num): self.num = num self.id = id(self) >>> s1 = Sample(1) >>> s2 = Sample(2) >>> listOne = [s1, s2] >>> listOne[0].num 1 >>> listOne[0].id 88131696 >>> listOne[1].num 2 >>> listOne[1].id 92813904 # so far we know that listOne contains these unique objects >>> listTwo = listOne >>> listTwo[0].num 1 >>> listTwo[0].id 88131696 >>> listTwo[1].num 2 >>> listTwo[1].id 92813904 # well shoot, the two lists have the same objects! # another way to do show that is to simply print the lists >>> listOne [<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>] >>> listTwo [<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>] # those odd number sequences are hexadecimal and represent the memory location # to prove the point further, you can use the built in hex() with id() >>> hex(listOne[0].id) # id is 88131696 from above '0x540c870' 85listOne is listTwo #Output: True 4 Original List: [4, 8, 2, 10, 15, 18]  After Cloning: [4, 8, 2, 10, 15, 18] 1Original List: [4, 8, 2, 10, 15, 18]  After Cloning: [4, 8, 2, 10, 15, 18] 2Original List: [4, 8, 2, 10, 15, 18]  After Cloning: [4, 8, 2, 10, 15, 18] 3Original List: [4, 8, 2, 10, 15, 18]  After Cloning: [4, 8, 2, 10, 15, 18] 4Original List: [4, 8, 2, 10, 15, 18]  After Cloning: [4, 8, 2, 10, 15, 18] 3Original List: [4, 8, 2, 10, 15, 18]  After Cloning: [4, 8, 2, 10, 15, 18] 6Original List: [4, 8, 2, 10, 15, 18]  After Cloning: [4, 8, 2, 10, 15, 18] 3Original List: [4, 8, 2, 10, 15, 18]  After Cloning: [4, 8, 2, 10, 15, 18] 8Original List: [4, 8, 2, 10, 15, 18]  After Cloning: [4, 8, 2, 10, 15, 18] 3Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18]0Original List: [4, 8, 2, 10, 15, 18]  After Cloning: [4, 8, 2, 10, 15, 18] 3Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18]2Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18]3

  • >>> class Sample:
            def __init__(self, num):
                self.num = num
                self.id = id(self)
    
    >>> s1 = Sample(1)
    >>> s2 = Sample(2)
    
    >>> listOne = [s1, s2]
    >>> listOne[0].num
    1
    >>> listOne[0].id
    88131696
    >>> listOne[1].num
    2
    >>> listOne[1].id
    92813904
    
    # so far we know that listOne contains these unique objects
    
    >>> listTwo = listOne
    >>> listTwo[0].num
    1
    >>> listTwo[0].id
    88131696
    >>> listTwo[1].num
    2
    >>> listTwo[1].id
    92813904
    
    # well shoot, the two lists have the same objects!
    # another way to do show that is to simply print the lists
    
    >>> listOne
    [<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
    >>> listTwo
    [<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
    
    # those odd number sequences are hexadecimal and represent the memory location
    # to prove the point further, you can use the built in hex() with id()
    
    >>> hex(listOne[0].id) # id is 88131696 from above
    '0x540c870'
    
    3
    listOne is listTwo
    #Output: True
    
    4
    Original List: [4, 8, 2, 10, 15, 18] 
    After Cloning: [4, 8, 2, 10, 15, 18] 
    33
    Original List: [4, 8, 2, 10, 15, 18]
    After Cloning: [4, 8, 2, 10, 15, 18]
    8
    listOne is listTwo
    #Output: True
    
    9
    Original List: [4, 8, 2, 10, 15, 18] 
    After Cloning: [4, 8, 2, 10, 15, 18] 
    36
  • Original List: [4, 8, 2, 10, 15, 18]
    After Cloning: [4, 8, 2, 10, 15, 18]
    7
    Original List: [4, 8, 2, 10, 15, 18]
    After Cloning: [4, 8, 2, 10, 15, 18]
    8
    Original List: [4, 8, 2, 10, 15, 18]
    After Cloning: [4, 8, 2, 10, 15, 18]
    9
    Original List: [4, 8, 2, 10, 15, 18] 
    After Cloning: [4, 8, 2, 10, 15, 18] 
    36
  • Trong bài viết này, chúng tôi sẽ trải qua nhiều cách khác nhau của danh sách sao chép trong Python. Những cách khác nhau để sao chép danh sách có thời gian thực hiện khác nhau, vì vậy chúng ta có thể so sánh chúng trên cơ sở thời gian. & NBSP;
  • Các cách khác nhau để sao chép danh sách:
  • Sử dụng kỹ thuật cắt & nbsp;
  • Sử dụng phương thức mở rộng () & nbsp;
  • Danh sách sao chép bằng cách sử dụng = (toán tử gán)
  • Sử dụng phương thức sao chép nông & nbsp;
  • Sử dụng danh sách hiểu & nbsp;

Sử dụng phương thức append () & nbsp;

Sử dụng phương thức Copy () & nbsp;

Sử dụng phương pháp sao chép sâu

Example:

Python3

Sử dụng phương thức bản đồ & nbsp;

1. Sử dụng kỹ thuật cắt & nbsp;

Đây là cách dễ nhất và nhanh nhất để sao chép danh sách. Phương pháp này được xem xét khi chúng tôi muốn sửa đổi một danh sách và cũng giữ một bản sao của bản gốc. Trong đó, chúng tôi tạo một bản sao của danh sách, cùng với tài liệu tham khảo. Quá trình này cũng được gọi là nhân bản. Kỹ thuật này mất khoảng 0,039 giây và là kỹ thuật nhanh nhất. & NBSP;

Để biết thêm thông tin, bạn có thể đọc bài viết này - Danh sách kỹ thuật cắt lát

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
0
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
1

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
9
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
0

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4

Output: 

Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 

>>> class Sample: def __init__(self, num): self.num = num self.id = id(self) >>> s1 = Sample(1) >>> s2 = Sample(2) >>> listOne = [s1, s2] >>> listOne[0].num 1 >>> listOne[0].id 88131696 >>> listOne[1].num 2 >>> listOne[1].id 92813904 # so far we know that listOne contains these unique objects >>> listTwo = listOne >>> listTwo[0].num 1 >>> listTwo[0].id 88131696 >>> listTwo[1].num 2 >>> listTwo[1].id 92813904 # well shoot, the two lists have the same objects! # another way to do show that is to simply print the lists >>> listOne [<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>] >>> listTwo [<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>] # those odd number sequences are hexadecimal and represent the memory location # to prove the point further, you can use the built in hex() with id() >>> hex(listOne[0].id) # id is 88131696 from above '0x540c870' 2>>> class Sample: def __init__(self, num): self.num = num self.id = id(self) >>> s1 = Sample(1) >>> s2 = Sample(2) >>> listOne = [s1, s2] >>> listOne[0].num 1 >>> listOne[0].id 88131696 >>> listOne[1].num 2 >>> listOne[1].id 92813904 # so far we know that listOne contains these unique objects >>> listTwo = listOne >>> listTwo[0].num 1 >>> listTwo[0].id 88131696 >>> listTwo[1].num 2 >>> listTwo[1].id 92813904 # well shoot, the two lists have the same objects! # another way to do show that is to simply print the lists >>> listOne [<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>] >>> listTwo [<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>] # those odd number sequences are hexadecimal and represent the memory location # to prove the point further, you can use the built in hex() with id() >>> hex(listOne[0].id) # id is 88131696 from above '0x540c870' 3listOne is listTwo #Output: True 4 >>> class Sample: def __init__(self, num): self.num = num self.id = id(self) >>> s1 = Sample(1) >>> s2 = Sample(2) >>> listOne = [s1, s2] >>> listOne[0].num 1 >>> listOne[0].id 88131696 >>> listOne[1].num 2 >>> listOne[1].id 92813904 # so far we know that listOne contains these unique objects >>> listTwo = listOne >>> listTwo[0].num 1 >>> listTwo[0].id 88131696 >>> listTwo[1].num 2 >>> listTwo[1].id 92813904 # well shoot, the two lists have the same objects! # another way to do show that is to simply print the lists >>> listOne [<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>] >>> listTwo [<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>] # those odd number sequences are hexadecimal and represent the memory location # to prove the point further, you can use the built in hex() with id() >>> hex(listOne[0].id) # id is 88131696 from above '0x540c870' 5 

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
7
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
8

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
9
listOne is listTwo
#Output: True
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
1
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
2
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
6
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
8
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
0__3333333

Example:

Python3

Sử dụng phương thức bản đồ & nbsp;

1. Sử dụng kỹ thuật cắt & nbsp;

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
[1, 2, [3, 5], 4]
2

Đây là cách dễ nhất và nhanh nhất để sao chép danh sách. Phương pháp này được xem xét khi chúng tôi muốn sửa đổi một danh sách và cũng giữ một bản sao của bản gốc. Trong đó, chúng tôi tạo một bản sao của danh sách, cùng với tài liệu tham khảo. Quá trình này cũng được gọi là nhân bản. Kỹ thuật này mất khoảng 0,039 giây và là kỹ thuật nhanh nhất. & NBSP;

Để biết thêm thông tin, bạn có thể đọc bài viết này - Danh sách kỹ thuật cắt lát

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
0
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
1

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
9
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
0

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
80
listOne is listTwo
#Output: True
4
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
82

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

9. Sử dụng chức năng liệt kê

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
85
listOne is listTwo
#Output: True
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
1
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
2
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
6
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
8
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
0
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
2
listOne is listTwo
#Output: True
15Python = operators. 

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
3
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
52
listOne is listTwo
#Output: True
53
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
04
listOne is listTwo
#Output: True
55
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
06__

Đầu ra There is an issue with this method if you modify in the new list then the old list is also modified due to the new list is referencing 

Example:

Python3

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
0
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
3
listOne is listTwo
#Output: True
4
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
7
listOne is listTwo
#Output: True
00

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
9
listOne is listTwo
#Output: True
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
1
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
2
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
6
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
8
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
0__3333333

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
18

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
9
listOne is listTwo
#Output: True
22

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4

Output: 

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

4. Sử dụng phương thức sao chép nông & nbsp; 

Phương pháp sao chép bằng cách sử dụng bản sao. Bản sao nông được giải thích rõ trong bài viết bản sao nông. Điều này mất khoảng 0,186 giây để hoàn thành. & NBSP;

Example:

Python3

listOne is listTwo
#Output: True
27
listOne is listTwo
#Output: True
28

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
9
listOne is listTwo
#Output: True
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
1
listOne is listTwo
#Output: True
32
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
6
listOne is listTwo
#Output: True
35
listOne is listTwo
#Output: True
36
listOne is listTwo
#Output: True
37
listOne is listTwo
#Output: True
38
listOne is listTwo
#Output: True
39
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
2

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
44

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
listOne is listTwo
#Output: True
46

Output:

[1, 2, [3, 5], 4]

5. Sử dụng danh sách hiểu & nbsp; 

Phương pháp hiểu danh sách có thể được sử dụng để sao chép tất cả các yếu tố riêng lẻ từ danh sách này sang danh sách khác. Điều này mất khoảng 0,217 giây để hoàn thành. & NBSP;

Example:

Python3

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
0
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
3
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
52
listOne is listTwo
#Output: True
53
listOne is listTwo
#Output: True
54
listOne is listTwo
#Output: True
55
listOne is listTwo
#Output: True
56

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
7
listOne is listTwo
#Output: True
00

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
9
listOne is listTwo
#Output: True
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
1
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
2
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
6
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
8
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
0__3333333

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
18

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
9
listOne is listTwo
#Output: True
22

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3
listOne is listTwo
#Output: True
85

Output: 

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

4. Sử dụng phương thức sao chép nông & nbsp; 

Phương pháp sao chép bằng cách sử dụng bản sao. Bản sao nông được giải thích rõ trong bài viết bản sao nông. Điều này mất khoảng 0,186 giây để hoàn thành. & NBSP;

Example:

Python3

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
0
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
3
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
91

listOne is listTwo
#Output: True
27
listOne is listTwo
#Output: True
28

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
7
listOne is listTwo
#Output: True
00

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
9
listOne is listTwo
#Output: True
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
1
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
2
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
6
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
8
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
0__3333333

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
18

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
9
listOne is listTwo
#Output: True
22

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4

Output: 

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

4. Sử dụng phương thức sao chép nông & nbsp; 

Phương pháp sao chép bằng cách sử dụng bản sao. Bản sao nông được giải thích rõ trong bài viết bản sao nông. Điều này mất khoảng 0,186 giây để hoàn thành. & NBSP;Python List copy()is an inbuilt method copy used to copy all the elements from one list to another. This takes around 1.488 seconds to complete.

Example: 

Python3

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
0
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
3
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
91

listOne is listTwo
#Output: True
27
listOne is listTwo
#Output: True
28

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
7
listOne is listTwo
#Output: True
00

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
9
listOne is listTwo
#Output: True
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
1
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
2
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
6
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
8
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
0__3333333

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
18

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
9
listOne is listTwo
#Output: True
22

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4

Output: 

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

4. Sử dụng phương thức sao chép nông & nbsp; 

Phương pháp sao chép bằng cách sử dụng bản sao. Bản sao nông được giải thích rõ trong bài viết bản sao nông. Điều này mất khoảng 0,186 giây để hoàn thành. & NBSP;

Example:

Python3

listOne is listTwo
#Output: True
27
listOne is listTwo
#Output: True
28

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
9
listOne is listTwo
#Output: True
4
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
1
listOne is listTwo
#Output: True
32
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
6
listOne is listTwo
#Output: True
35
listOne is listTwo
#Output: True
36
listOne is listTwo
#Output: True
37
listOne is listTwo
#Output: True
38
listOne is listTwo
#Output: True
39
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
2

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
4
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
44

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
84

Output:

[1, 2, [3, 5], 4]

5. Sử dụng danh sách hiểu & nbsp;

Python3

Phương pháp hiểu danh sách có thể được sử dụng để sao chép tất cả các yếu tố riêng lẻ từ danh sách này sang danh sách khác. Điều này mất khoảng 0,217 giây để hoàn thành. & NBSP;

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
3
listOne is listTwo
#Output: True
4
listOne is listTwo
#Output: True
52
listOne is listTwo
#Output: True
53
listOne is listTwo
#Output: True
54
listOne is listTwo
#Output: True
55
listOne is listTwo
#Output: True
56

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
9
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
11

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
15

6. Sử dụng phương thức append () & nbsp;

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

Điều này có thể được sử dụng để nối thêm và thêm các yếu tố vào danh sách hoặc sao chép chúng vào một danh sách mới. Nó được sử dụng để thêm các yếu tố vào vị trí cuối cùng của danh sách. Điều này mất khoảng 0,325 giây để hoàn thành và là phương pháp nhân bản chậm nhất. & NBSP;

Python3

>>> class Sample:
        def __init__(self, num):
            self.num = num
            self.id = id(self)

>>> s1 = Sample(1)
>>> s2 = Sample(2)

>>> listOne = [s1, s2]
>>> listOne[0].num
1
>>> listOne[0].id
88131696
>>> listOne[1].num
2
>>> listOne[1].id
92813904

# so far we know that listOne contains these unique objects

>>> listTwo = listOne
>>> listTwo[0].num
1
>>> listTwo[0].id
88131696
>>> listTwo[1].num
2
>>> listTwo[1].id
92813904

# well shoot, the two lists have the same objects!
# another way to do show that is to simply print the lists

>>> listOne
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]
>>> listTwo
[<__main__.Sample object at 0x0540C870>, <__main__.Sample object at 0x05883A50>]

# those odd number sequences are hexadecimal and represent the memory location
# to prove the point further, you can use the built in hex() with id()

>>> hex(listOne[0].id) # id is 88131696 from above
'0x540c870'
2
listOne is listTwo
#Output: True
53
listOne is listTwo
#Output: True
94
listOne is listTwo
#Output: True
55
listOne is listTwo
#Output: True
96

7. Sử dụng phương thức Copy () & nbsp;

Bản sao danh sách Python () là một bản sao phương thức sẵn có được sử dụng để sao chép tất cả các phần tử từ danh sách này sang danh sách khác. Điều này mất khoảng 1,488 giây để hoàn thành.

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
7
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
8
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
3
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
45
Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 
46

Output:

listOne is listTwo
#Output: True
2