Python:如何在列表中找到第二高的数字?

时间:2015-11-02 20:15:54

标签: python

def second_highest(list):
""" (list of int) -> int

如何在不使用删除,弹出或排序(我尝试过)的情况下从整数列表中找到第二高的值,因为我需要稍后使用相同的列表? 没有重复的数字。

我尝试使用max删除最高数字,对列表进行排序,但由于这些会改变列表,我无法使用它们。

离。

list.sort()
return list[-2]

提前谢谢!

8 个答案:

答案 0 :(得分:6)

使用内置sorted oy mylist,不会修改mylist(感谢@Tofystedeth)

mylist = [1, 2, 8, 3, 12]
print(sorted(mylist, reverse=True)[1])

答案 1 :(得分:3)

data = [1,2,8,3,12]

largest = None
second_largest = None

for a in data:
    if not largest or a > largest:
        if largest:
            second_largest = largest
        largest = a

print("largest: {}".format(largest))
print("second_largest: {}".format(second_largest))

答案 2 :(得分:1)

<configuration>
  <appSettings>
    <add key="WSGI_HANDLER" value="myApp.wsgi.application"/>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot" />
    <add key="DJANGO_SETTINGS_MODULE" value="myApp.settings" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <add name="PythonHandler" path="handler.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\python362x86\python.exe|D:\home\python362x86\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
    <rewrite>
      <rules>
        <rule name="Static Files" stopProcessing="true">
          <conditions>
            <add input="true" pattern="false" />
          </conditions>
        </rule>
        <rule name="Configure Python" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

答案 3 :(得分:1)

nums=[1,2,3,3,5,4,5,5,2]

#making new set to maintain uniqueness
new_list= set(nums)

high= max(nums)
new_list.remove(high)
print(max(new_list))

#This code uses remove but does not do any changes to the given list
print(nums)

答案 4 :(得分:0)

array = [2, 3, 4, 2, 4, -3, 43, -4, -25, 45, 9]

arr = array.copy()

z = max(arr)

# if list contains duplicate max elements removing them until We get Second highest

while max(arr) == z:
  arr.remove(max(arr))

print(max(arr))
print(array)

答案 5 :(得分:0)

这是在不使用任何内置函数的情况下查找列表中第二大数字的代码。

data = [11,22,1,2,5,67,21,32]

max1 = data[0] # largest num
max2 = data[1] # second largest num


for num in data:
    if num > max1:
        max2 = max1 # Now this number would be second largest
        max1 = num # This num is largest number in list now.
    
    # Check with second largest
    elif num > max2:
        max2 = num # Now this would be second largest.

print(max2)

答案 6 :(得分:0)

我认为我的回答对初学者来说更简单易读

x=[2,3,4,2,5,9,8,4,5,6,7,8,9,2,1,3,4,5]

max=-10000000
for i in x:
    if(i>max):
        secondmax=max
        max=i
    elif(i>secondmax and i!=max):
        secondmax=i
    
        
print(secondmax)   
    

答案 7 :(得分:-1)

  1. 将唯一列表元素复制到另一个列表(如果列表元素已经是唯一的,请转到步骤2)。

  2. 您应该在列表中找到最大值并保存其索引。然后使用remove()函数将其从列表中删除,然后找到新列表的最大值(删除原始最大值),这将是您的第二高元素。然后,您可以使用insert()方法将原始最大值重新添加回列表中。