Python:如何找到列表中第二高的数字?
如果不使用remove、pop或sort(我尝试过),如何从整数列表中找到第二高的值,因为我以后需要使用相同的列表 数字不会重复Python:如何找到列表中第二高的数字?,python,Python,如果不使用remove、pop或sort(我尝试过),如何从整数列表中找到第二高的值,因为我以后需要使用相同的列表 数字不会重复 def second_highest(list): """ (list of int) -> int 我试着用max删除最大的数字,对列表进行排序,但由于这些数字会改变列表,我无法使用它们。使用内置的排序的oymylist,它不会修改mylist(感谢@Tofystedeth) 将唯一列表元素复制到另一个列表(如果列表元素已经是唯一的,请转至步骤2) 您应该在
def second_highest(list):
""" (list of int) -> int
我试着用max删除最大的数字,对列表进行排序,但由于这些数字会改变列表,我无法使用它们。使用内置的
排序的oymylist
,它不会修改mylist
(感谢@Tofystedeth)
将唯一列表元素复制到另一个列表(如果列表元素已经是唯一的,请转至步骤2)
您应该在列表中找到最大值并保存其索引。然后使用remove()
函数将其从列表中删除,然后找到新列表的最大值(原始最大值已删除),这将是第二高的元素。然后可以使用insert()
方法将原始最大值添加回列表中
下面是在不使用任何内置函数的情况下查找列表中第二大数字的代码
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)
我认为我的答案更简单,对初学者来说更具可读性
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)
你真的应该展示一些代码,这里没有人会帮你,但是你知道如何找到最大的元素吗?如果你这样做了,你可以用同样的方法来做这个问题,但是为第二大元素添加另一个变量。你似乎忘记在你的问题中包含一个问题。这听起来像是家庭作业。你试过什么?给我们看一些代码。先从概念上看问题:要知道第二高的数字,你首先要知道什么?可能是最高的数字。你怎么发现的?他没有提到他不想使用任何排序吗?排序已经返回了一个新列表。在mylist本身而不是mylist[:]上使用它应该是安全的。mylist.sort()将是实际修改mylist的函数。若要处理重复值,请使用set(mylist)调用将mylist转换为set,并将其传递给排序函数。OP写了“…而不使用remove…”并使用remove
”发布答案。trincot尝试此代码,它不会改变列表。op要求提供不改变列表的代码。我同意它不会改变列表。所以发布此代码可以吗?或者我应该删除它吗?它可以保留。我会将remove(max(arr))
替换为remove(z)
,以避免不必要的额外遍历。如果你能解释你的代码,最好只做1次遍历,跟踪2个最大数
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])
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)
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)
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)