简体   繁体   中英

how to extend the numpy array in loop

import numpy as np

attendance = np.array([1,0,0,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,1,1,1,1])

name_list = np.array(["Ali","Ahmad","Beng","Chris","Sita","Marion","Stephen","Cobby","Akmal","Nita"])

day_list = np.array(["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"])

attendance=np.reshape(attendance,[10,7])

an the output for attendance for a week

 array([[1, 0, 0, 1, 1, 1, 0],
       [1, 0, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 0],
        [1, 0, 0, 1, 1, 1, 1],
        [1, 1, 1, 0, 1, 1, 1],
        [1, 0, 0, 0, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1],
        [1, 1, 0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1, 1, 1],
        [0, 0, 0, 1, 1, 1, 1]])

i want to append each name in name_list to most-left in each item in attendance

Try this

C = np.concatenate((name_list.reshape(-1,1),attendance), axis=1)
print(C)

Output

[['Ali' '1' '0' '0' '1' '1' '1' '0']                                                                                                                 
 ['Ahmad' '1' '0' '1' '1' '1' '1' '1']                                                                                                               
 ['Beng' '1' '1' '1' '1' '1' '1' '0']                                                                                                                
 ['Chris' '1' '0' '0' '1' '1' '1' '1']                                                                                                               
 ['Sita' '1' '1' '1' '0' '1' '1' '1']                                                                                                                
 ['Marion' '1' '0' '0' '0' '1' '1' '1']                                                                                                              
 ['Stephen' '1' '1' '1' '1' '1' '1' '1']                                                                                                             
 ['Cobby' '1' '1' '0' '0' '0' '0' '0']                                                                                                               
 ['Akmal' '1' '1' '1' '1' '1' '1' '1']                                                                                                               
 ['Nita' '0' '0' '0' '1' '1' '1' '1']] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM