'how to get all column for 2-d array except first column?

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

for example the 2-d array like this how to get all the column except first one without using any library? the total column is unknown, this is just an example.

Thank for help!!



Solution 1:[1]

You can try this:

import numpy as np

a=[['1','2','3'],
   ['4','5','6'],
   ['7','8','9']] # a size is (3,3)

a = np.array(a)

all_col = np.zeros((np.shape(a[:,1:]))) # all_col size is similar to "a" with only one column less

for i in enumerate(a[:,1:]):
    all_col[i[0]] = i[1]
print(all_col)

Notice that 1: in the for loop above causes iteration to continue until the last column while skips the first one. Similarly, if you want to start iteration from column n until column m you can write [:,m:n]. Also, notice i[0] is number of index which helps allocate "a" array values, i[1], inside all_col array.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hamed Sabagh