Idea from my friend, who wonder about how to generating 2 random list from a list of person. Original blog could be found here.
-
-
Save neizod/1872945 to your computer and use it in GitHub Desktop.
Random Separate Element into 2 Lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group_2 = [group_1.pop(random.randint(0, len(group_1)-1)) for i in group_1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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