对数组列表执行迭代处理,为循环中的所有数组保存中间步骤

Perform itterative processing to a list of arrays saving intermediate steps for all arrays in a loop

试图找到通过循环对数组列表执行一组处理步骤的方法

假设我们只有一个数组。我们执行以下迭代步骤,因此如果需要,我们可以 运行 对每个步骤进行诊断。每个步骤的过程是无关紧要的,但输出结果作为中间(step1,step2,step3 ...等)保存在一个新数组中。

a=(rand(10000,4))

#This is the process that i want to repeat for all the arrays:
a_step1=a[a[:,0]<0.1]                 #This is the first step
a_step2=a_step1[a_step1[:,1]>0.1]     #second
a_step3=a_step2[a_step2[:,2]<0.5]     #third
....                                  #etc

像这样最后我有 a, a_step1, a_step2, a_step3 作为我可以执行检查的数组。

所以我想在一系列数组(a、b、c、d...)上执行此操作

我试过了

a=(rand(10000,4))
b=(rand(10000,4))
c=(rand(10000,4))
d=(rand(10000,4))
for i in (a,b,c,d):
    (i)_step1=(i)[(i)[:,0]<0.1]
    (i)_step2=(i)_step1[(i)_step1[:,1]>0.1]
    (i)_step3=(i)_step2[(i)_step2[:,2]<0.5]
    .....

并得到:

File "<ipython-input-732-7917e62bc9c0>", line 6
    (i)_step1=(i)[(i)[:,0]<0.1]
            ^
SyntaxError: invalid syntax

所以给定所有数组的列表(a,b,c,d...) 我希望最后将以下内容作为数组生成以供 运行 检查:

a_step1, b_step1, c_step1, d_step1
a_step2, b_step2, c_step2, d_step2
a_step3, b_step3, c_step3, d_step3
.......

看起来很简单,但我无法理解......

已编辑

您尝试进行的处理如下。我使用列表理解,测试第一个元素与一个值,如果测试结果为真,则返回整行。如果您不喜欢这种语法,或者如果检查的需求对于单行来说太大了,您也可以使用 filter() 或自定义函数来执行此操作。

val = 0.1
first_element_lessthan_pt1 = [row for row in a if row[0]<val]

我还将所有四个 "steps" 合并到一个命名元组中,而不是每个值一个。如果您仍希望将它们命名为 step1/step2/step3/step4,则可以改为如下声明 StepResult,并相应地编辑打印语句。

StepResult = namedtuple("StepResult", "step1, step2, step3, step4")

如果需要,您可以使用另一个 namedtuple 为每个对象的每个测试提供点注释访问权限,只需遵循与我在步骤中所做的相同的概念。

from random import random
from collections import namedtuple


# an object type to help organize our steps so we can follow code easier
# and refer to each step/test in dot-notation which can be helpful!
StepResult = namedtuple("StepResult", "lt1_list, gt1_list, lt5_list, gt5_list")

def rand(row, col):
    # creates a jagged array/list of floats in the shape of (row, col)
    # this is a mock of a numpy function which OP was using
    return [[random() for _c in xrange(0, col)] for _r in xrange(0, row)]


def run_steps(tup):
    # tup is an iterable representing a jagged matrix/array. ex: list of lists
    # yields four new lists:
    # nested lists where the first value are less than 0.1
    # nested lists where the first value are greater than 0.1
    # nested lists where the first value are less than 0.5
    # nested lists where the first value are greater than 0.5

    for val in (0.1, 0.5):
        yield [row for row in tup if row[0]<val]
        yield [row for row in tup if row[0]>val]


def steps(tup):
    # tup is an iterable representing a jagged matrix/array
    # returns a StepResult object
    # .. note, the * unpacks the results of run_steps as individual parameters
    # when calling the initalize of StepResult.
    return StepResult(*run_steps(tup=tup))


if __name__ == '__main__':
    """ A program which creates 4 jagged arrays with random float values, then
        iterates through the lists to provide a sort of report on which elements
        in each list are greater-than or less-than certain values.
    """
    a=(rand(4, 3))
    b=(rand(4, 3))
    c=(rand(4, 3))
    d=(rand(4, 3))

    tuples = (a, b, c, d)
    for tup in tuples:
        print "----original----"
        print tup
        tup_steps = steps(tup)
        print "----lt1----"
        print tup_steps.lt1_list
        print "----gt1----"
        print tup_steps.gt1_list
        print "----lt5----"
        print tup_steps.lt5_list
        print "----gt5----"
        print tup_steps.gt5_list