简体   繁体   English

3D列表索引-如何获取特定x,y的max(z)

[英]3D list indexing- how to get max(z) for a specific x,y

I have a list with 3D index and for each combination (x,y,z) I have a value. 我有一个带有3D索引的列表,对于每个组合(x,y,z)我都有一个值。 I would like to know if there is a way to get the max z for a specific x,y. 我想知道是否有一种方法可以获取特定x,y的最大值z。

Example: 例:

 a=defaultdict(list)
 ...
 a = {(0,0,1): 5, (0,0,2): 1, (0,0,3): 4}

How to get the max(z) for (x=0,y=0) (which should be 3)? 如何获得(x = 0,y = 0)(应该为3)的max(z)?

I need to use this kind of data structure, because I have a dynamic 3 dimensional matrix with unknown z (heterogenous data over Z). 我需要使用这种数据结构,因为我有一个动态的3维矩阵,其中z未知(Z上的异构数据)。

How about: max(b[2] for b in a.keys() if b[0] == 0 and b[1] == 0) 怎么样: max(b[2] for b in a.keys() if b[0] == 0 and b[1] == 0)

This uses a generator expression to return the z-coordinate of those values where x and y is 0. 这使用生成器表达式返回x和y为0的那些值的z坐标。

If you have access to numpy , this could be much easier: 如果您可以访问numpy ,这可能会容易得多:

>>> import numpy as np
>>> a = np.arange(1, 9).reshape((2, 2, 2))
>>> a
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])
>>> a[0, 0].max()
2

In case you need this for any of all possible combinations of x and y, i suggest that you should reorder your data, such that a tuple of x and y is a key, whereas the corresponding z is the value. 如果您需要x和y的所有可能组合中的任何一个,我建议您应该对数据重新排序,以使x和y的元组为键,而对应的z为值。 Then you can simply find the max z: 然后,您可以简单地找到最大z:

from collections import defaultdict
a = {(0,0,1): 5, (0,0,2): 1, (0,0,3): 4, (1,0,2): 5}
b = defaultdict(list)
for xyz in a:
    xy = xyz[:-1]
    z = xyz[-1]
    b[xy].append(z)

max(b[(0,0)])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当Z是Python中的列表列表时,如何用X,Y,Z绘制3D曲面? - How to plot 3D surface with X, Y, Z when Z is a list of list in Python? Python-如何在表面离散3D点(x,y,z)之后从给定的x,y获取z值 - Python - How to get z value from given x, y after surface discrete 3D points (x,y,z) 获取 3D 中平面给定 x 和 z 的 y 坐标 - Get y coordinates given x and z for a plane in 3D Python列表索引,列表[[x] [y]:-z] - Python list Indexing, List[ [x][y]:-z ] 如何制作 3D plot(X,Y,Z),将 Z 值分配给 X,Y 有序对? - How to make a 3D plot (X, Y, Z), assigning Z values to X,Y ordered pairs? 如何在Python中获取每次单击的3D模型的(x,y,z)坐标? - How to get the (x,y,z) coordinates of a 3D model of every click in Python? 3d将x,y,z坐标转换为3d numpy数组 - 3d coordinates x,y,z to 3d numpy array Python,Axes3D,绘图:无法附加到我的3D散点图中的X,Y,Z值列表 - Python, Axes3D, plotting: Cannot append to List of X,Y,Z values in my 3D scatterplot (x,y)对,用于列表中的最大z值 - (x,y) pair for max z value in list 如何绘制来自3个不同列表(x,y和z)的3d直方图,其中z是所有重复的x,y坐标的平均值 - How to plot a 3d histogram from 3 different lists (x, y and z), where z is the mean of all repeated x,y coordinates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM