本文介绍了如何在列表中查找多个最大值项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图找出如何获取整数列表并将该列表中的所有项目返回到另一个列表作为其最大值,但是使用它们的索引。

I'm trying to figure out how I can take a list of integers and return all the items in that list into another list as their max value, but with their index.

所以我需要能够在不使用枚举,lambda,numpy或任何类似的东西的情况下执行此操作。它必须是列表的基本方法。基本上,像append,max等。 。
如果声明也很好。

So I need to be able to do this without using enumeration, lambda, numpy, or anything of that sort. It has to be really basic methods for lists. Basically, things like append, max, etc. . .If for statements are fine too.

为了澄清我想要做的事情,请说我有一个清单: [4,34,0,0,6,34, 1]
我希望它返回 [1,5]

To clarify what I'm trying to do, say I have a list: [4, 34, 0, 0, 6, 34, 1]I want it to return [1, 5]

推荐答案

最简单的方法:

in [24]: a = [4, 34, 0, 0, 6, 34, 1]

In [25]: j=0

In [26]: M=[]

In [27]: m = max(a)

In [28]: for i in a:
    if i==m:
        M.append(j)
    j+=1
   ....:

In [29]: M
Out[29]: [1, 5]

使用list-comprehension和enumerate,上面的内容可缩短为:

Using list-comprehension and enumerate, the above can be shortened to:

In [30]: [i for i, x in enumerate(a) if x == max(a)]
Out[30]: [1, 5]

这篇关于如何在列表中查找多个最大值项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:21