[英]Finding third largest element in the list without using len() and sort()
Here is the code which I used to find the third largest element in the list without using any built in functions like max,sort,len. 这是我用来查找列表中第三大元素而不使用任何内置函数(例如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)
The same code is not working for the 相同的代码不适用于
list = [35,10,45,9,8,5]
I am not getting what mistake I have done. 我没有犯什么错误。 How can I fix this?
我怎样才能解决这个问题?
@venkat: Here another proposal to get the third largest number out of your list without using len()
and sort()
. @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
I don't know why you wouldn't want to use len()
, or max()
- they're literally built-in functions, not part of any library, and there's no practical reason not to use them. 我不知道您为什么不想使用
len()
或max()
-它们实际上是内置函数,不是任何库的一部分,并且没有实际的理由不使用它们。 That said, if you really want to do it otherwise, here's another approach: 就是说,如果您真的想这样做,这是另一种方法:
Take three variables, assign them largest
, second_largest
, and third_largest
, and walk through the list. 以三个变量,给它们
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)
You could also store your top 3 max numbers in a dictionary, then print out the third largest one: 您还可以将前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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.