1

I would like to save to data.tsv each random list of rewards however the file just shows the final shuffle for all of the rows. I can see with print that the rewards are being shuffled but the output file isn't showing this. Does anyone know what I'm doing wrong?

```import random, numpy
data=[]
rewards = [1,2,3,4]
print(rewards)
data.append([1, rewards])
random.shuffle(rewards)
print(rewards)
data.append([2, rewards])
random.shuffle(rewards)
print(rewards)
data.append([3, rewards])

numpy.savetxt("data.tsv", data, fmt="%s")```
C:\Users\randr\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\core\_asarray.py:83: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
[4, 3, 1, 2]
  return array(a, dtype, copy=False, order=order)
[1, 2, 4, 3]

Process finished with exit code 0```



data.tsv
1 [1, 2, 4, 3]
2 [1, 2, 4, 3]
3 [1, 2, 4, 3]
marmite
  • 23
  • 4
  • You add a reference of `rewards` to a list `data` - three times. References point all to the same underlying data. You need to actually copy the randomized data. See dupe. In this case doing `data.append([1, rewards[:]])` (etc.) should work. – Patrick Artner Nov 06 '20 at 11:39
  • Ah I see that makes sense. Thank you very much. – marmite Nov 06 '20 at 11:52

0 Answers0