[英]Python: How to find the second highest number in a list?
def second_highest(list):
""" (list of int) -> int
你如何在不使用刪除、彈出或排序(我嘗試過)的情況下從整數列表中找到第二大值,因為我稍后需要使用相同的列表?
不會有重復的數字。
list.sort()
return list[-2]
我嘗試使用 max 刪除最高數字,對列表進行排序,但由於這些改變了列表,我無法使用它們。
使用內置sorted
oy mylist
,它不會修改mylist
(感謝@Tofystedeth)
mylist = [1, 2, 8, 3, 12]
print(sorted(mylist, reverse=True)[1])
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))
arr = [2, 3, 4, 2, 4, -3, 43, -4, -25, 45, 9]
my_list = list(set(arr))
my_list.sort()
if len(my_list) == 1:
print(my_list[0])
elif len(my_list) >= 2:
print(my_list[-2])
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)
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)
這是在不使用任何內置函數的情況下查找列表中第二大數字的代碼。
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)
我認為我的答案對初學者來說更簡單,更易讀
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)
##即使數字為負數,這也會起作用。 邏輯是:將 list() 轉換為 set() 並返回到 list 以使用 sort() 方法,然后打印列表中的 -2 位置值。 這為您提供了第二高的價值。## "Python 3"
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().split()))
z = list(set(arr))
z.sort()
print(z[-2])
這是一種使用排序的方法。 但是,它使您有可能重用列表,因為您暫時將列表arr存儲在sortedArr中。 單獨調用arr將返回原始列表。 在這里您可以在線試用!
# Finding a runner up number in a List of Arrays
# Second highest number in a list
arr = [2,3,6,6,5]
sortedArr = sorted(arr,reverse=True) # Sorting the array in descending order.
highest = sortedArr[0] # Assign the highest value in the array to the variable `highest`.
secondHighest = 0 # Initializing the variable `secondHighest` to 0.
for x in (sortedArr): # Iterating through the sorted array and checking if the value is equal to the highest value.
if(x == highest):
continue # If it is, it will continue to the next value.
else:
secondHighest = x # If it is not, it will assign the value to the variable `secondHighest`
break # break out of the loop.
print(secondHighest) # Printing the value of the variable `secondHighest`.
>>> 5
list = [2,3,5,1,7,8]
secondhighest = max([i for i in list if i<max(a)])
這將為您提供列表中第二高的值。
將唯一列表元素復制到另一個列表(如果列表元素已經是唯一的,請轉到步驟 2)。
您應該在列表中找到最大值並保存其索引。 然后使用remove()
函數將其從列表中remove()
,然后找到新列表的最大值(刪除原始最大值),這將是您的第二大元素。 然后,您可以使用insert()
方法將原始最大值重新添加到列表中。
這是在列表或數組(python)中查找最大和第二個最大數字的代碼。 嘗試了所有元素的組合(負面和正面),它工作正常。 我們可以優化此代碼,因為我沒有使用任何內置方法。 以下是此代碼測試的列表組合:la = [1,1,1,1,1],la = [1,1,1,1,1,0],la = [5,4,1, 2,3],la = [3,6,2,7],la = [1,2,3,4,5],la = [4,5,1,2,3],la = [5, 5,4,3,2,1],la = [-1,1,0,0,0],la = [-1,-2,-3,-4], la = [-1,0] ,la = [-2,-2,-2,-2,0,1,1,1,1]
def findSecmax(la):
mx = la[0]
sec = 0 # or sec = min(la) or we can take (- sys.maxsize)
for i in la:
if i < sec:
sec = i
for i in la:
if i > mx:
mx = i
for i in la:
if i > sec and mx > i:
sec = i
if sec == mx:
return "we have same elements in the list. So max is {}".format(mx)
else:
return mx,sec
print(findSecmax(la))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.