Python 查找列表中最大的两个数字

Python 查找列表中最大的两个数字,python,Python,我必须在列表中找到第二大数字。我在下面编写了一个程序,当输入第一次遇到第二大数字时,该程序可以正常运行。如果输入的第一个数字最大,则结果是该最大数字的两倍 #pr no:78 #11/06/2020 # find out the 2nd biggest element in list using list a=[] x=int(input("value of x ")) while x!=1000: a.append(x) x=int(input("value of x "))

我必须在列表中找到第二大数字。我在下面编写了一个程序,当输入第一次遇到第二大数字时,该程序可以正常运行。如果输入的第一个数字最大,则结果是该最大数字的两倍

#pr no:78
#11/06/2020
# find out the 2nd biggest element in list using list
a=[]
x=int(input("value of x "))
while x!=1000:
    a.append(x)
    x=int(input("value of x "))
n=len(a)
i=0
big=a[0]
while i<n:
    if a[i]>big:
        big=a[i]
    i+=1
print("first big number",big)
i=0
secondbig=a[0]
while i<n:
    if a[i]!=big:
        if a[i]>secondbig:
            secondbig=a[i]
    i+=1
print ("second big number",secondbig)
如果我给出的第一个数字很小,它会告诉我正确的答案

2st输出:

value of x 100
value of x 5000
value of x 200
value of x 1000
first big number 5000
second big number 200

Process finished with exit code 0
value of x 5000
value of x 200
value of x 100
value of x 1000
first big number 5000
second big number 5000

Process finished with exit code 0
可以使用.sort()按升序对数组进行排序

numbers = [7,3,8,1,6,4] #Here, we have to find 7, which is the second biggest number.
numbers.sort() #This arranges the numbers in ascending order.
print(numbers[-2]) #This prints the second last number or largest number in the list.

请发布您的代码和输出,而不是图片请在此处咨询有关如何提问的提示。此外,在第二次发布问题之前,请先进行搜索,同时循环将if更改为
a[i]
,无需其他选项,您可以使用单个
if
条件
if[i]!=大和a[i]