使用 python 查找列表中唯一的最大值

发布于 2024-08-24 18:06:35 字数 313 浏览 2 评论 0原文

我有一个点列表,如下所示,

points=[ [x0,y0,v0],  [x1,y1,v1],  [x2,y2,v2].......... [xn,yn,vn]]

其中一些点具有重复的 x,y 值。我想要做的是提取唯一的最大值 x,y 点

例如,如果我有点 [1,2,5] [1,1,3] [1,2,7] [1, 7,3]

我想获取列表[1,1,3] [1,2,7] [1,7,3]

我如何在 python 中执行此操作?

谢谢

I have a list of points as shown below

points=[ [x0,y0,v0],  [x1,y1,v1],  [x2,y2,v2].......... [xn,yn,vn]]

Some of the points have duplicate x,y values. What I want to do is to extract the unique maximum value x,y points

For example, if I have points [1,2,5] [1,1,3] [1,2,7] [1,7,3]

I would like to obtain the list [1,1,3] [1,2,7] [1,7,3]

How can I do this in python?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心作怪 2024-08-31 18:06:35

例如:

import itertools

def getxy(point): return point[:2]

sortedpoints = sorted(points, key=getxy)

results = []

for xy, g in itertools.groupby(sortedpoints, key=getxy):
  results.append(max(g, key=operator.itemgetter(2)))

即:按 xy 对点进行排序和分组,对于具有固定 xy 的每个组,选取具有最大 z 的点。如果您熟悉 itertools (你应该如此,它确实是一个非常强大且有用的模块!)。

或者,您可以构建一个字典,其中 (x,y) 元组作为键,z 列表作为值,然后对该字典进行最后一次传递以选择最大 z 对于每个 (x, y),但我认为排序和分组方法更可取(除非你有数百万个点,以至于排序的大 O 性能令人担忧我猜是出于可扩展性的目的)。

For example:

import itertools

def getxy(point): return point[:2]

sortedpoints = sorted(points, key=getxy)

results = []

for xy, g in itertools.groupby(sortedpoints, key=getxy):
  results.append(max(g, key=operator.itemgetter(2)))

that is: sort and group the points by xy, for every group with fixed xy pick the point with the maximum z. Seems straightforward if you're comfortable with itertools (and you should be, it's really a very powerful and useful module!).

Alternatively you could build a dict with (x,y) tuples as keys and lists of z as values and do one last pass on that one to pick the max z for each (x, y), but I think the sort-and-group approach is preferable (unless you have many millions of points so that the big-O performance of sorting worries you for scalability purposes, I guess).

夏の忆 2024-08-31 18:06:35

您可以使用 dict 来实现此目的,使用 属性表示“如果给定的键出现多次,则与其关联的最后一个值将保留在新字典中。”此代码对点进行排序以确保最高值出现在后面,创建一个字典,其键是前两个值的元组,其值是第三个坐标,然后将其转换回列表

points = [[1,2,5], [1,1,3], [1,2,7], [1,7,3]]
sp = sorted(points)
d = dict( ( (a,b), c) for (a,b,c) in sp)
results = [list(k) + [v] for (k,v) in d.iteritems()]

可能有一种方法可以进一步改进它,但它满足您的所有要求。

You can use dict achieve this, using the property that "If a given key is seen more than once, the last value associated with it is retained in the new dictionary." This code sorts the points to make sure that the highest values come later, creates a dictionary whose keys are a tuple of the first two values and whose value is the third coordinate, then translates that back into a list

points = [[1,2,5], [1,1,3], [1,2,7], [1,7,3]]
sp = sorted(points)
d = dict( ( (a,b), c) for (a,b,c) in sp)
results = [list(k) + [v] for (k,v) in d.iteritems()]

There may be a way to further improve that, but it satisfies all your requirements.

东走西顾 2024-08-31 18:06:35

如果我理解你的问题..也许使用字典将 (x,y) 映射到 max z

像这样(未测试)

dict = {}
for x,y,z in list
    if dict.has_key((x,y)):
        dict[(x,y)] = max(dict[(x,y)], z)
    else:
        dict[(x,y)] = z

虽然顺序会丢失

If I understand your question .. maybe use a dictionary to map (x,y) to the max z

something like this (not tested)

dict = {}
for x,y,z in list
    if dict.has_key((x,y)):
        dict[(x,y)] = max(dict[(x,y)], z)
    else:
        dict[(x,y)] = z

Though the ordering will be lost

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文