samedi 15 janvier 2022

Randomly Crop a Given Input Sequence in Python?

I want to create a function that takes in a input sequence/list of given length

(ex: [48083, 50118, 50118, 39631, 5868, 452, 32, 460, 15, 49, 1028, 4, 252, 32, 460, 15, 49, 1028, 55, 87, 195, 722, 10, 183, 117, 912, 479, 3684, 51, 109, 16, 2788, 124, 8, 556, 8, 95, 33, 333, 732, 2923, 15, 592, 433, 4.])

and the function will output a random seqeunce of given length say 5 from the input

(ex: [48083, 50118, 50118, 39631, 5868] or [479, 3684, 51, 109, 16])

It would basically look something like this -

def foo(x, len):
  return ...

x = [48083, 50118, 50118, 39631, 5868, 452, 32, 460, 15, 49]
output_seq = foo(x, 5) # [39631, 5868, 452, 32, 460]
output_seq = foo(x, 5) # [452, 32, 460, 15, 49]
output_seq = foo(x, 5) # [50118, 50118, 39631, 5868, 452]

Can this be done in python3x ? Any help would be appreciated ?




Aucun commentaire:

Enregistrer un commentaire