Hướng dẫn copy 2d list python - sao chép danh sách 2d python

Tôi có 2 danh sách:

list1 = [[1,2,3],[4,5,6],[7,8,9]]
list2 = list1.copy[]

Sau đó, tôi muốn trao đổi các phần tử trong list_2 bằng function hoán đổi fap_elements []. Tôi đã cố gắng trao đổi số 1 và 9:

def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list

list2 = swap_elements[list1,0,0,2,2]

Sau khi in cả hai danh sách, chúng ta có thể thấy rằng các yếu tố trong list_1 cũng hoán đổi. Tại sao?

print[list1]
print[list2]

đưa ra đầu ra:

[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]

Python sao chép danh sách 2D với các ví dụ mã

Xin chào mọi người, trong bài đăng này, chúng tôi sẽ xem xét cách giải quyết Python sao chép danh sách 2D trong lập trình.

y = [row[:] for row in x]

Chúng tôi đã học cách giải quyết bản sao Python một danh sách 2D bằng cách xem xét một loạt các trường hợp khác nhau.

Làm thế nào để bạn nhân đôi danh sách 2D trong Python?

Tôi muốn sao chép danh sách 2D, để nếu tôi sửa đổi một danh sách, thì bản kia không được sửa đổi. Bạn có thể sử dụng:

  • B = Sao chép. DeepCopy [a]
  • B = [Mục [:] cho mục trong A]
  • B = [Mục. Sao chép [] cho mục trong một]
  • b = [list [mục] cho mục trong a]
  • B = [Sao chép.
  • b = []; b.

Bạn có thể sao chép một danh sách trong Python không?

Sao chép một danh sách. Bạn không thể sao chép danh sách chỉ bằng cách nhập List2 = List1, vì: List2 sẽ chỉ là một tham chiếu đến List1 và các thay đổi được thực hiện trong List1 cũng sẽ tự động được thực hiện trong List2. Có nhiều cách để tạo một bản sao, một cách là sử dụng Phương thức Danh sách tích hợp [].

Làm cách nào để sao chép danh sách vào danh sách trong Python?

Để thực sự sao chép danh sách, bạn có một số tùy chọn:

  • Bạn có thể sử dụng phương thức Buildin Dist.Copy [] [có sẵn từ Python 3.3]: new_list = old_list.copy []
  • Bạn có thể cắt nó: new_list = old_list [:]
  • Bạn có thể sử dụng hàm tạo Danh sách [] tích hợp: new_list = list [old_list]

Làm cách nào để sao chép danh sách các đối tượng trong Python?

Có năm cách chính để sao chép danh sách trong Python:

  • Phương thức sao chép [].
  • [:] Toán tử cắt.
  • danh sách [] hàm.
  • sao chép. Sao chép [] hàm.
  • sao chép. Hàm DeepCopy [].

Có danh sách 2D trong Python không?

Python cung cấp nhiều cách để tạo danh sách/mảng 2 chiều.13-SEPT-2022

Làm thế nào để bạn tạo một bản sao nông của một danh sách?

Câu trả lời ngắn:

  • Để tạo một bản sao nông của danh sách danh sách, hãy sử dụng danh sách. Phương thức Copy [] tạo một danh sách bên ngoài mới với các tham chiếu được sao chép vào cùng một danh sách bên trong.
  • Để tạo một bản sao sâu với Danh sách bên trong được sao chép, hãy nhập thư viện sao chép và Call Copy. DeepCopy [danh sách] để sao chép cả danh sách bên trong và bên ngoài.

Làm thế nào để bạn sao chép một danh sách?

Các cách khác nhau để sao chép danh sách:

  • Sử dụng kỹ thuật cắt.
  • Sử dụng phương thức mở rộng [].
  • Danh sách sao chép bằng cách sử dụng = [toán tử gán]
  • Sử dụng phương pháp sao chép nông.
  • Sử dụng danh sách hiểu.
  • Sử dụng phương thức append [].
  • Sử dụng phương thức Copy [].
  • Sử dụng phương pháp sao chép sâu.

Làm thế nào để bạn sao chép một danh sách mà không thay đổi Python ban đầu?

Sao chép [] Trả về Giá trị Phương thức COPY [] Trả về một danh sách mới. Nó không sửa đổi danh sách ban đầu.

Làm cách nào để sao chép danh sách này sang danh sách khác?

Một cách tiếp cận khác để sao chép các thành phần là sử dụng phương thức addall: list copy = new ArrayList []; sao chép. addall [danh sách]; Điều quan trọng là phải ghi nhớ bất cứ khi nào sử dụng phương pháp này, như với hàm tạo, nội dung của cả hai danh sách sẽ tham chiếu cùng một đối tượng.11-NOV-2021

Tại sao COPY [] không hoạt động Python?

Danh sách Python sao chép không hoạt động. Lý do chính tại sao danh sách. Phương thức Copy [] có thể không hoạt động cho bạn là vì bạn cho rằng nó tạo ra một bản sao sâu khi trong thực tế, nó chỉ tạo ra một bản sao nông của danh sách.

Trong bài viết trước, chúng ta đã thấy cách sao chép hoặc sao chép một danh sách, bây giờ hãy để xem cách sao chép một danh sách lồng nhau trong Python.

Phương pháp số 1: Sử dụng phép lặp

Input_list

def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
0
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
1
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
2
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
3
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
4
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
3
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
6
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
7
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
8
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
3__

print[list1]
print[list2]
4
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
0
print[list1]
print[list2]
6

print[list1]
print[list2]
7
print[list1]
print[list2]
8
print[list1]
print[list2]
9
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
0
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
1
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
2
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
3

[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
4
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
5
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
0
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
7

[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
4
print[list1]
print[list2]
7
y = [row[:] for row in x]
0
print[list1]
print[list2]
9
y = [row[:] for row in x]
2

y = [row[:] for row in x]
3
y = [row[:] for row in x]
4

[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
4
y = [row[:] for row in x]
6

y = [row[:] for row in x]
7
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
1
y = [row[:] for row in x]
9
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
0

y = [row[:] for row in x]
7
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
2

y = [row[:] for row in x]
7
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
1
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
5
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
0

y = [row[:] for row in x]
7
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
8

Output:

Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]

& nbsp; Phương pháp #2: Sử dụng DeepCopy
Method #2: Using deepcopy

Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
9
Initial list is:
[[1, 0, 1], [1, 0, 1]]
New assigned list is
[[1, 0, 1], [1, 0, 1]]
0

Initial list is:
[[1, 0, 1], [1, 0, 1]]
New assigned list is
[[1, 0, 1], [1, 0, 1]]
1
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
0
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
1
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
4
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
3__12

print[list1]
print[list2]
4
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
0
Initial list is:
[[0, 1, 2], [3, 4, 5], [0, 1, 8]]
New assigned list is
[[0, 1, 2], [3, 4, 5], [0, 1, 8]]
8
Initial list is:
[[1, 0, 1], [1, 0, 1]]
New assigned list is
[[1, 0, 1], [1, 0, 1]]
1
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
0

y = [row[:] for row in x]
7
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
1
y = [row[:] for row in x]
9
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
0

y = [row[:] for row in x]
7
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
1
Initial list is:
[[1, 0, 1], [1, 0, 1]]
New assigned list is
[[1, 0, 1], [1, 0, 1]]
1
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
0

y = [row[:] for row in x]
7
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
1
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
5
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
0

y = [row[:] for row in x]
7
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
8

Output:

Initial list is:
[[1, 0, 1], [1, 0, 1]]
New assigned list is
[[1, 0, 1], [1, 0, 1]]

& nbsp; Phương pháp #3: Sử dụng danh sách hiểu và cắt
Method #3: Using list comprehension and slicing

Input_list

def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
0
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
1
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
2Input_list 9
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
4Input_list 9
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
6__

def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
16
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
0
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
18
print[list1]
print[list2]
7
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
20
print[list1]
print[list2]
9
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
22

y = [row[:] for row in x]
7
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
1
y = [row[:] for row in x]
9
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
0

y = [row[:] for row in x]
7
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
2

y = [row[:] for row in x]
7
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
[[9, 2, 3], [4, 5, 6], [7, 8, 1]]
1
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
5
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
0

y = [row[:] for row in x]
7
def swap_elements[old_list,x1,y1,x2,y2]:
  new_list = old_list.copy[]
  num1 = new_list[x1][y1]
  num2 = new_list[x2][y2]
  new_list[x1][y1] = num2
  new_list[x2][y2] = num1
  return new_list
34

Output:

Initial list is:
[[0, 1, 2], [3, 4, 5], [0, 1, 8]]
New assigned list is
[[0, 1, 2], [3, 4, 5], [0, 1, 8]]


Bài Viết Liên Quan

Chủ Đề