当前位置 博文首页 > leslie lee的博客(python ansys):根据子数组来获取原数组的下
[1]Python zip() 函数. https://www.runoob.com/python/python-func-zip.html
子数组b,是从原数组a中抽样得到的,通过a与b来获取b中的值在a中所对应的下标,也叫索引。
目前只想到一个方法,原本想利用where函数的,但不行。
该方法得到了布尔索引
index = [i in b for i in a]
如果是一维数组np.where(condition,x,y)相当于:
[xv if c else yv
for c, xv, yv in zip(condition, x, y)]
理解这个需要明白:zip函数、列表解析
condition,x,y为一维数组,condition为布尔值数组,zip函数将三个数组打包
a = array([1, 2, 3, 4, 5, 6, 7])
b = array([1, 2, 3, 4])
c = zip(a>2, a, b)
print(list(c))
输出为:[(False, 1, 1), (False, 2, 2), (True, 3, 3), (True, 4, 4)]
继续说列表解析,遍历打包后的列表,c为布尔值,如果c为true,则返回xv,否则返回yv,也就相当于:
d = []
for i in c:
if i[0]:
d.append(i[1])
else:
d.append(i[2])
d = np.array(d)