如何找到列表中最大值的所有位置?
我有一个列表:
a = [32, 37, 28, 30, 37, 25, 27, 24, 35, 55, 23, 31, 55, 21, 40, 18, 50,
35, 41, 49, 37, 19, 40, 41, 31]
最大元素是 55(位置 9 和 12 上的两个元素)
我需要找到最大值位于哪个位置。请帮忙。
I have a list:
a = [32, 37, 28, 30, 37, 25, 27, 24, 35, 55, 23, 31, 55, 21, 40, 18, 50,
35, 41, 49, 37, 19, 40, 41, 31]
max element is 55 (two elements on position 9 and 12)
I need to find on which position(s) the maximum value is situated. Please, help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
会告诉你列表
a
中最大值元素的第一个实例的索引。will tell you the index of the first instance of the largest valued element of list
a
.所选答案(以及大多数其他答案)需要至少两次遍历列表。
这是一个一次性解决方案,对于较长的列表可能是更好的选择。
编辑:解决@John Machin 指出的两个缺陷。对于(2),我尝试根据每个条件发生的估计概率和前人允许的推论来优化测试。确定适用于所有可能情况的
max_val
和max_indices
的正确初始化值有点棘手,特别是如果最大值恰好是列表中的第一个值 -但我相信现在确实如此。The chosen answer (and most others) require at least two passes through the list.
Here's a one pass solution which might be a better choice for longer lists.
Edited: To address the two deficiencies pointed out by @John Machin. For (2) I attempted to optimize the tests based on guesstimated probability of occurrence of each condition and inferences allowed from predecessors. It was a little tricky figuring out the proper initialization values for
max_val
andmax_indices
which worked for all possible cases, especially if the max happened to be the first value in the list — but I believe it now does.我想出了以下内容,它的工作原理正如您在以下列表中使用
max
、min
和其他函数所看到的那样:因此,请考虑下一个示例列表,找出列表中最大值的位置
a
:使用生成器
枚举
并进行转换此时,我们可以提取 max 的位置
上面告诉我们,最大值在位置 4 处,他的值为 5。
如你所见,在
key
参数中,你可以通过定义适当的 lambda 来找到任何可迭代对象的最大值。我希望它有所贡献。
PD:正如@PaulOyster 在评论中指出的那样。在
Python 3.x
中,min
和max
允许使用新关键字default
来避免引发异常当参数为空列表时出现 ValueError
。max(enumerate(list), key=(lambda x:x[1]), default = -1)
I came up with the following and it works as you can see with
max
,min
and others functions over lists like these:So, please consider the next example list find out the position of the maximum in the list
a
:Using the generator
enumerate
and making a castingAt this point, we can extract the position of max with
The above tells us, the maximum is in the position 4 and his value is 5.
As you see, in the
key
argument, you can find the maximum over any iterable object by defining a lambda appropriate.I hope that it contributes.
PD: As @PaulOyster noted in a comment. With
Python 3.x
themin
andmax
allow a new keyworddefault
that avoid the raise exceptionValueError
when argument is empty list.max(enumerate(list), key=(lambda x:x[1]), default = -1)
另外,还可以使用 numpy 来实现只给出第一次出现的解决方案:
Also a solution, which gives only the first appearance, can be achieved by using
numpy
:我无法重现@martineau 引用的击败@SilentGhost 的表演。以下是我的比较结果:
=== maxelements.py ===
来自一台在 Windows XP SP3 上运行 Python 2.7 的破旧笔记本电脑的结果:
I can't reproduce the @SilentGhost-beating performance quoted by @martineau. Here's my effort with comparisons:
=== maxelements.py ===
Results from a beat-up old laptop running Python 2.7 on Windows XP SP3:
您还可以使用 numpy 包:
这将返回包含最大值的所有索引的 numpy 数组:
如果您想将其转换为列表,
You can also use the numpy package:
This will return an numpy array of all the indices that contain the max value
if you want to turn this to a list:
我通常就是这样做的。
That is how I usually do it.
@shash 在其他地方回答了这个问题
Which does one pass。然而,它比 @Silent_Ghost 的解决方案慢,甚至比 @nmichaels 的解决方案慢:
@shash answered this elsewhere
Which does one pass. Yet, it is slower than the solution by @Silent_Ghost and, even more so, @nmichaels:
只需一行:
Just one line:
这是最大值及其出现的索引:
稍后:为了满足@SilentGhost
Here is the max value and the indexes it appears at:
Later: for the satisfaction of @SilentGhost
与列表理解类似的想法,但没有枚举
Similar idea with a list comprehension but without enumerate
如果您想获取名为
data
的列表中最大的n
个数字的索引,可以使用 Pandassort_values
:If you want to get the indices of the largest
n
numbers in a list calleddata
, you can use Pandassort_values
:这是一个简单的单遍解决方案。
Here's a simple single-pass solution.
此代码并不像之前发布的答案那么复杂,但它会起作用:
上面代码中的 ilist 将包含列表中最大数字的所有位置。
This code is not as sophisticated as the answers posted earlier but it will work:
ilist in the above code would contain all the positions of the maximum number in the list.
您可以通过多种方式做到这一点。
旧的常规方法是,
另一种无需计算列表长度并将最大值存储到任何变量的方法,
我们可以用 Pythonic 和智能方式来完成!仅在一行中使用列表理解,
我的所有代码都在 Python 3 中。
You can do it in various ways.
The old conventional way is,
Another way without calculating the length of the list and storing maximum value to any variable,
We can do it in Pythonic and smart way! Using list comprehension just in one line,
All my codes are in Python 3.