按最后一个元素从列表列表中选择最大一个的最佳方法是什么? [英] What's the best way to select the maximum one from a list of lists by their last element?

查看:25
本文介绍了按最后一个元素从列表列表中选择最大一个的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Mathematica 中,Max[] 是获取数字列表中最大数字的最有效函数,但是如何在列表列表中找到具有最大最后一个元素的列表?例如一系列坐标中 x 部分最大的二维坐标.

In Mathematica, Max[] is the most efficient function to get the maximum number in a list of numbers, but how do I find the list with the maximum last element in a list of lists? e.g. the 2-d coordinate with the biggest x part in a series of coordinates.

我最好的尝试是 SortBy,但显然我不需要程序来对我的列表进行排序,只需要我需要的最大一个.

My best try is SortBy, but obviously I don't need the program to sort my list, only the maximum one I need.

推荐答案

在阅读了一些文档并做了一些实验后,我设法对这个问题有了更清晰的认识.

After reading some documentations and doing a few experiments, I managed to get a clearer view of this problem.

我实际上想知道为什么 Max[] 似乎故意避免提供指令,使其不仅返回 max 元素本身,还返回其位置.毕竟,提供位置不会改变算法的 O(n) 复杂度.例如,想象:

I actually was wondering why Max[] seemed intentionally avoid providing directives that make it return not only the max element itself, but also its position. After all, providing position doesn't change the O(n) complexity of the algorithm. For example, imagine:

In[1]:= Max[{991, 993, 992}, ReturnPosition -> True]

Out[1]= {2}

如果可以的话,你可以简单地使用下面的代码来解决我的问题:

If that could be done, you can simply use the code below to solve my problem:

list[[Max[list[[All, -1]], ReturnPosition -> True]]]

但现在我意识到系统函数Max[] 不是为在lists 中查找最大元素而设计的不是.您可以看出 Wolfram 团队显然使 Max[] 更像数学中的传统 max 函数——它进行简单的符号化简,它自动展平所有列表,它可以在一个可绘制的函数中,最重要的是,它是Orderless:

But now I realize that the system function Max[] is not designed for finding the max element in lists. You can tell that the Wolfram team obviously made Max[] more like a traditional max function in mathematics ― it does simple symbolic simplifications, it automatically flatten out all lists, it can be in a plotable function, and most importantly, it's Orderless:

In[2]:= Attributes[Max]

Out[2]= {Flat, NumericFunction, OneIdentity, Orderless, Protected}

这使得职位毫无意义.总之,它把里面的所有列表都当作数学集.

Which makes positions meaningless. In a word, it treats all the lists inside as mathematical sets.

因此从哲学上讲,Mathematica 计算此值并非易事.我需要做的就是DIY"一个复杂度为 O(n) 的函数并且可以完成这项工作.我认为 TomD 正朝着正确的方向前进,尽管我更喜欢:

So philosophically it's not trivial for Mathematica to compute this. All I need to do is to "DIY" a function with the O(n) complexity and can do the job. I think TomD is heading the right direction, although I prefer:

maxLast[l_] := Cases[l, {___, Max[Last/@l]}]

而Heike(黑客?)采用了Pick,它可能有更好的技术,专门为选择元素而设计,但算法的复杂度必须没有本质上的差异.而且我可能会这样改写:(名字和头像更少,速度更快)

And Heike (黑客?) adopted Pick which may have better techniques especially designed for selecting elements, but there must be no virtual difference in the complexity of the algorithm. And I may rewrite it this way: (fewer names and heads, faster the speed)

maxLast[l_] := Pick[l, #, Max[#]] &[Last /@ l]

他们都是很好的答案.

这篇关于按最后一个元素从列表列表中选择最大一个的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆