0

I have spent numerous hours on this and still can't see what is wrong with my while loop.

i  = 0
while(i < points):
    trainingset[i][0] = firstx[i]
    trainingset[i][1] = firsty[i]
    trainingset[i][2] = 1
    
    i+= 1

print(trainingset)

I want the while loop to loop over every array inside trainingsetsuch that trainingset[0] = [firstx[0], firsty[0], 1] and trainingset[points-1] = [firstx[points-1], firsty[points-1], 1].

However, the code seems to loop over ever subarray of trainingset for each iteration of i, such that at the end trainingset[0] = [firstx[points-1], firsty[points-1], 1]and so does every single subarray of trainingset.

What is wrong with this while loop?

P.S firstx and firsty are 1d arrrays like [1,2,3] and points is a number.

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
jbg05
  • 51
  • 7
  • Did you initialize `trainingset` with something like `trainingset = [[0,0,0] * points]`? It seems that you initialized `trainingset` to hold many references to the exact same list. See [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/q/240178/11082165) – Brian61354270 Apr 08 '22 at 02:02
  • As a side note, a `while` loop is generally a poor choice for any sort of index-based or sequential iteration. At very least `for i in range(points)` or `for example_list in trainingset` would be more conventional. But in this case your best choice would probably be a simple list compression: `trainingset = [[x,y,1] for x, y in zip(firstx, firsty)]` – Brian61354270 Apr 08 '22 at 02:07
  • Thank you for that Brian! I did initialize the training set like that. For next time how should I initialize it? – jbg05 Apr 08 '22 at 02:17
  • But Brian, how would I do such a list compression if I want the first `point` subarrays to be `[[x,y,1] for x, y in zip(firstx, firsty)]` and the next `point` subarrays to be `[[x,y,1] for x, y in zip(secondx, secondy)]` – jbg05 Apr 08 '22 at 02:20
  • It's very rare that you ever need to initialize a collection in python. Usually it's simpler (and safer) to just append entries as needed. Either using a list compression as in my comment above or initializing trainingset as a empty list and using `trainingset.append([..., ..., ...])` as required would likely be your best options. – Brian61354270 Apr 08 '22 at 02:23
  • Sounds like you're looking for something like `[[x,y,1] for xs, ys in [(firstx, firsty), (secondx, secondy), ...] for x,y in zip(xs, ys)]`, or something altogether simpler depending on how `firstx`, `secondx`, etc were obtained in the first place. If you only need to iterate `trainingset` once, you might also just use `itertools.chain` to consecutively iterate over each of `firstx`, `secondx`, etc in one go. – Brian61354270 Apr 08 '22 at 02:27
  • So in that case what would xs and ys be? – jbg05 Apr 08 '22 at 02:33
  • See [Explanation of how nested list comprehension works?](https://stackoverflow.com/q/20639180/11082165). `(xs, ys)` takes on `(firstx, firsty)` in the first iteration, `(secondx, secondy)` in the second, and so on. – Brian61354270 Apr 08 '22 at 02:39

0 Answers0