0
loss=[[  -137.70171527 -81408.95809899 -94508.84395371   -311.81198933 -294.08711874]]

When I print loss it prints the addition of the numbers and not the individual numbers. I want to change this to a list so I can iterate over each individual number bit and I don't know how, please help.

I have tried:

result = map(tuple,loss)

However it prints the addition of the inside. When I try to index it says there is only 1 element. It works if I put a comma in between but this is a matrix that is outputed from other codes so i can't change or add to it.

John
  • 1,012
  • 14
  • 22
sebastian
  • 11
  • 2
  • https://stackoverflow.com/questions/36042645/accessing-rows-of-an-array-inside-an-array-of-arrays refer above for accessing element of array inside array. – svj Sep 26 '19 at 08:45
  • python recognizes it as subtracting 5 numbers, as result you are getting one number : -137.70171527 - 81408.95809899 - 94508.84395371 - 311.81198933 -294.08711874 = -176661.40287604 – ncica Sep 26 '19 at 08:48

1 Answers1

0

You have a list of a list with numbers the outer list contains exactly one element (namely the inner list). The inner list is your list of integers over which you want to iterate. Hence to iterate over the inner list you would first have to access it from the outer list using indices for example like this:

for list_item in loss[0]:
    do_something_for_each_element(list_item)

Moreover, I think that you wanted to have separate elements in the inner list and not compute one single number, didn't you? If that is the case you have to separate each element using a ,. E.g.:

loss=[[-137.70171527, -81408.95809899, -94508.84395371,   -311.81198933, -294.08711874]]

EDIT:

As you clarified in the comments you want to iterate over a numpy matrix. One way to do so is by converting the matrix into an n dimensional array (ndarray) and the iterate that structure. This could for example look like this, other options have also been presented in this answer(Iterate over a numpy Matrix rows):

import numpy as np
test_matrix=np.matrix([[1, 2], [3, 4]])
for row in test_matrix.A:
  print(row)

note that the A attribute of a matrix object is its ndarray representation (https://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html).

Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
  • is there a way to add commas automatically through code or is the only way to add it manually. ? beacuse that is the ouput to another set of code, so is there a way to automatically add the commas? – sebastian Sep 26 '19 at 08:55
  • Could you also give the other set of code? I'm sure there is a way to add them automatically – Sebastian Walla Sep 26 '19 at 09:17
  • def objective(x): """Objective function to minimize""" # Create the polynomial object x0=x['set1'] x1=x['set2'] x2=x['set3'] x3=x['set4'] x4=x['set5'] x5=x['set6'] test = np.matrix([x0, x1, x2, x3, x4, x5]) print(test) when i print test it returns it in the form [[ 0.06 0.8 4.17 0.03 0.32 23.38]] – sebastian Sep 26 '19 at 09:22
  • Ah that is just the way numpy prints matrices. Do you only get the input as string or would you also have access to the value of test? – Sebastian Walla Sep 26 '19 at 15:44
  • i have access to test aswell – sebastian Sep 27 '19 at 06:30
  • And the respective content of `x['set1']` is `0.06`? – Sebastian Walla Oct 04 '19 at 07:04