gpt4 book ai didi

python - 对数组列表执行迭代处理,节省循环中所有数组的中间步骤

转载 作者:太空宇宙 更新时间:2023-11-03 16:59:48 24 4
gpt4 key购买 nike

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

假设我们只有一个数组。我们执行以下迭代步骤,因此我们可以根据需要对每个步骤运行诊断。每个步骤的过程无关紧要,但输出作为中间保存在新数组中(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
.......

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

最佳答案

已编辑

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

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

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

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

关于python - 对数组列表执行迭代处理,节省循环中所有数组的中间步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35088741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com