0

I have little experience coding, most of what I did was done with VBA and there you have arrays. On python you apparently do not use arrays but lists of lists if I'm not mistaken and I seem to have trouble with these. Here a sample of what I did:

array=[["string"]*2]*2

So essentially I wanted to create a 2x2 array. This I wanted to fill using a loop, for the minimal example I fill it with a string b+a:

for b in range(0,2):
    for a in range(0,2):
        array[b][a]=b,"+",a
        print(array[b][a])

print(array[0][0], array[1][0],array[0][1], array[1][1])

The first print command gives what I expected:

(0, '+', 0)
(0, '+', 1)
(1, '+', 0)
(1, '+', 1)

But the second print command is strange:

(1, '+', 0) (1, '+', 0) (1, '+', 1) (1, '+', 1)

When printing array[0][0] I would expect to receive (0, '+', 0) but instead I get what I would expect to be array[1][0]. It looks like when using print inside the loop I get the results that I expect but if I print the exact same element outside of the loops I get strange results.

array[0][0]

should have the exact same content as

array[b][a]

for when I just started the loop (first print command), no?

I'm sorry I don't really know how to describe the problem more precisely, I hope you see what my problem is.

Deglupta
  • 25
  • 4

0 Answers0