Get user keyboard input through the input() function, use split(',') to separate the input into a list by commas, and finally use np.array() to convert the list into a NumPy array.
Using Python to input an array from the keyboard
How to use Python to input an array from the keyboard?
Using Python's input()
function, you can get an array of user input from the keyboard.
Detailed steps:
numpy
Library: import numpy as np
input()
Get the user's input: user_input = input("请输入数组元素,用逗号分隔:")
user_list = user_input.split(',')
numpy.array()
Convert list to NumPy array: array = np.array(user_list, dtype=int)
Sample code:
import numpy as np user_input = input("请输入数组元素,用逗号分隔:") user_list = user_input.split(',') array = np.array(user_list, dtype=int) print(array)
Output:
<code>[1 2 3 4 5]</code>
The above is the detailed content of How to enter an array from the keyboard in Python. For more information, please follow other related articles on the PHP Chinese website!