Python Find the Minimum Value in a List and Its Position
In Python, we can use the built-inmin()function to find the minimum value in a list. To find the position of the minimum value, we can use theindex()method. The following is an example code that shows 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 the result
print(f"The minimum value in the list is: {min_value}")
print(f"The position of the minimum value is: {min_index}")
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 the result
print(f"The minimum value in the list is: {min_value}")
print(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): Use themin()function to find the minimum value in the list, and assign it tomin_value。min_index = numbers.index(min_value): Use theindex()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 最小值的位置是: 5Other extensions
Python3 Examples