Androidy
Androidy

Reputation: 53

Appending to 2 dimensional array in python

I am reading Data from CSV file which comes similar to the below matrix/array

b = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

I would like to change the index of every element greater than 1 to a new row in the arraylist

this will make the above array as below

b = [[1,2],[5,6],[9,10],[3,4],[7,8][11,12]]

what i have done in python (but couldn't get the answer)

b = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
c = b
rows = len(b)
columns = len(b[0])
c[4].append(1)
count = 3
for i in  range(rows):
 for j in  range(columns):
     if i > 1:
         for k in columns
             list1 = 
             c.insert(count,list1)
             count = count + 1

Upvotes: 1

Views: 13845

Answers (8)

Mou
Mou

Reputation: 165

Use numpy.expand_dims() to have one more axis and append your 2D arrays along that axis.

a1=np.expand_dims(np.array(np.arange(4).reshape(2,-1))+0,0)
a2=np.expand_dims(np.array(np.arange(4).reshape(2,-1))+10,0)
a1_a2=np.concatenate([a1,a2],axis=0)

Outcome shown as print(a1_a2):

array([[[ 0,  1],
        [ 2,  3]],
       [[10, 11],
        [12, 13]]])

Upvotes: 0

seralouk
seralouk

Reputation: 33197

One line solution np.array(b).reshape(-1,2):

import numpy as np
b = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

np.array(b).reshape(-1,2)
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10],
       [11, 12]])

Upvotes: 1

Dariswan Janweri P.
Dariswan Janweri P.

Reputation: 281

data =[]
data.append(['a',1])
data.append(['b',2])
data.append(['c',3])
data.append(['d',4])
print(data)

output

[['a', 1], ['b', 2], ['c', 3], ['d', 4]]

Upvotes: 1

Vasilis G.
Vasilis G.

Reputation: 7859

Another approach would be this:

b = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

step = 2
length = len(b[0])
b = [elem[i:i+step] for i in range(0,length,step) for elem in b]
print(b)

Output:

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

Upvotes: 0

Austin
Austin

Reputation: 26047

You can use numpy. Perform indexing and concatenate at the end:

import numpy as np
b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(np.concatenate((b[:,:2], b[:,2:])))

# [[ 1  2]
#  [ 5  6]                                                    
#  [ 9 10]                                                  
#  [ 3  4]                 
#  [ 7  8]                                                     
#  [11 12]]                                                  

Upvotes: 2

Rakesh
Rakesh

Reputation: 82795

Using a list comprehension.

Ex:

b = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
n = 2
b = [j[i:i+n] for j in b for i in range(0, len(j), n)]
b = b[0::2] + b[1::2]
print(b)

Output:

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

Upvotes: 0

snooze_bear
snooze_bear

Reputation: 599

You might want to use numpy arrays and the concatenate function.

import numpy as np
b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) # or b = np.array(b)
c = np.concatenate((b[:, :2], b[:, 2:]),0)

If you prefer working with python arrays, you can use list interpretation:

c = [row[:2] for row in b]
c.extend([row[2:] for row in b])

which returns

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

Upvotes: 0

Probie
Probie

Reputation: 1361

Why not just split it into two lists, and then recombine them?

new_elements = []
for i in range(len(b)):
    if len(b[i]) > 2:
        new_elements.append(b[i][2:])
    b[i] = b[i][:2]
b.extend(new_elements)

Upvotes: 0

Related Questions