Jason
Jason

Reputation: 105

Append groups of random values to a list (Python)

I am trying to generate a list of values that append another list as parameter in the order shown below in the 'parameters' list.

How may I go about appending 'parameters' to 'vector' with random values upon each iteration of the for loop?

e.g. Desired output appending 'parameters' twice to 'vector' [ 2, 14000, 120, 1, 12000, 80]

def generate_vector(self):
      parameters = [lambda:random.randint(0, 3), lambda:random.randint(0, 400000), lambda:random.randint(0, 128)]
      vector = []
      path, dirs, files = os.walk("templ_list/").next()
      file_count = len(files)

      for file in files:
          vector.append(parameters)
      return vector,

Upvotes: 1

Views: 119

Answers (1)

pask
pask

Reputation: 927

You can create a generator for your Parameters and flatten the list with sum.

params_gen =  ((random.randint(0, 3), random.randint(0, 400000), random.randint(0, 128)) for _ in range(len(files)))
vector = sum(params_gen, ())

Upvotes: 1

Related Questions