由元组切片阵列

时间:2016-01-11 19:18:36

标签: python numpy

我有一个2乘20的数组(A),它包含另一个2d数组(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')

1 个答案:

答案 0 :(得分:2)

你在找这个吗?

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