1

I am a beginner in Python. I am having difficulty creating multidimensional array or list.

I want to do two things:

a) Create a list, b) Intialize the list

For example, if D is list then:

The total matrix is till D[8] [7] but I need to initialize only below values to 0

D[0][0] = 0

D[0][1] = 0
D[0][2] = 0
D[0][3] = 0
D[0][4] = 0

D[1][0] = 0
D[2][0] = 0
D[3][0] = 0
D[4][0] = 0
D[5][0] = 0

I tried following but its not working:

import numpy
Matrix = numpy.zeros((len(A) + 1, len(B) + 1))
    for i in range(len(B) + 1):
        Matrix[0][i] = i

    for i in range(len(A) + 1):
        Matrix[i][0] = i

But it gives output something like below which I don't want:

[[0. 1. 2. 3. 4. 5. 6. 7. 8.]
 [1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [2. 0. 0. 0. 0. 0. 0. 0. 0.]
 [3. 0. 0. 0. 0. 0. 0. 0. 0.]
 [4. 0. 0. 0. 0. 0. 0. 0. 0.]
 [5. 0. 0. 0. 0. 0. 0. 0. 0.]
 [6. 0. 0. 0. 0. 0. 0. 0. 0.]
 [7. 0. 0. 0. 0. 0. 0. 0. 0.]]
D555
  • 1,704
  • 6
  • 26
  • 48
  • 1
    What you want is done when you ran `Matrix = numpy.zeros((len(A) + 1, len(B) + 1))`. Now, `Matrix` is a multidimensional list with all zeros – Anwarvic May 31 '20 at 14:50

1 Answers1

0

if you are using numpy then You can do your both work together with "shape" feature as it define and initialise with 0 as well

import numpy
a = numpy.zeros(shape=(8,7))

or for the general case

a = numpy.zeros(shape=(i,j))
classicdude7
  • 903
  • 1
  • 6
  • 23
  • 3
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – SherylHohman May 31 '20 at 18:37
  • 1
    @SherylHohman thanks for the feedback, I will keep that in mind. – classicdude7 Jun 01 '20 at 04:46