I need a 2D array to store strings of variable length. Numpy can do 2D arrays of strings but you need to pre-declare the length which is a bit of a hassle for the intended purpose. I thought that I can do
x = [['']*rows]*columns
but that does not work as intended, as illustrated by the following snippet
x = [['']*2]*2
print(x)
x[0][0] = 'a'
print(x)
which produces
[['', ''], ['', '']]
[['a', ''], ['a', '']]
However the following works as intended:
x=[['a','b'],['c','d']]
x[0][0]='f'
print(x)
with a result
[['f', 'b'], ['c', 'd']]
Obviously the original definition is triggering some type of a shared memory issue but I am having a hard time wrapping my mind over what is going on and how to work around it. How do I sent up a [rows,columns] array of lists where I can address each [row,column] element?