Skip to content

Instantly share code, notes, and snippets.

@neizod
Forked from Python1Liners/LICENSE.txt
Created February 21, 2012 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neizod/1872945 to your computer and use it in GitHub Desktop.
Save neizod/1872945 to your computer and use it in GitHub Desktop.
Random Separate Element into 2 Lists
group_2 = [group_1.pop(random.randint(0, len(group_1)-1)) for i in group_1]

Random Separate Element into 2 Lists

Idea from my friend, who wonder about how to generating 2 random list from a list of person. Original blog could be found here.

import random
group_1 = ['some', 'one', 'like', 'you']
group_2 = [group_1.pop(random.randint(0, len(group_1)-1)) # element from group_1 also removed by pop().
for i in group_1] # size of group_1 is decreasing each loop,
# this make group_2 stop adding new element
# when it's size larger or equal to group_1.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Nattawut Phetmak <http://about.me/neizod>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "random_separate_element_into_2_lists",
"description": "from 1 non-empty list, random it's element and separate into 2 lists.",
"keywords": [
"random",
"list"
]
}
import random
group_1 = ['some', 'one', 'like', 'you']
group_2 = [group_1.pop(random.randint(0, len(group_1)-1)) for i in group_1]
# /!\ caution: random
# some possible output:
# ['some', 'you'] ['one', 'like']
# ['like', 'you'] ['some', 'one']
print(group_1, group_2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment