1

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,
1
  • 1
    Please note that the parameters are parsed into the templates using a different function and that depending on the number of files in the directory then the for loop will loop x amount of times e.g. 10 files = 10 loops Commented Mar 7, 2018 at 9:44

1 Answer 1

1

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, ())
Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I was looking for Pask. Thanks for your help dude :)
params_gen creates an array which looks like this: ((1, 1000, 64), (3, 1200, 34)) sum will then add the tuples resulting in: () + (1, 1000, 64) + (3, 1200, 34) = (1, 1000, 64, 3, 1200, 34)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.