python中2d_【IT专家】如何在Python中复制一个2D数组?(复制)
本文由我司收集整編,推薦下載,如有疑問,請與我司聯系
如何在
Python
中復制一個
2D
數組?
(
復制
)
如何在
Python
中復制一個
2D
數組
?(
復制
)[
英
]How?to?make?a?copy?of?a?2D?array
in?Python??[duplicate]X?is?a?2D?array.?I?want?to?have?a?new?variable?Y?that?which?has?the
same
value
as
the
array
X.
Moreover,
any
further
manipulations
with
Y
should
not
influence?the?value?of?the?X.
X
是一個二維數組。我想要一個新的變量
Y
它的值和數組
X
相同,而且,任何對
Y
的進一步操作都不應該影響
X
的值。
It?seems?to?me?so?natural?to?use?y?=?x.?But?it?does?not?work?with?arrays.?If?I?do?it?this
way?and?then?changes?y,?the?x?will?be?changed?too.?I?found?out?that?the?problem?can?be
solved?like?that:?y?=?x[:]
在我看來,使用
y
=
x
是很自然的事,但它不適用于數組。如果我這樣做,然后
改變
y,?x
也會改變。我發現問題可以這樣解決
:y?=?x[:]
But?it?does?not?work?with?2D?array.?For?example:
但它不能處理
2D
數組。例如
:
x?=?[[1,2],[3,4]]y?=?x[:]y[0][0]=?1000print?x?returns?[?[1000,?2],?[3,?4]?].?It?also?does?not
help?if?I?replace?y=x[:]?by?y?=?x[:][:].
返回
[[1000,2]
,
[3,4]]
。如果我把
y=x[:]
換成
y=x[:]
也沒有用。
Does?anybody?know?what?is?a?proper?and?simple?way?to?do?it?
有人知道什么是合適的簡單方法嗎
?
I’m?not?sure,?maybe?copy()?is?sufficient.
我不確定,也許拷貝
()
就足夠了。
51
Using?deepcopy()?or?copy()?is?a?good?solution.?For?a?simple?2D-array?case
使用
deepcopy()
或
copy()
是一個很好的解決方案。對于一個簡單的
2d
數組情況
y?=?[row[:]?for?row?in?x]?7
For?2D?arrays?it’s?possible?use?map?function:
總結
以上是生活随笔為你收集整理的python中2d_【IT专家】如何在Python中复制一个2D数组?(复制)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python b64encode_Pyt
- 下一篇: supervisor python_使用