Home > OS >  Function to generate arrays with given choices Python
Function to generate arrays with given choices Python

Time:10-26

I want to output something like: [array([-1, 1, -1]), array([-1, 1, 1])] etc. 100 times. I have been editing my code without success for invalid syntax.

test = []
i =1
def testdata():
    while i<= 100:
        new = [random.choices([-1,1], k=3) for _ in range(100)]
        test.append(new) 
        np.array(n) for n in new.values()[:3]
        i  = 1
        return new

CodePudding user response:

import random as r
c = [-1, 1]
[np.array([r.choice(c), r.choice(c), r.choice(c)]) for _ in range(100)]

CodePudding user response:

return np.array(random.choices([-1, 1], k=3*100)).reshape(100, 3)
  • Related