简体   繁体   中英

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(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 like int s or tuple s, 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 _ in range(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.

Create one list at a time and insert them:

import copy
m=4
tagProb=[]
count=0
index=0
for line in lines:
    print(line)
    innerlist = []
    if(count < m+1):
       innerlist.append(line.split('@@')[2].strip()) 
       count+=1
    if(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).

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

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

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