Python程序在列表中查找最大和最小元素的位置?
在python中也很容易找出最大,最小元素及其位置。Python提供了不同的内置函数。min()用于在数组中查找最小值,max()用于在数组中查找最大值。index()用于查找元素的索引。
算法
maxminposition(A, n)
/* A is a user input list and n is the size of the list.*/
Step 1: use inbuilt function for finding the position of minimum element.
A.index(min(A))
Step 2: use inbuilt function for finding the position of a maximum element.
A.index(max(A))范例程式码
# Function to find minimum and maximum position in list
def maxminposition(A, n):
# inbuilt function to find the position of minimum
minposition = A.index(min(A))
# inbuilt function to find the position of maximum
maxposition = A.index(max(A))
print ("The maximum is at position::", maxposition + 1)
print ("The minimum is at position::", minposition + 1)
# Driver code
A=list()
n=int(input("Enter the size of the List ::"))
print("Enter the Element ::")
for i in range(int(n)):
k=int(input(""))
A.append(k)
maxminposition(A,n)输出结果
Enter the size of the List ::4 Enter the Element:: 12 34 1 66 The maximum is at position:: 4 The minimum is at position:: 3
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便