Python Find the Minimum Value in a List and Its Position
In Python, we can use the built-in min() function to find the minimum value in the list. To find the position of the minimum value, we can use the index() method. The following is an example showing how to find the minimum value in a list and its position.
Example
Define a list
numbers
=
[
34
,
12
,
56
,
78
,
23
,
9
]
Find the minimum value
min_value
=
min
(
numbers
)
Find the position of the minimum value
min_index
=
numbers.
index
(
min_value
)
Output result
(
f
"The minimum value in the list is: {min_value}"
)
(
f
"The position of the minimum value is: {min_index}"
)
Code analysis:
numbers = [34, 12, 56, 78, 23, 9]: Define a list containing multiple integers.min_value = min(numbers): When usingmin()function to find the minimum value in the list and assign it tomin_value。min_index = numbers.index(min_value): When usingindex()method to find the position of the minimum value in the list and assign it tomin_index。print(f"列表中的最小值是: {min_value}"): Output the minimum value.print(f"最小值的位置是: {min_index}"): Output the position of the minimum value.
Output result:
列表中的最小值是: 9
最小值的位置是: 5
Python3 Examples