Python 程序:查找列表中的第二大数字

写一个 Python 程序,用一个实际例子找出列表中的第二大数字。

Python 程序在列表中查找第二大数字示例 1

这个 python 程序允许用户输入长度。接下来,我们使用 For 循环在 Python 中向列表中添加数字。

python 中的排序函数以升序对列表元素进行排序。接下来,我们使用 Python 索引位置来打印列表中的最后一个元素。

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

NumList.sort()

print("The Largest Element in this List is : ", NumList[Number - 2])

Python 程序在列表中查找第二大数字示例 2

这个程序按照升序对元素进行排序。接下来,我们使用反转功能来反转列表项。最后,我们使用索引位置 1 来打印列表中的第二个元素。

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

NumList.sort()
NumList.reverse()
print("The Largest Element in this List is : ", NumList[1])
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 20
Please enter the Value of 2 Element : 56
Please enter the Value of 3 Element : 78
Please enter the Value of 4 Element : 97
Please enter the Value of 5 Element : 60
The Largest Element in this List is :  78

Python 程序在列表中查找第二大数字示例 3

在这个程序中,我们没有使用任何内置功能,比如排序或者反转功能。为此,我们使用For Loop

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))

for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

first = second = NumList[0]
for j in range(1, Number):
    if(NumList[j] > first):
        second = first
        first = NumList[j]
    elif(NumList[j] > second and NumList[j] < first):
        second = NumList[j]

print("The Largest Element in this List is : ", first)
print("The Second Largest Element in this List is : ", second)
Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 55
Please enter the Value of 2 Element : 57
Please enter the Value of 3 Element : 22
Please enter the Value of 4 Element : 3
The Largest Element in this List is :  57
The Second Largest Element in this List is :  55

从上面的 Python 程序返回列表截图中的第二大数字,可以观察到用户插入的值是

NumList[4] = {55,57,22,3}
第一个=第二个= NumList[0] = 55

第一次迭代–对于范围(1,4)中的 1–条件为真
因此,它开始在循环内执行 If 语句,直到条件失败。
如果 for 循环内的(NumList[j] > first)为 True,因为(57>55)
first = first = 55
first = NumList[1]= 57

第二次迭代:对于范围(1,4)中的 2–条件为真
如果(NumList[2] >优先)=(22>57)–条件为假。因此,它进入 elif 语句
elif(NumList[2] >第二,NumList[2] <第一)
elif(22 > 55 和 22<57)–条件为假

第三次迭代:对于范围(1,4)中的 3–条件为真
如果(3>57)–条件为假
elif(3 > 55 和 3<57)–条件为假

第四次迭代:对于范围(1,4)中的 4–条件为假。所以,它从循环中退出。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/297336.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-28 12:15
下一篇 2024-12-28 12:15

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python循环符合要求数字求和

    这篇文章将详细介绍如何通过Python循环符合要求数字求和。如果你想用Python求和但又不想手动输入数字,那么本文将是一个不错的选择。 一、使用while循环实现求和 sum =…

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • Python购物车程序

    Python购物车程序是一款基于Python编程语言开发的程序,可以实现购物车的相关功能,包括商品的添加、购买、删除、统计等。 一、添加商品 添加商品是购物车程序的基础功能之一,用…

    编程 2025-04-29
  • 爬虫是一种程序

    爬虫是一种程序,用于自动获取互联网上的信息。本文将从如下多个方面对爬虫的意义、运行方式、应用场景和技术要点等进行详细的阐述。 一、爬虫的意义 1、获取信息:爬虫可以自动获取互联网上…

    编程 2025-04-29
  • Python基本数字类型

    本文将介绍Python中基本数字类型,包括整型、布尔型、浮点型、复数型,并提供相应的代码示例以便读者更好的理解。 一、整型 整型即整数类型,Python中的整型没有大小限制,所以可…

    编程 2025-04-29
  • Vb运行程序的三种方法

    VB是一种非常实用的编程工具,它可以被用于开发各种不同的应用程序,从简单的计算器到更复杂的商业软件。在VB中,有许多不同的方法可以运行程序,包括编译器、发布程序以及命令行。在本文中…

    编程 2025-04-29
  • Python列表中大于某数的元素处理方法

    本文将会介绍如何在Python列表中找到大于某数的元素,并对其进行进一步的处理。 一、查找大于某数的元素 要查找Python列表中大于某数的元素,可以使用列表推导式进行处理。 nu…

    编程 2025-04-29

发表回复

登录后才能评论