📅  最后修改于: 2023-12-03 14:46:32.747000             🧑  作者: Mango
Python是一种高级编程语言,通常用于快速开发和繁琐任务自动化。在Python中,有不同的方法来查找最大的3个数字,以下是其中一些方法。
numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
numbers.sort(reverse=True)
print(numbers[0:3])
输出:[100, 90, 80]
import heapq
numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
largest_numbers = heapq.nlargest(3, numbers)
print(largest_numbers)
输出:[100, 90, 80]
numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
def three_largest_numbers(numbers):
max1 = max(numbers)
numbers.remove(max1)
max2 = max(numbers)
numbers.remove(max2)
max3 = max(numbers)
return [max1, max2, max3]
print(three_largest_numbers(numbers))
输出:[100, 90, 80]
以上就是三种常见的Python方法来查找最大的3个数字的方法。