0

I used Jupyter notebook, I am new to Python, I try to fetch value from user in multidimensional array how I do that? I write a little code, after put first value I get error that I don't understand

Error:

Traceback (most recent call last)
   <ipython-input-64-4d8986a5e412> in <module>
          5 for i in range(lengthrow):
          6     for j in range(lengthcol):
    ----> 7         arr[i][j]=int(input("enter value"))
          8 print(arr)

IndexError: index 0 is out of bounds for axis 0 with size 0

code:

from numpy import*
arr = array([[],[]])
lengthrow=int(input("enter array row length"))
lengthcol=int(input("enter array col length"))
for i in range(lengthrow):
    for j in range(lengthcol):
        arr[i][j]=int(input("enter value"))
print(arr)
1
  • You made arr a (2,0) shape array. arr[0] is a (0,) shape array. Trying to index that with [0] produces the error. Commented Feb 12, 2019 at 17:09

3 Answers 3

1

I took @Austin great answer and made some little changes:

import numpy as np

n_rows = int(input("Enter number of rows: "))
n_cols = int(input("Enter number of columns: "))

arr = [[int(input("Enter value for {}. row and {}. column: ".format(r + 1, c + 1))) for c in range(n_cols)] for r in range(n_rows)]

print(np.array(arr))

The output is:

Enter number of rows: 2
Enter number of columns: 3
Enter value for 1. row and 1. column: 1
Enter value for 1. row and 2. column: 2
Enter value for 1. row and 3. column: 3
Enter value for 2. row and 1. column: 4
Enter value for 2. row and 2. column: 5
Enter value for 2. row and 3. column: 6
[[1 2 3]
 [4 5 6]]

You got an exception, because you initialized an empty array and used invalid indices. With this answer you generate the array after you have entered the users input.


Here is the long version of the one-liner (arr = [[...) which gives you the same result:

outer_arr = []
for r in range(n_rows):
    inner_arr = []
    for c in range(n_cols):
        num = int(input("Enter value for {}. row and {}. column: ".format(r + 1, c + 1)))
        inner_arr.append(num)
    outer_arr.append(inner_arr)

print(np.array(outer_arr))
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Darius Morawec, this method as also great actually I need this output thanks a lot.Can you suggest some reference to learn two dimensional array I need to deeply learn about it.Thank you :)
Sure, multidimensional arrays are matrices in linear algebra, which allows you do to powerful operations. Here are some introductions: #1, #2, #3 and #4.
Again Thanks a lot @Darius Morawiec :)
Can you explain this line [[int(input("Enter value for {}. row and {}. column: ".format(r + 1, c + 1))) for c in range(n_cols)] for r in range(n_rows)] i don't really understand how flow of for loop
@ToufikKhan Sure, I edited the answer and added a longer version of the same code. We use two for loops to create a list of lists which we convert to a two-dimensional array.
1

You can use a list-comprehension here as the final result is a list of lists:

lengthrow = int(input("enter array row length: "))
lengthcol = int(input("enter array col length: "))

arr = [[int(input("enter value: ")) for _ in range(lengthcol)] for _ in range(lengthrow)]

print(arr)

Problem with your code:

arr = array([[],[]])
print(arr.shape)
# (2, 0)

That means you have a size 0 column in array, so when you do arr[0][0], for example, it throws error.

Comments

0

The problem is the shape of the initial array:

In [1]: arr = np.array([[], []])
In [2]: arr
Out[2]: array([], shape=(2, 0), dtype=float64)
In [3]: arr[0]
Out[3]: array([], dtype=float64)
In [4]: arr[0][0]
... 
IndexError: index 0 is out of bounds for axis 0 with size 0

Think of this array as 2 rows, 0 columns. You can't reference a non-existent column. And, in contrast to some other languages, you can't grow an array simply by referencing a new index. Once created the size is fixed.

While others have shown nice list comprehension approaches, I'll show how to use your approach correctly:

In [8]: lengthrow, lengthcol = 2,3    # or user input
In [9]: arr = np.zeros((lengthrow, lengthcol), dtype=int)  # large enough array
In [10]: arr
Out[10]: 
array([[0, 0, 0],
       [0, 0, 0]])
In [11]: for i in range(2):
    ...:     for j in range(3):
    ...:         arr[i,j] = int(input(f'{i},{j}:'))
    ...:         
0,0:0
0,1:1
0,2:2
1,0:3
1,1:4
1,2:5
In [12]: arr
Out[12]: 
array([[0, 1, 2],
       [3, 4, 5]])

2 Comments

Thanks for your valuable replay, this salutation is really good. once again thank you :)
np.array() function convert the list to numpy array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.