'How to make various list using random.sample()? [closed]
in this list
AIB13seoul= ['김예나','김혜관','노주연','박진수','박희선','양건희','양세비',
'이예지','전형준','정승기','sangwon','이지현','김강호','김슬기',
'김용석','김재성','방준원','한유성','한현구','강병우']
I want to make 3 other random lists called a, b, and c without duplicates, using loop.
I'm not used to utilizing loops, so this is how I tried originally:
import random
a = random.sample(AIB13seoul, 7)
list(a)
removed=list(set(AIB13seoul) - set(a))
b = random.sample(removed, 7)
removed1=list(set(removed) - set(b))
c = random.sample(removed1, 6)
And this is the file I'm using: off-site file
I tried this:
a=[]
for x in AIB13seoul:
select=random.sample(AIB13seoul,7)
a.append(select)
print(a)
but it didn't work .
Solution 1:[1]
You could write a fairly flexible implementation with numpy and random:
import random as rand
import numpy as np
def split(list, subdivisions):
l = rand.sample(list, len(list))
splits = [int(len(l) * split) for split in np.linspace(0, 1, subdivisions + 1)]
return [ l[splits[i] : splits[i+1]] for i in range(len(splits) - 1)]
split(AIB13seoul, 3)
Which results in:
> [['???', '???', '???', '???', '???', '???'],
['???', '???', '???', '???', '???', '???', '???'],
['???', '???', '???', '???', 'sangwon', '???', '???']]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Neervana |
