farideh
farideh

Reputation: 31

Time-consuming of the definition of two-dimensional array in Python

I want define a two-dimensional array of size [545 333][116 109] and the next part of the code that initializes I use the following code

w1, h1 = 545333, 116109
X = [[0 for x1 in range (w1)] for y1 in range (h1)]
while (i <116109):
 ###### other code...........
     j = 0
     while (j <545333):
         if allfeature [j] not in eachfeature:
             X [i] [j] = 0
         else:
             X [i] [j] = 1
         j = j + 1
     i=i+1

But the problem is the high time and low speed. What code do you recommend?

Upvotes: 1

Views: 98

Answers (2)

PM 2Ring
PM 2Ring

Reputation: 55469

FWIW,

X = [[0] * w1 for y1 in range (h1)]

is faster than

X = [[0 for x1 in range (w1)] for y1 in range (h1)]

and produces identical results. But with w1, h1 = 545333, 116109 you don't have enough RAM to create that list.

A better option is to make X a set of tuples, you may have enough RAM for that, if allfeature[j] in eachfeature is only true for a small number of the j values in range(w1).

Here's a short demo:

w1, h1 = 545333, 116109
X = set()
for j in range(w1):
    if allfeature[j] in eachfeature:
        for i in range(h1):
            X.add((i, j))

However, even that uses quite a lot of RAM, due to the size of h1. As Peter Wood says, there may be a better way to organize the data and logic of your code.


The code you posted in your answer doesn't do what you want. But even if you did have enough RAM to create that list it wouldn't contain the data you want because all the inner lists are actually the same list.

Here's a small demo of that behaviour:

x = []
x1 = [0, 9]
for i in range(5):
    x1[0] = i
    x.append(x1)
print(x)

output

[[4, 9], [4, 9], [4, 9], [4, 9], [4, 9]]

Instead, you need to append copies of x1, like this:

x = []
x1 = [0, 9]
for i in range(5):
    x1[0] = i
    x.append(x1[:])
print(x)

output

[[0, 9], [1, 9], [2, 9], [3, 9], [4, 9]]

Upvotes: 1

farideh
farideh

Reputation: 31

I've found another way

w1= 545333
X1 = [0 for x1 in range(w1)]
X=[]
while(i<116109):
    ..........

    while(j<545333):
        if allfeature[j] in eachfeature:
            X1[j]=1
        j=j+1

    X.append(X1)

Upvotes: 0

Related Questions