0
import numpy as np

nparray = np.zeros((5,5))

L1 = list()
L2 = []
L3 = [3,4,1,6,7,5]
L4 = [[2, 9, -5], [-1, 0, 4], [3, 1, 2]]

print(L3[0])

a = np.asarray(L4)
print(a)
for (x,y), value in np.ndenumerate(a):
  print(x,y)

Output is

3
[[ 2  9 -5]
 [-1  0  4]
 [ 3  1  2]]
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2

I have no idea where 0 0/0 1/02...2/2 is coming from

My assignment is to find the mean of all the #'s in L4. I am having trouble iterating the list.

The only hint that was given is "Simplest solution makes use of NumPy"

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
software is fun
  • 7,286
  • 18
  • 71
  • 129
  • 1
    What do you think `print(x,y)` *should* be doing? – Scott Hunter Mar 02 '20 at 16:41
  • 1
    Why are you using ndenumerate? – OneCricketeer Mar 02 '20 at 16:42
  • Try `print(x, y, value)` and see if you understand. – Guy Mar 02 '20 at 16:42
  • You may find it helpful to read the [documentation for np.ndenumerate](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndenumerate.html). – Chris Mueller Mar 02 '20 at 16:45
  • 1
    You may find it more helpful to ignore `npdenumerate`. – Scott Hunter Mar 02 '20 at 16:48
  • I just want to iterate the MD array. I am trying to learn Python and the "solution" just hands over the answer on how to perform the homework.. I thought print(x,y) was getting the values from my array. – software is fun Mar 02 '20 at 16:51
  • @softwareisfun _I just want to iterate the MD array._ (I'm guessing you mean ndarray) If you find yourself mostly iterating over a ndarray, it might be a sign that you're better off using a plain Python list. _I thought print(x,y) was getting the values from my array._ Here `x` and `y` are the coordinates of the current value. – AMC Mar 02 '20 at 17:15

0 Answers0