简体   繁体   中英

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? 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'] ?

["T"] + ["T"] is the same thing as ["T", "T"] . Multiplication is just addition several times. Therefore, ["T"] * 5 is the same thing as ["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:

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.


1 Don't use list multiplication with mutable objects because each item in the list will be the same object . 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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