简体   繁体   English

过滤多维数组

[英]Filter Multi-Dimensional Array

I have an array (lists) which is NxK.我有一个数组(列表),它是 NxK。 However, I want to "filter" is after inputting some constraints based on values in Columns 4 and 6. This is the code I have so far.但是,我想在根据第 4 列和第 6 列中的值输入一些约束之后进行“过滤”。这是我目前拥有的代码。

minmag = 5
maxmag = 7

mindist = 25
maxdist = 64

filter = np.zeros((1, 7), dtype='object')
add = np.zeros((1, 7), dtype='object')
k = 0

for i in range(0,len(lists)):
    if lists[i, 4]>= minmag and lists [i, 4] <= maxmag and lists [i, 6]>=mindist and  lists [i, 6]<= maxdist:
        if k == 0:
            for x in range(0,16):
                filter[0, x] = lists[i, x]
            k = 1
        else:
            for x in range(0, 16):
                add[0, x] = lists[i, x]
            filter = np.append(filter, add, axis=0)

It works, however it is not so neat.它有效,但它不是那么整洁。 Just wondering if anyone has a better solution.只是想知道是否有人有更好的解决方案。

Simplifying the most repetitive parts:简化最重复的部分:

if k==0:
    for x in xrange(1,8):
        lists[i,x] = filter[0,x]
    k = 1
else:
    for x in xrange(1,8):
        lists[i,x] = add[0,x]
    filter = np.append(filter, add, axis=0)

You could also combine your nested if s into a single one with the 4 conditions combined with and s.您还可以将嵌套的if s 与 4 个条件组合成一个and s 结合的条件。

I also believe (not seeing how lists is defined, I'm not sure) you can replace the outer loop with我也相信(没有看到lists是如何定义的,我不确定)你可以用

for row in lists:

and then use row[x] in place of lists[i,x]然后使用row[x]代替lists[i,x]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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