Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to Create Array of zeros using Numpy in Python
Next article icon

NumPy is a powerful library in Python used for numerical computing. It provides an efficient way to work with arrays making operations on large datasets faster and easier. To take input for arrays in NumPy, you can use numpy.array.

Taking Array Input Using numpy.array()

The most simple way to create aNumPy array is by converting a list of inputs into an array. It works well for scenarios where all elements are provided at once in a single line.

Python
importnumpyasnp# Take input for the array as a space-separated stringval=input("Enter elements separated by space: ").split()# Convert input elements to a NumPy array# Convert to integer array, change dtype as neededarr=np.array(val,dtype=int)print(arr)

Output:

Enter elements separated by space: 1 2 3 4 5
[1 2 3 4 5]

Explanation: Here, we first take input as a space-separated string split it into a list and then use numpy.array() to convert it into a NumPy array.

Let's explore other methods to create a NumPy array from user input:

Using List Comprehension with NumPy

We can also uselist comprehension to create a NumPy array from user input. We can provide prompts for each input which makes it suitable for user-friendly input handling.

Python
importnumpyasnp# Take input for the number of elementsn=int(input("Enter the number of elements: "))# Use list comprehension to collect elementsval=[int(input(f"Enter element{i+1}: "))foriinrange(n)]# Convert to NumPy arrayarr=np.array(val)print(arr)

Output:

Enter the number of elements: 6
Enter element 1: 23
Enter element 2: 45
Enter element 3: 21
Enter element 4: 45
Enter element 5: 23
Enter element 6: 12
[23 45 21 45 23 12]

Explanation:This code uses list comprehension to take n elements and then converts the resulting list into a NumPy array.

Using numpy.fromiter()

Another way to create a NumPy array is by usingnumpy.fromiter() which creates an array from an iterable. This method creates arrays directly without creating an intermediate list.

Python
importnumpyasnp# Take input as space-separated valuesval=input("Enter elements separated by space: ").split()# Use fromiter() to create a NumPy arrayarr=np.fromiter((int(x)forxinval),dtype=int)print(arr)

Output:

Enter elements separated by space: 12 18 87 83 86
[12 18 87 83 86]

Explanation: Here, we use a generator expression to create an iterable that numpy.fromiter() can convert to a NumPy array.

This method is suitable for processing large data inputs where conversion speed and efficiency matter.


Taking Input for Multi-dimensional Arrays

For multi-dimensional arrays, you can take input for each row separately and stack them.

Python
importnumpyasnp# Input number of rows and columnsrows=int(input("Enter the number of rows: "))cols=int(input("Enter the number of columns: "))# Empty list to hold the rowsdata=[]# Input each rowforiinrange(rows):row=list(map(int,input(f"Enter row{i+1} elements separated by spaces: ").split()))data.append(row)# Convert list of lists to NumPy arrayarr=np.array(data)print("2D NumPy Array:")print(arr)

Output:

Enter the number of rows: 2
Enter the number of columns: 3
Enter row 1 elements separated by spaces: 1 2 3
Enter row 2 elements separated by spaces: 4 5 6
2D NumPy Array:
[[1 2 3]
[4 5 6]]




Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp