リスト内の最大要素と最小要素の位置を見つける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
-
リスト内で2番目に大きい数を見つけるPythonプログラム
この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 −リストが与えられたので、リストの2番目に大きい番号を表示する必要があります。 問題を解決するための3つのアプローチがあります- アプローチ1-set()関数とremove()関数を使用します 例 list1 = [11,22,1,2,5,67,21,32] # to get unique elements new_list = set(list1) # removing the largest element from list1 new_list.remove(max(new_list)) # now
-
リスト内で最大、最小、2番目に大きい、2番目に小さいものを見つけるPythonプログラム?
配列が与えられたら、最大、最小、2番目に大きい、2番目に小さい数を見つける必要があります。 アルゴリズム Step 1: input list element Step 2: we take a number and compare it with all other number present in the list. Step 3: get maximum, minimum, secondlargest, second smallest number. サンプルコード # To find largest, smallest, second largest and second small