Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to find the second largest value of list

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge about "how to use Python to find the second largest value of list". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Title: There is an lst, no duplicate data, its content is:

lst = [1,4,2,77,24,57,42]

This time you are not asked to find the largest value, but the second largest value, requiring that you can only traverse the list once, and that the size range of the elements in lst is uncertain.

topic analysis

The list can only be iterated once, which means that you cannot sort first, because the sorting process iterates more than once

What we're looking for is the second largest value.

Looking back at the way to find the maximum

lst = [1,4,2,77,24,57,42]

max = lst[0]

for item in lst:

if item > max:

max = item

print max

To find the maximum value, first define a max, its value is lst[0], compare one by one in the process of traversal, then find the second largest value and not on this basis.

Define a second, also make its value lst[0], also compare one by one in the traversal process, if item is larger than second, at the same time smaller than max can not be it!

sample code

lst = [1,4,2,77,24,57,42]

max = lst[0]

second = lst[0]

for item in lst:

if item > max:

max = item

if item > second and item

< max: second = item print max,second 程序的输出结果为 77 57 ,程序关键之处在于比较item和second的大小时,也考虑到了max的情况,这样很简单嘛,一点都不烧脑,???? 不烧脑,我怎么敢拿出来呢! 请考虑这种情况: lst = [77,4,2,24,57,42] lst[0] 本身就是最大值,上面的代码还能给出正确的结果么?不能,程序运行的结果是 77 77 ,剧情翻转了,由于lst[0]是最大值,导致两个if条件语句都不成立,现在感觉到烧脑了么? 之所以程序不能给出正确结果,皆因max 和 second在最初赋值的时候相等,这是问题的根源,那么只需要让second的初始值比max小便能解决这个问题 示例代码 lst = [77,1,4,2,24,57,42] max = lst[0] if lst[0] >

lst[1]:

second = lst[1]

else:

second = lst[0]

for item in lst:

if item > max:

max = item

if item > second and item < max:

second = item

print max,second

"How to use Python to find the second largest value in the list" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report