簡體   English   中英

如何在循環中擴展 numpy 陣列

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

output 參加一周

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

我想 append name_list 中的每個名字在出席的每個項目的最左邊

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM