简体   繁体   English

在Python中附加到多维数组

[英]Appending to a multi-dimensional array in Python

I have this code: 我有以下代码:

philips_trousers = []
for i in range(0, 5):
  philips_trousers.append(["T"] * 5)
  philips_trousers.append(["R"] * 5)
  philips_trousers.append(["O"] * 5)
  philips_trousers.append(["U"] * 5)
  philips_trousers.append(["S"] * 5)
  philips_trousers.append(["E"] * 5)
  philips_trousers.append(["R"] * 5)
  philips_trousers.append(["S"] * 5)
print philips_trousers

Which outputs the following: 输出以下内容:

[['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'], ['O', 'O', 'O', 'O', 'O'], ['U', 'U', 'U', 'U', 'U'], ['S', 'S', 'S', 'S', 'S'], ['E', 'E', 'E', 'E', 'E'], ['R', 'R', 'R', 'R', 'R'], ['S', 'S', 'S', 'S', 'S'], ['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'], ['O', 'O', 'O', 'O', 'O'], ['U', 'U', 'U', 'U', 'U'], ['S', 'S', 'S', 'S', 'S'], ['E', 'E', 'E', 'E', 'E'], ['R', 'R', 'R', 'R', 'R'], ['S', 'S', 'S', 'S', 'S'], ['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'], ['O', 'O', 'O', 'O', 'O'], ['U', 'U', 'U', 'U', 'U'], ['S', 'S', 'S', 'S', 'S'], ['E', 'E', 'E', 'E', 'E'], ['R', 'R', 'R', 'R', 'R'], ['S', 'S', 'S', 'S', 'S'], ['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'], ['O', 'O', 'O', 'O', 'O'], ['U', 'U', 'U', 'U', 'U'], ['S', 'S', 'S', 'S', 'S'], ['E', 'E', 'E', 'E', 'E'], ['R', 'R', 'R', 'R', 'R'], ['S', 'S', 'S', 'S', 'S'], ['T', 'T', 'T', 'T', 'T'], ['R', 'R', 'R', 'R', 'R'], ['O', 'O', 'O', 'O', 'O'], ['U', 'U', 'U', 'U', 'U'], ['S', 'S', 'S', 'S', 'S'], ['E', 'E', 'E', 'E', 'E'], ['R', 'R', 'R', 'R', 'R'], ['S', 'S', 'S', 'S', 'S']]

So far so good you're probably thinking, but I'm wondering something -- why does Python fill philips_trousers in quite the way it does? 到目前为止,您可能一直在思考,但是我想知道一些事情philips_trousers为什么以这种方式填充philips_trousers That is, why does the append() function create new child arrays with the form ['T', 'T', 'T', 'T', 'T'] , ['R', 'R', 'R', 'R', 'R'] etc, rather than: ['T', 'R', 'O', 'U', 'S', 'E', 'R', 'S'] ? 也就是说,为什么append()函数以['T', 'T', 'T', 'T', 'T']['R', 'R', 'R', 'R', 'R']等,而不是: ['T', 'R', 'O', 'U', 'S', 'E', 'R', 'S']

["T"] + ["T"] is the same thing as ["T", "T"] . ["T"] + ["T"]["T", "T"] Multiplication is just addition several times. 乘法只是几次加法。 Therefore, ["T"] * 5 is the same thing as ["T", "T", "T", "T", "T"] 1 . 因此, ["T"] * 5["T", "T", "T", "T", "T"] 1相同 What you see in your end list is several of such lists. 您在结束列表中看到的是几个这样的列表。 If you want a bunch of ["T", "R", "O", ...] , you can do this: 如果要一堆["T", "R", "O", ...] ,可以执行以下操作:

philips_trousers = []

for _ in range(5):
    philips_trousers.append(list("TROUSERS"))

print philips_trousers

which is pretty much the shorter version of Remuze's answer. 这几乎是Remuze答案的简短版本。


1 Don't use list multiplication with mutable objects because each item in the list will be the same object . 1请勿对可变对象使用列表乘法,因为列表中的每个项目都是相同的对象 That means that modifying one will modify them all. 这意味着修改一个将全部修改它们。

["T"] * 5 first gets converted into [['T'],['T'],['T'],['T'],['T']] , which is then appended on to the empty array. ["T"] * 5首先被转换为[['T'],['T'],['T'],['T'],['T']] ,然后将其附加到空数组。 The same then happens for each of the other letters. 然后,其他每个字母都发生相同的情况。

To get the functionality you are looking for, you should try this: 要获得所需的功能,请尝试以下操作:

philipsTrousers = []
for i in range(0, 5):
  philipsTrousers.append(["T","R","O","U","S","E","R","S"])
print philipsTrousers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM