this is the code i am trying to create the 2d matrix
m=4
tagProb=[[]]*(m+1)
count=0
index=0
for line in lines:
print(line)
if(count < m+1):
tagProb[index].append(line.split('@@')[2].strip())
count+=1
if(count == m+1): // this check to goto next index
count = 0
index+=1
print(tagProb)
i get the o/p
[['0.0', '0.6', '0.05', '0.3', '0.05', '0.1', '0.0', '0.6', '0.0', '0.0', '0.1', '0.0', '0.0', '0.9', '0.0', '0.1', '0.0', '0.2', '0.7', '0.0', '0.1', '0.0', '0.9', '0.0', 0.0'], ['0.0', '0.6', '0.05', '0.3', '0.05', '0.1', '0.0', '0.6', '0.0', '0.0', '0.1', '0.0', .0', '0.9', '0.0', '0.1', '0.0', '0.2', '0.7', '0.0', '0.1', '0.0', '0.9', '0.0', '0.0'], '0.0', '0.6', '0.05', '0.3', '0.05', '0.1', '0.0', '0.6', '0.0', '0.0', '0.1', '0.0', '0.0','0.9', '0.0', '0.1', '0.0', '0.2', '0.7', '0.0', '0.1', '0.0', '0.9', '0.0', '0.0'] ]
all the values are appended and list has same values.
How can i avoid this ?
You are using
*on lists, which has a gotcha — it will make a list of lots of references to the same object. This is fine for immutables likeints ortuples, but not for mutables likelist, because changing one of the objects will change all of them. See:If you don’t want this to happen, the standard way to avoid it is to use a list comprehension, which will initialise the list with new objects:
However, this issue doesn’t appear very much in idiomatic Python, because initialising a large list is not a common thing to do — it’s very much a C mentality. (That’s not to say that it’s not sometimes the right thing to do — Python is multi-paradigm!)
On a different note, your code is not nice. The
forloop in Python is designed to handle iterating over objects so that you don’t have to manage index variables (indexandcountin your code) manually. It would be better rewritten as follows:Explanation: the first line defines
tagProbas a numpy array (a fast C-based array type with a lot of linear algebra functions) of one dimension with all the values in a row. The second line coerces it into a matrix of heightm+1and inferred width (note that it must be square for this to work; you can pad it withNoneif necessary) and then transposes it. I believe this is what your iteration does, but it’s kinda hard to follow — let me know if you want a hand with this.