1

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

8 Answers 8

2

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]]                                                  
Sign up to request clarification or add additional context in comments.

Comments

1
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]]

Comments

1

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]])

Comments

0

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)

Comments

0

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]]

Comments

0

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]]

4 Comments

Rakesh, thanks for the quick response but the answer should be [[1,2],[5,6],[9,10],[3,4],[7,8][11,12]]
the data actually represent the below
studentID StudentName MathGrade ScienceGrade studentID StudentName MathGrade ScienceGrade studentID StudentName MathGrade ScienceGrade (all students data comes in rows which i try to get in column)
Updated snippet.
0

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]]

Comments

0

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]]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.