Python生成器表达式:如何从循环中添加和设置条件?
具有以下距离矩阵Python生成器表达式:如何从循环中添加和设置条件?,python,generator,generator-expression,Python,Generator,Generator Expression,具有以下距离矩阵dist: dist = [[[ 0, 0], [ 5, 0], [ 1, 0], [ 1, 1], [11, 3], [ 3, 6], [ 5, 5]], [[ 5, 0], [ 0, 0], [ 6, 0], [ 4, 1], [ 6, 3], [ 2, 6], [10, 5]], [[ 1, 0], [ 6, 0], [ 0, 0], [ 2, 1], [12, 3], [ 4, 6], [ 4, 5]], [[ 1, 1],
dist
:
dist = [[[ 0, 0],
[ 5, 0],
[ 1, 0],
[ 1, 1],
[11, 3],
[ 3, 6],
[ 5, 5]],
[[ 5, 0],
[ 0, 0],
[ 6, 0],
[ 4, 1],
[ 6, 3],
[ 2, 6],
[10, 5]],
[[ 1, 0],
[ 6, 0],
[ 0, 0],
[ 2, 1],
[12, 3],
[ 4, 6],
[ 4, 5]],
[[ 1, 1],
[ 4, 1],
[ 2, 1],
[ 0, 0],
[10, 2],
[ 2, 7],
[ 6, 6]],
[[11, 3],
[ 6, 3],
[12, 3],
[10, 2],
[ 0, 0],
[ 8, 9],
[16, 8]],
[[ 3, 6],
[ 2, 6],
[ 4, 6],
[ 2, 7],
[ 8, 9],
[ 0, 0],
[ 8, 1]],
[[ 5, 5],
[10, 5],
[ 4, 5],
[ 6, 6],
[16, 8],
[ 8, 1],
[ 0, 0]]]
我需要构建一个生成器表达式
,其条件如下:
gexp = (dist[:, :, 0] <= 1) & (dist[:, :, 1] <= 2)
我有以下列:
columns = ['weight', 'height']
它们的阈值:
thresholds = {'weight': 1.0, 'height': 1.0}
及其相应的指标:
indexes = {'weight': 0, 'height': 1}
我尝试以以下方式构建生成器表达式:
dynamic_gexpr = [dist[:,:,indexes.get(column)] <= thresholds.get(column) for column in columns]
我使用列表理解来应用构建生成器的条件,以另一种方式来做会让我感觉不对。无论如何,我不确定这是否是你想要的
x = ([second[0] <= 1 and second[1] <=2 for second in first] for first in dist)
for y in x:
print(y)
对于这个版本,你会觉得它是错的,因为它是你想要的
x = ((second[0] <= 1 and second[1] <=2 for second in first) for first in dist)
for y in x:
print([next(y),next(y),next(y),next(y),next(y),next(y),next(y)])
x=((秒[0]如果您添加更多列,这应该会扩展。arrays
将为每个条件保留一个数组。每个条件都通过reduce
进行处理,reduce
依次累加所有数组的按位_和
import numpy as np
from functools import reduce
columns = ['weight', 'height']
thresholds = {'weight': 1.0, 'height': 1.0}
indexes = {'weight': 0, 'height': 1}
l = np.array(dist)
arrays = [(l[:, :, indexes[column]] <= thresholds[column]) for column in columns]
l2 = reduce(lambda a, acc : np.bitwise_and(a, acc), arrays)
print(l2)
正如@1Z10在其评论中所建议的那样,
np.reduce
最终将成为您在这里的朋友
您应该首先构建一个布尔矩阵,其中每个最高级别的行都是子数组与其相应阈值的比较。然后只需在该矩阵上使用np.reduce
和nb.logical_和
即可得到您的结果,无论涉及多少次比较:
dynamic_expr = np.logical_and.reduce(np.array(
[dist[:, :, indexes[col]] <= thresholds[col]
for col in columns]))
print(dynamic_expr)
您可以共享一个更副本的可复制ndarray吗?:-)(一个简单的打印就可以了)使用
numpy
,您可以这样做:l2=np.array(l)
和l3=(l2[:,:,0]@yatu现在它已被编辑,我不知道为什么它打印时不带逗号。@Jeppe我需要在运行时使用列、索引和thResold中的值来构建此表达式。所有带I的条件都可以有N个条件,我只能在运行时知道,是否可以在不硬编码和条件的情况下以编程方式实现这一点ons或索引,以及阈值?添加了另一段代码,我在其中分解了比较位,因此更具程序性…也许这会有所帮助。
x = ((second[0] <= 1 and second[1] <=2 for second in first) for first in dist)
for y in x:
print([next(y),next(y),next(y),next(y),next(y),next(y),next(y)])
def compare(a,b):
return all([a[i] <= b[i] for i in range(len(a))])
x = ((compare(second, [1,2]) for second in first) for first in dist)
for y in x:
print([next(y),next(y),next(y),next(y),next(y),next(y),next(y)])
import numpy as np
from functools import reduce
columns = ['weight', 'height']
thresholds = {'weight': 1.0, 'height': 1.0}
indexes = {'weight': 0, 'height': 1}
l = np.array(dist)
arrays = [(l[:, :, indexes[column]] <= thresholds[column]) for column in columns]
l2 = reduce(lambda a, acc : np.bitwise_and(a, acc), arrays)
print(l2)
[[ True False True True False False False]
[False True False False False False False]
[ True False True False False False False]
[ True False False True False False False]
[False False False False True False False]
[False False False False False True False]
[False False False False False False True]]
dynamic_expr = np.logical_and.reduce(np.array(
[dist[:, :, indexes[col]] <= thresholds[col]
for col in columns]))
print(dynamic_expr)
[[ True False True True False False False]
[False True False False False False False]
[ True False True False False False False]
[ True False False True False False False]
[False False False False True False False]
[False False False False False True False]
[False False False False False False True]]