[英]Reducing a X,Y,Z list to an X,Y list and retrieve the min & max X of the XY PTS
I have large data sets of XYZ data I need to extract only the XY as PTS and find the minimum and maximum. 我有大量的XYZ数据集,我只需要提取XY作为PTS并找到最小值和最大值。 The real data sets are floats... ( I still struggle to understand the list comprehension I guess... ) This is part of a bigger problem with Ascii Grid Files, that Im trying to resolve by creating a larger XYZ data set... (Which is a poor approach I know...) 真正的数据集是浮点数...(我想我仍然很难理解列表理解...)这是Ascii Grid Files更大问题的一部分,Im试图通过创建更大的XYZ数据集来解决。 (我知道这是一种糟糕的方法...)
# I have got this... xyz data...
PTSXYZ = [[1,1,3],[4,4,2],[6,4,1],[6,6,5]]
# I want to get this....xy data PTSXY = [[1,1],[4,4],[6,4],[6,6]]
# I have Tried.... this ??
PTSXY = [PTSXYZ[0][i],PTSXYZ[1][i] for i in PTSXYZ] # Addressing Wrong ?
#Then If I want the Minimum & Maximum X Values..
print min(PTSXY[:][0]),max(PTSXY[:][0])
For your first question, you can use list comprehension to create a new list based on the first two elements of PTSXYZ
: 对于第一个问题,您可以使用列表PTSXYZ
基于PTSXYZ
的前两个元素创建一个新列表:
PTSXY = [i[:2] for i in PTSXYZ]
print PTSXY
# [[1, 1], [4, 4], [6, 4], [6, 6]]
You might also want to consider a generator, which won't store the entire list in memory: 您可能还需要考虑一个生成器,它不会将整个列表存储在内存中:
PTSXY = (i[:2] for i in PTSXYZ)
For the second question, you can get the min and max by creating a list of X elements and taking their min and max: 对于第二个问题,您可以通过创建X元素列表并取其最小值和最大值来获得最小值和最大值:
print min(i[0] for i in PTSXY)
# 1
print max(i[0] for i in PTSXY)
# 6
For retrieving from the XYZ matrix the XY matrix you can proceed with slicing : 要从XYZ矩阵中检索XY矩阵,可以进行切片 :
import numpy as np
a = np.array([[1, 1, 3], [4, 4, 2], [6, 4, 1], [6, 6, 5]])
a[:, :2]
With its output being: 输出为:
array([[1, 1],
[4, 4],
[6, 4],
[6, 6]])
Then, for retrieving the min and max of the X you can use numpy.min
and numpy.max
: 然后,要检索X的最小值和最大值,可以使用numpy.min
和numpy.max
:
_max, _min = np.max(a[:, 0]), np.min(a[:, 0])
Obtaining: 获得:
6, 1
Let's run a test to see how the difference between numpy and list comprehension using numpy: 让我们运行一个测试,以查看numpy和使用numpy的列表理解之间的区别:
import numpy as np
a = np.random.randint(1000, size=(1000, 3))
%timeit [[i[0], i[1]] for i in a]
>>> 483 µs ± 10.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit a[:,:2]
>>> 391 ns ± 6.29 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
As expected, numpy is amazingly faster. 不出所料,numpy的速度惊人地快。
Once you get the XY list as @101 explained, you can get the minimum and maximum in a similar way: 一旦获得了@ 101解释的XY列表,就可以通过类似的方式获得最小值和最大值:
PTSXY = [[i[0], i[1]] for i in PTSXYZ]
minimum = min([i[0] for i in PTSXY])
maximum = max([i[0] for i in PTSXY])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.