Python程序从列表中查找N个最大元素
给定一个整数列表,我们的任务是在列表中找到N个最大的元素。
示例
Input : [40, 5, 10, 20, 9] N = 2 Output: [40, 20]
算法
Step1: Input an integer list and the number of largest number. Step2: First traverse the list up to N times. Step3: Each traverse find the largest value and store it in a new list.
示例
def Nnumberele(list1, N):
new_list = []
for i in range(0, N):
max1 = 0
for j in range(len(list1)):
if list1[j] > max1:
max1 = list1[j];
list1.remove(max1);
new_list.append(max1)
print("Largest numbers are ",new_list)
# Driver code
my_list = [12, 61, 41, 85, 40, 13, 77, 65, 100]
N = 4
# Calling the function
Nnumberele(my_list, N)输出结果
Largest numbers are [100, 85, 77, 65]
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬