Skip to content Skip to sidebar Skip to footer

Create 2d Array In Python?

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(

Solution 1:

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 like ints or tuples, but not for mutables like list, because changing one of the objects will change all of them. See:

>>> foo = [[]]*10
>>> foo[0].append(1)
>>> foo
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]

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:

>>>bar = [[] for _ inrange(10)]>>>bar[0].append(1)>>>bar
[[1], [], [], [], [], [], [], [], [], []]

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 for loop in Python is designed to handle iterating over objects so that you don't have to manage index variables (index and count in your code) manually. It would be better rewritten as follows:

import numpy as np
m = 4
tagProb = np.array(list(line.split("@@")[2].strip() for line in lines)))
tagProb = tagProb.reshape((m+1,-1)).T

Explanation: the first line defines tagProb as 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 height m+1 and inferred width (note that it must be square for this to work; you can pad it with None if 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.

Solution 2:

Create one list at a time and insert them:

importcopy
m=4
tagProb=[]
count=0
index=0for line in lines:
    print(line)
    innerlist = []
    if(count < m+1):
       innerlist.append(line.split('@@')[2].strip()) 
       count+=1if(count == m+1): // this check to goto next index
        count = 0
        index+=1
        tagProb.append(copy.deepcopy(innerlist))
        innerlist = []
print(tagProb)  

As you can see, there is an innerlist that is added to, then for each row, it adds the list to the list of lists. (You may want to do a list copy, though).

Solution 3:

m=4
tagProb=[]
count=0index=0
 innerlist = []
for line in lines:
print(line)

if(count < m+1):
   innerlist.append(line.split('@@')[2].strip()) 
   count+=1if(count == m+1): // this check to gotonextindex
    count = 0index+=1
    tagProb.append(innerlist)
    innerlist = []
print(tagProb) 

Post a Comment for "Create 2d Array In Python?"