我得到了defaultdict,其中列表作为值,元组作为键(在下面的代码中是ddict).我想找出一组给定关键点的最小值和最大值.密钥以数字数组的形式给出.NumPy数组是包含密钥的3Darray.3D数组的每一行是我们需要为其找到minmax的键块,即,对于每一行,我们获取对应的2D数组条目,并获得与这些条目相对应的值,并在这些值上找到minmax.我需要对3D数组的所有行执行此操作.

from operator import itemgetter
import numpy as np

ddict =  {(1.0, 1.0): [1,2,3,4], (1.0, 2.5): [2,3,4,5], (1.0, 3.75): [], (1.5, 1.0): [8,9,10], (1.5, 2.5): [2,6,8,19,1,31], (1.5,3.75): [4]}
indA = np.array([ [ [( 1.0, 1.0), ( 1.0, 3.75)], [(1.5,1.0), (1.5,3.75)] ], [ [(1.0, 2.5), (1.5,1.0)], [(1.5, 2.5), (1.5,3.75)] ] ])

mins = min(ddict, key=itemgetter(*[tuple(i) for b in indA for i in b.flatten()]))
maxs = max(ddict, key=itemgetter(*[tuple(i) for b in indA for i in b.flatten()]))

我try 了上面的代码以获得

min1 = min([1,2,3,4,8,9,10,4])&min2 = min([2,3,4,5,8,9,10,2,6,8,19,1,31,4])max1= max([1,2,3,4,8,9,10,4])&&max2 = max([2,3,4,5,8,9,10,2,6,8,19,1,31,4])

我想为NumPy数组中的每个2D数组计算minmax.有什么解决办法吗?为什么我的代码不工作?它给出了错误TypeError: tuple indices must be integers or slices, not tuple

推荐答案

这就是我认为你想要的:

import numpy as np

# I've reformatted your example data, to make it a bit clearer
# no change in content though, just different whitespace
# whether d is a dict or defaultdict doesn't matter
d = {
    (1.0, 1.0): [1, 2, 3, 4],
    (1.0, 2.5): [2, 3, 4, 5],
    (1.0, 3.75): [],
    (1.5, 1.0): [8, 9, 10],
    (1.5, 2.5): [2, 6, 8, 19, 1, 31],
    (1.5, 3.75): [4]
}

# indA is just an array of indices, avoid capitals in variable names
indices = np.array(
    [
        [[(1.0, 1.0), (1.0, 3.75)], [(1.5, 1.0), (1.5, 3.75)]],
        [[(1.0, 2.5), (1.5, 1.0)], [(1.5, 2.5), (1.5, 3.75)]]
    ])

for group in indices:
    # you flattened each grouping of indices, but that flattens
    # the tuples you need intact as well:
    print('not: ', group.flatten())
    # Instead, you just want all the tuples:
    print('but: ', group.reshape(-1, group.shape[-1]))

# with that knowledge, this is how you can get the lists you want
# the min and max for
for group in indices:
    group = group.reshape(-1, group.shape[-1])
    values = list(x for key in group for x in d[tuple(key)])
    print(values)

# So, the solution:
result = [
    (min(vals), max(vals)) for vals in (
        list(x for key in grp.reshape(-1, grp.shape[-1]) for x in d[tuple(key)])
        for grp in indices
    )
]
print(result)

输出:

not:  [1.   1.   1.   3.75 1.5  1.   1.5  3.75]
but:  [[1.   1.  ]
 [1.   3.75]
 [1.5  1.  ]
 [1.5  3.75]]
not:  [1.   2.5  1.5  1.   1.5  2.5  1.5  3.75]
but:  [[1.   2.5 ]
 [1.5  1.  ]
 [1.5  2.5 ]
 [1.5  3.75]]
[1, 2, 3, 4, 8, 9, 10, 4]
[2, 3, 4, 5, 8, 9, 10, 2, 6, 8, 19, 1, 31, 4]
[(1, 10), (1, 31)]

也就是说,[(1, 10), (1, 31)]是您要计算的结果,1是第一组索引的组合值中的最小值,10是同一组值中的最大值,依此类推.

对关键线路的一些解释:

values = list(x for key in group for x in d[tuple(key)])

这通过循环遍历group中的每一对键值并将它们用作进入词典d的索引来构造组合值的列表.然而,由于密钥在整形后将是ndarray,因此它首先被传递给tuple()函数,从而正确地索引dict.它循环遍历检索到的值并将每个值x添加到结果列表.

解决方案归结为一个单一的理解:

[
    (min(vals), max(vals)) for vals in (
        list(x for key in grp.reshape(-1, grp.shape[-1]) for x in d[tuple(key)])
        for grp in indices
    )
]

外括号表示正在构建一个列表.(min(vals), max(vals))vals的最小和最大值的元组,vals在内心的理解上循环.如上所述,内部理解是一个生成器(使用圆括号而不是方括号)为indices中的每个组生成列表.

Python相关问答推荐

在电影中向西北方向对齐""

如何强制向量中的特定元素在Gekko中处于优化解决方案中

解决Geopandas和Altair中的正图和投影问题

当HTTP 201响应包含 Big Data 的POST请求时,应该是什么?  

ModuleNotFoundError:Python中没有名为google的模块''

Django在一个不是ForeignKey的字段上加入'

在round函数中使用列值

修改.pdb文件中的值并另存为新的

在一个数据帧中,我如何才能发现每个行号是否出现在一列列表中?

有没有一种方法可以根据不同索引集的数组从2D数组的对称子矩阵高效地构造3D数组?

如何让PYTHON上的Selify连接到现有的Firefox实例-我无法连接到Marionette端口

如何在networkx图中提取和绘制直接邻居(以及邻居的邻居)?

解析网站时无法得到正确答案

带参数约束的类型提示函数*args->;tuple[*args]

如何从两对点得到一个二维变换矩阵?

如何从直接URL和间接URL中提取文件扩展名?

如何使用libclang';S Python绑定获取返回类型和参数类型的完全限定名?

Pandas 多列数据帧的重采样和内插

游程编码产生错误的结果

如何在多维情况下检验NumPy数组切片是否为副本?