Python create multi dimensional array of of normal array

Viewed 491

I want to create a multi dimensional Array out of normal array. When there is a 0 in my start array i want to change the row number by 1.

My start array looks like this:

arr = [1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9]

My futre multi array should look like this:

multi_arr = [[1, 2, 3],
             [4, 5, 6],
             [7, 8, 9]]

My code looks like this so far:

multi_arr = []
end = len(arr)
i = 0   #row
j = 0   #column

for h in range(end):

   if arr[h] != 0:
       j += 1
       multi_arr[i][j]= arr[h]

   elif arr[i] != 0:
       i += 1

I always get a list index error with this code.

6 Answers

This should do the job in a simpler way:

arr = [1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9]

multi_arr = []

temp_arr = []
for h in arr:
    if h != 0:
        temp_arr.append(h)
    else:
        multi_arr.append(temp_arr)
        temp_arr = []

if h != 0:  # Insert the last array it the last number wasn't 0
    multi_arr.append(temp_arr)

print(multi_arr)

This is happening because at the point where you are trying set multi_arr[i][j], it is still a 1-d array. You can verify this with a print statement or a debugger - like so:

for h in range(end):

    if arr[h] != 0:
        print(multi_arr)
        j += 1
        multi_arr[i][j]= arr[h]

    elif arr[i] != 0:
        i += 1

I would definitely try something more pythonic for what you are trying to accomplish, reading the elements into a temp array and appending that temp array to multi array.

arr = [1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9]
multi_arr = []
temp_array = []

for item in arr:
    if item == 0:
        multi_arr.append(temp_array)
        temp_array = []
    else:
        temp_array.append(item)
multi_arr.append(temp_array)
        

print(multi_arr)

If you really want to work with lists, you dont need a column indexer. You can simply append the value to the correct list.


multi_arr = [[]]  # start with a nested list, since your vector doesnt start with a 0
endi = len(arr)
i = 0   #row

for h in range(endi):

   if arr[h] != 0:
       multi_arr[i].append(arr[h])
   else:
       i += 1
       multi_arr.insert(i,[])  # open new list

output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

You are declaring a 1D array and appending like 2D array One solution two your Problem is :

arr=[1,2,3,4,5,6,7,8,9]
multi_arr=[]
end = len(arr)
a = []
counter =0
for i in arr :
    if counter < 3 :
        a.append(i)
        counter += 1
    else:
        multi_arr.append(a)
        a=[]
        counter = 0
        a.append(i)
        counter += 1   
multi_arr.append(a)
print(multi_arr)

Here is a yield approach:

def f(arr):
    res = []
    for x in arr:
        if x == 0:
            yield res
            res = []
        else:
            res.append(x)
    yield res

list(f(arr))

#> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

A different approach for you: super simple and no loop.

import numpy as np

arr = [1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9]
a = [i for i in arr if i]
x = arr[arr!=0] + 1

np.array(a).reshape([-1, x])

Output:

array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

And if you need lists, just use np.array(a).reshape([x, y]).tolist() instead.

One-liner:

After converting your list to a numpy array a = np.array(arr):

a[a!=0].reshape([-1, len(a) - np.count_nonzero(a)+1])
Related