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

Python 对数组列表执行迭代处理,为循环中的所有数组保存中间步骤,python,list,for-loop,batch-processing,Python,List,For Loop,Batch Processing,试图找到通过循环执行一组处理步骤以列出数组的方法 假设我们只有一个数组。我们执行以下迭代步骤,以便在需要时为每个步骤运行诊断。每个步骤的过程都是无关的,但输出作为中间过程保存在一个新数组中(步骤1、步骤2、步骤3…等等) 得到: File "<ipython-input-732-7917e62bc9c0>", line 6 (i)_step1=(i)[(i)[:,0]<0.1] ^ SyntaxError: invalid syntax 它看起

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

假设我们只有一个数组。我们执行以下迭代步骤,以便在需要时为每个步骤运行诊断。每个步骤的过程都是无关的,但输出作为中间过程保存在一个新数组中(步骤1、步骤2、步骤3…等等)

得到:

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

它看起来很简单,但我不能把我的头绕在它身上…..

编辑

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

val = 0.1
first_element_lessthan_pt1 = [row for row in a if row[0]<val]
如果需要,您可以使用另一个namedtuple为每个对象上的每个测试提供dot-note访问权限,只需遵循与步骤相同的概念即可

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
从随机导入随机
从集合导入namedtuple
#一种对象类型,用于帮助组织我们的步骤,以便我们可以更轻松地遵循代码
#并参考点符号中的每个步骤/测试,这可能会有所帮助!
StepResult=namedtuple(“StepResult”、“lt1\U列表、gt1\U列表、lt5\U列表、gt5\U列表”)
def rand(行、列):
#创建形状为(行、列)的交错数组/浮动列表
#这是OP使用的numpy函数的模拟
在xrange(0,列)中为_r返回[[random()表示xrange(0,列)]
def运行步骤(tup):
#tup是一个表示交错矩阵/数组的iterable。例:名单
#产生四个新列表:
#第一个值小于0.1的嵌套列表
#第一个值大于0.1的嵌套列表
#第一个值小于0.5的嵌套列表
#第一个值大于0.5的嵌套列表
对于(0.1,0.5)中的val:
yield[如果行[0]val,则tup中的行对应行]
def步骤(tup):
#tup是一个表示交错矩阵/数组的iterable
#返回StepResult对象
# .. 注意,*将run_步骤的结果解压为单个参数
#调用StepResult的初始化时。
返回StepResult(*运行步骤(tup=tup))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
“”“一个用随机浮点值创建4个锯齿状数组的程序,然后
遍历列表以提供一种关于哪些元素的报告
每个列表中的值都大于或小于某些值。
"""
a=(兰特(4,3))
b=(兰特(4,3))
c=(兰特(4,3))
d=(兰特(4,3))
元组=(a,b,c,d)
对于元组中的元组:
打印“----原件--”
打印tup
tup_步骤=步骤(tup)
打印“---lt1---”
打印tup_steps.lt1_列表
打印“---gt1---”
打印tup_steps.gt1_列表
打印“---lt5---”
打印tup_steps.lt5_列表
打印“---gt5---”
打印tup_steps.gt5_列表

您能澄清一下这个问题是否与Numpy o plain python有关吗?谢谢,您的代码中似乎没有定义
d
。您可以显示数组的声明吗?如果脚本第一次看到变量d,python将不知道它是什么。如果你有一行像'a,b,c,d=(rand(10000,4)表示范围(4)中的i),你实际上需要处理数组。我想问题是针对普通python的@Kikohs,CeramicSheep你说得对。但是,即使我事先定义了数组,我的输出也不是我所期望的,因为您正在覆盖i的值(通过写入i=(rand(10000,4))在for循环中表示a,b,c,d)。如果您解释了预期的输出是什么,这将对我们有所帮助。可能值得从您尝试的解决方案中后退一步,并为我们提供您尝试解决的上下文问题。我尝试运行代码,但我认为这不是我遇到的问题。我的想法是使用字符串“\u step1”的新名称应添加到原始数组名称(a、b、…)一旦第一步过程完成。我想我需要将数组名称提取为字符串,然后根据该字符串+描述每个步骤的新字符串生成一个新名称。如果按顺序进行,就我所知,不需要包含生成器。因此,可能真正的问题是如何提取数组的名称y作为字符串。这更清楚吗?@AlexD.,您可以使用locals()或globals()获取变量的字符串表示形式,并从中可以求值()基于您正在迭代的变量的新变量。这是非常粗糙且不可取的。请使用上述自定义对象、dict、list或命名元组来表示此关联,而不是尝试将该逻辑构建到变量命名约定中。例如,a.step1可以是自定义对象或命名元组的属性,而不是_step1作为一个不相关的局部变量,它比我想象的要复杂得多,但这种方式非常先进
a_step1, b_step1, c_step1, d_step1
a_step2, b_step2, c_step2, d_step2
a_step3, b_step3, c_step3, d_step3
.......
val = 0.1
first_element_lessthan_pt1 = [row for row in a if row[0]<val]
StepResult = namedtuple("StepResult", "step1, step2, step3, step4")
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