How to make various list using random.sample()?

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 .

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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', '전형준', '김예나']]


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x