Shuffle list values into sublists of 19 values each

Question:

I have a large list of around 200 values

The list looks like this

list_ids =  [10148,
 10149,
 10150,
 10151,
 10152,
 10153,
 10154,
 10155,
 10156,
 10157,
 10158,
 10159,
 10160,
 10161,
 10163,
 10164,
 10165,
 10167,
 10168,
 10169,
 10170,
 10171,
 10172,
 10173,
 10174,
 10175,
 10177,
 10178,
 10179,
 10180,
 10181,
 10182,
 10183,
 7137,
 7138,
 7139,
 7142,
 7143,
 7148,
 7150,
 7151,
 7152,
 7153,
 7155,
 7156,
 7157,
 9086,
 9087,
 9088,
 9089,
 9090,
 9091,
 9094,
 9095,
 9096,
 9097,
 2164]

I would like to shuffle this list and create a sublist of 19 values for each sublist.

I tried :

list_ids.sort(key=lambda list_ids, r={b: random.random() for a, b in list_ids}: r[list_ids[1]])

But it didnt work. Looks like I am missing something.

End result is a sublist with shuffled values containing 19 values each

Asked By: LivingstoneM

||

Answers:

Convert to pandas series and get a sample of size 19:

import pandas as pd

ids = pd.Series(list_ids)
ids.sample(19).values

for random numbers between 0 and 1:

import random
random.shuffle(list_ids)
result = {}
for i in list_ids:
    result[i] = [random.random() for x in range(19)]
result

for random numbers from the original list:

import random
random.shuffle(list_ids)
result = {}
for i in list_ids:
    result[i] = [ids.sample(19).values]
result
Answered By: StephanT

you can shuffle the list with random.shuffle:

import random

# shuffles list in place
random.shuffle(list_ids)

#split into lists containg 19 elements
splits = list([list_ids[i:i+19] for i in range(0,len(list_ids),19)])
Answered By: Runinho
import random

s = 19
random.shuffle(list_ids)
sub_lists = [list_ids[s*i:s*(i+1)] for i in range(len(list_ids) // s)]
Answered By: DMcC
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.