这是我用来查找列表中第三大元素而不使用任何内置函数(例如max,sort,len)的代码。
list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
#list = [35,10,45,9,8,5]
largest_1 = list[0]
largest_2 = list[0]
largest_3 = list[0]
print (largest_1)
print (largest_2)
print (largest_3)
for each in list:
print ('Each element Before if Loop --->',each)
if each > largest_1:
print ('Each element inside if loop --->',each)
largest_1 = each
print('largest_1 element---->',largest_1)
elif largest_2 != largest_1 and largest_2 < each:
print ('Each element inside if loop --->',each)
largest_2 = each
print ('Largest_1 element is ---->',largest_1)
print ('Largest_2 element is ---->',largest_2)
elif largest_3 != largest_2 and largest_3 < each:
print ('Each element inside if loop --->',each)
largest_3 = each
print ('Largest_2 element is ---->',largest_2)
print ('Largest_3 element is ---->',largest_3)
print (largest_1)
print (largest_2)
print (largest_3)
相同的代码不适用于
list = [35,10,45,9,8,5]
我没有犯什么错误。我该如何解决?
答案 0 :(得分:3)
@venkat:这是另一项建议,可在不使用len()
和sort()
的情况下从列表中获取第三大数字。
def find_largest(alist):
"""
Find the largest number in a list.
Return the largest number found and it index
"""
largest = alist[0]
for item in alist[1:]:
if item > largest:
largest = item
idx = alist.index(largest)
return (idx, largest)
#--
def get_third_largest(alist):
"""
Return the third largest number in a list.
"""
# Let make a copy of the input list so that any change to it may not affect the
# original data.
thisList = alist.copy()
index, largest = 0, 0
for item in range(3):
index, largest = find_largest(thisList)
if item != 2:
# delete the first two largest from the List
del thisList[index]
return largest
# Test of the algorithm
if __name__ == "__main__":
List = [12, 45, 2, 41, 31, 10, 8, 6, 4]
third = get_third_largest(List)
# print("Initial list: ", List)
# print("The third largest item in the list:")
print("\tExpected: 31")
print("\tResult: %d" % third);
# --- Output---
# Expected: 31
# Result: 31
答案 1 :(得分:1)
我不知道您为什么不想使用len()
或max()
-它们实际上是内置函数,不是任何库的一部分,并且没有实际原因使用它们。就是说,如果您真的想这样做,这是另一种方法:
采用三个变量,分别给它们分配largest
,second_largest
和third_largest
,然后遍历列表。
largest = 0
second_largest = 0
third_largest = 0
for each in list:
if each >= largest:
# assign the new largest, and push the rest of them back down the chain
# we use >= instead of > to ensure that duplicate maximums still work.
#
largest, second_largest, third_largest = each, largest, second_largest
elif each >= second_largest:
second_largest, third_largest = each, second_largest
elif each > third_largest:
third_largest = each
print(third_largest)
答案 2 :(得分:0)
您还可以将前3个最大数字存储在字典中,然后打印出第三大数字:
largest = {"first": 0, "second": 0, "third": 0}
lst = [12, 45, 2, 41, 31, 10, 8, 6, 4]
for number in lst:
if number > largest["first"]:
largest["third"] = largest["second"]
largest["second"] = largest["first"]
largest["first"] = number
elif number > largest["second"]:
largest["third"] = largest["second"]
largest["second"] = number
elif number > largest["third"]:
largest["third"] = number
print(largest)
# {'first': 45, 'second': 41, 'third': 31}
print(largest["third"])
# 31