Skip to main content
 首页 » 编程设计

python之按元组切片数组

2024年10月24日11xing901022

我有一个 2 x 20 数组 (A),其中包含另一个二维数组 (B) 的值行和列。

2x20 数组如下所示:

A =  [[151 111] 
     [184 150] 
     [176 156] 
     [101 224] 
     [ 46 156] 
     [134 152] 
     [164 104] 
     [110 252] 
     [286 316] 
     [320  56] 
     [320  41] 
     [164 173] 
     [315 309] 
     [356 231] 
     [342 250] 
     [183 332] 
     [323 307] 
     [298 239] 
     [277 247] 
     [223 228]] 

,其中第一列为行,第二列为列

我想要的是在 B 中为 A 中的行/列的每个组合打印值。 我知道如何打印值:

print B[151, 111] 
print B[184, 150] 

但是我怎样才能为 A 中的每个行/列组合自动执行此操作?

编辑:在索取完整代码后,我将其发布在下方。

c1 = np.array([148, 108]) 
c2 = np.array([181, 147]) 
c3 = np.array([173, 153]) 
c4 = np.array([98, 221]) 
c5 = np.array([43, 153]) 
c6 = np.array([131, 149]) 
c7 = np.array([161, 101]) 
c8 = np.array([107, 249]) 
c9 = np.array([283, 313]) 
c10 = np.array([317, 53]) 
c11 = np.array([317, 38]) 
c12 = np.array([161, 170]) 
c13 = np.array([312, 306]) 
c14 = np.array([353, 228]) 
c15 = np.array([339, 247]) 
c16 = np.array([180, 329]) 
c17 = np.array([320, 304]) 
c18 = np.array([295, 236]) 
c19 = np.array([274, 244]) 
c20 = np.array([220, 225]) 
 
trees_list = [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20] 
 
def tree_add_rcol(filt_size, tree): 
    rc_add = filt_size // 2 
    tree_add = tree + rc_add 
    print tree_add 
    return tree_add 
 
 
def trees_pixel(rc_list, site): 
    t_row = rc_list[0] 
    t_col = rc_list[1] 
    tree = site[t_row, t_col] 
    print tree 
    return tree 
 
for i in trees_list: 
    trees_pixel(i, site) 
 
def trees_add_matrix(trees_list): 
    list = [] 
    for i in trees_list: 
        tree = tree_add_rcol(7, i) 
        list.append(tree) 
    trees_mat = np.array(list) 
    print trees_mat 
    return trees_mat 
 
A = trees_add_matrix(trees_list) 
B = np.genfromtxt('E:.....\Input\Plot_1.txt', dtype=None, delimiter='\t') 

请您参考如下方法:

你在找这个吗?

for i,j in A: 
   print( B[i][j] )