python - 将一个多维列表的值设置为另一个多维列表的值
问题描述
我在 Python 3.9 中有一个包含多个字符串的多维列表。现在我想[["a", "b"], ["c", "d"]]
成为[["d", "a"], ["b", "c"]]
。所以每个字符串都向上移动一个位置。这是我尝试过的代码:
list = [["a", "b"], ["c", "d"]]
helper = list
list[0][0] = helper[1][1]
list[0][1] = helper[0][0]
list[1][0] = helper[0][1]
list[1][1] = helper[1][0]
print(list)
但不幸的是,代码给了我[['d', 'd'], ['d', 'd']]
而不是[["d", "a"], ["b", "c"]]
. helper
在我的程序结束时打印出来[['d', 'd'], ['d', 'd']]
也给了我,但为什么呢?助手不应该是[["a", "b"], ["c", "d"]]
因为我将他设置list
为我的程序的顶部。
list = [["a", "b"], ["c", "d"]]
helper = list
但是,以下代码证明在我的程序期间助手正在更改:
list = [["a", "b"], ["c", "d"]]
helper = list
print("Top of program:", helper)
list[0][0] = helper[1][1]
list[0][1] = helper[0][0]
list[1][0] = helper[0][1]
list[1][1] = helper[1][0]
print("Bottom of program:", helper)
输出:
Top of program: [['a', 'b'], ['c', 'd']]
Bottom of program: [['d', 'd'], ['d', 'd']]
所以我的问题是为什么helper
改变它的值以及为什么我的程序没有做它应该做的事情。
解决方案
推荐阅读
- java - 将 ASCII 值写入 RS485 等串行通信设备的寄存器
- javascript - 为什么字符串文字在 JavaScript 中被视为原始类型?
- macos - 如何在 MAC 上永久更改 JAVA_HOME 目录?
- angular - 在这些情况下是否有必要订阅和删除 observables?
- reporting-services - How to write SSRS 2017 expression for calculating percentages when report contains Row Groups level hierarchy data
- python - PyCharm 提示“foo”可以是未定义的,但是如何定义呢?
- amazon-web-services - 是否可以将两个 RoR 5 网站连接到一个亚马逊 RDS 数据库,每月费用
- reactjs - 反应本机 Redux useDispatch 不起作用
- python - 静态方法返回无结果?
- r - 时间序列/Tidyverse:微积分取决于给定组中的所有先前行