Python: How do I randomly mix two lists?
If I generate two random lists for example:
N=5
X=20
parent1 = np.random.choice(X, N, replace=True)
parent2 = np.random.choice(X, N, replace=True)
Would give two lists e.g:
[2,5,1,1,12]
[3,18,4,5,1]
How could I make a new list which is a random mix of the two lists with the same amount of numbers?
e.g.
[2,18,1,5,12]
or
[18,5,1,12,5]
It doesn't matter about order.
python python-3.x list random genetic-algorithm
add a comment |
If I generate two random lists for example:
N=5
X=20
parent1 = np.random.choice(X, N, replace=True)
parent2 = np.random.choice(X, N, replace=True)
Would give two lists e.g:
[2,5,1,1,12]
[3,18,4,5,1]
How could I make a new list which is a random mix of the two lists with the same amount of numbers?
e.g.
[2,18,1,5,12]
or
[18,5,1,12,5]
It doesn't matter about order.
python python-3.x list random genetic-algorithm
Is it allowed to pick same number multiple times?
– Krishna
Nov 22 '18 at 2:03
random.sample(a+b, N)
would work?
– bro-grammer
Nov 22 '18 at 2:06
In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,
– wwii
Nov 22 '18 at 2:07
Are the parents necessarily the same length?
– Mark_Anderson
Nov 22 '18 at 2:25
add a comment |
If I generate two random lists for example:
N=5
X=20
parent1 = np.random.choice(X, N, replace=True)
parent2 = np.random.choice(X, N, replace=True)
Would give two lists e.g:
[2,5,1,1,12]
[3,18,4,5,1]
How could I make a new list which is a random mix of the two lists with the same amount of numbers?
e.g.
[2,18,1,5,12]
or
[18,5,1,12,5]
It doesn't matter about order.
python python-3.x list random genetic-algorithm
If I generate two random lists for example:
N=5
X=20
parent1 = np.random.choice(X, N, replace=True)
parent2 = np.random.choice(X, N, replace=True)
Would give two lists e.g:
[2,5,1,1,12]
[3,18,4,5,1]
How could I make a new list which is a random mix of the two lists with the same amount of numbers?
e.g.
[2,18,1,5,12]
or
[18,5,1,12,5]
It doesn't matter about order.
python python-3.x list random genetic-algorithm
python python-3.x list random genetic-algorithm
asked Nov 22 '18 at 2:00
P erryP erry
455
455
Is it allowed to pick same number multiple times?
– Krishna
Nov 22 '18 at 2:03
random.sample(a+b, N)
would work?
– bro-grammer
Nov 22 '18 at 2:06
In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,
– wwii
Nov 22 '18 at 2:07
Are the parents necessarily the same length?
– Mark_Anderson
Nov 22 '18 at 2:25
add a comment |
Is it allowed to pick same number multiple times?
– Krishna
Nov 22 '18 at 2:03
random.sample(a+b, N)
would work?
– bro-grammer
Nov 22 '18 at 2:06
In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,
– wwii
Nov 22 '18 at 2:07
Are the parents necessarily the same length?
– Mark_Anderson
Nov 22 '18 at 2:25
Is it allowed to pick same number multiple times?
– Krishna
Nov 22 '18 at 2:03
Is it allowed to pick same number multiple times?
– Krishna
Nov 22 '18 at 2:03
random.sample(a+b, N)
would work?– bro-grammer
Nov 22 '18 at 2:06
random.sample(a+b, N)
would work?– bro-grammer
Nov 22 '18 at 2:06
In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,
– wwii
Nov 22 '18 at 2:07
In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,
– wwii
Nov 22 '18 at 2:07
Are the parents necessarily the same length?
– Mark_Anderson
Nov 22 '18 at 2:25
Are the parents necessarily the same length?
– Mark_Anderson
Nov 22 '18 at 2:25
add a comment |
4 Answers
4
active
oldest
votes
Continuing your example, you could simply try this:
result = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)
Wether to sample with replacement or not is your choice (argument replace
).
parent1+parent2
will add corresponding elements of two arrays!
– bro-grammer
Nov 22 '18 at 2:11
@bro-grammer You're right. Forgot thatparent1
andparent2
were arrays and not lists. Fixed it by concatenating them.
– normanius
Nov 22 '18 at 2:16
Brill thanks very much!
– P erry
Nov 22 '18 at 2:29
add a comment |
if you are given parent1
and parent2
, you can use np.choose
:
which_one = np.random.int(2, size=N).astype(np.bool)
out_array = np.choose(which_one, [parent1, parent2])
however, keep in mind that if parent1
and parent2
are both random as per in your code, then you could simply do
out_array = np.random.choice(X, N, replace=True)
add a comment |
A few ways to get a uniform random selection from 2 lists.
- Random 0 or 1 to pick list, then chose randomly in list.
- Random number up to the sum of the lists, then use logic to chose from the first or second list according to that number.
.
import random
#### Option 1
for i in range(len(parent1)):
list_number = random.randint(1,3)
output(i) = exec('np.random.choice(parent'+str(list_number)+')')
#### Option 2
output_list = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)
The former represents each list equally in the output regardless of their relative length, the latter samples according to the relative length of the lists.
Option 2 is so simple and exactly what I was looking for. Thanks! :)
– P erry
Nov 22 '18 at 2:28
What does "replace=False" actually mean?
– P erry
Nov 22 '18 at 2:28
When repeatedly picking from the list[parent1, parent2]
, do you "replace" the numbers you've picked (such that they can be picked again), or not?
– Mark_Anderson
Nov 22 '18 at 2:34
No same number can be used twice unless there is a duplicate of the number
– P erry
Nov 22 '18 at 11:01
Sorry, that was my explanation ofReplace=False
. The code above does as you require.
– Mark_Anderson
Nov 22 '18 at 12:55
add a comment |
One way would be:
N=5
select_one = np.random.randint(0,2,N) # Outputs an array of 1 or 0s
mixed_list = select_one*parent1 + (1 - select_one)*parent2
If you also want to randomly permute the list elements, you can use:
mixed_list = np.random.permutation(rand_fact*parent1 + (1 - rand_fact)*parent2)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422887%2fpython-how-do-i-randomly-mix-two-lists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Continuing your example, you could simply try this:
result = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)
Wether to sample with replacement or not is your choice (argument replace
).
parent1+parent2
will add corresponding elements of two arrays!
– bro-grammer
Nov 22 '18 at 2:11
@bro-grammer You're right. Forgot thatparent1
andparent2
were arrays and not lists. Fixed it by concatenating them.
– normanius
Nov 22 '18 at 2:16
Brill thanks very much!
– P erry
Nov 22 '18 at 2:29
add a comment |
Continuing your example, you could simply try this:
result = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)
Wether to sample with replacement or not is your choice (argument replace
).
parent1+parent2
will add corresponding elements of two arrays!
– bro-grammer
Nov 22 '18 at 2:11
@bro-grammer You're right. Forgot thatparent1
andparent2
were arrays and not lists. Fixed it by concatenating them.
– normanius
Nov 22 '18 at 2:16
Brill thanks very much!
– P erry
Nov 22 '18 at 2:29
add a comment |
Continuing your example, you could simply try this:
result = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)
Wether to sample with replacement or not is your choice (argument replace
).
Continuing your example, you could simply try this:
result = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)
Wether to sample with replacement or not is your choice (argument replace
).
edited Nov 22 '18 at 2:31
answered Nov 22 '18 at 2:06
normaniusnormanius
1,4861129
1,4861129
parent1+parent2
will add corresponding elements of two arrays!
– bro-grammer
Nov 22 '18 at 2:11
@bro-grammer You're right. Forgot thatparent1
andparent2
were arrays and not lists. Fixed it by concatenating them.
– normanius
Nov 22 '18 at 2:16
Brill thanks very much!
– P erry
Nov 22 '18 at 2:29
add a comment |
parent1+parent2
will add corresponding elements of two arrays!
– bro-grammer
Nov 22 '18 at 2:11
@bro-grammer You're right. Forgot thatparent1
andparent2
were arrays and not lists. Fixed it by concatenating them.
– normanius
Nov 22 '18 at 2:16
Brill thanks very much!
– P erry
Nov 22 '18 at 2:29
parent1+parent2
will add corresponding elements of two arrays!– bro-grammer
Nov 22 '18 at 2:11
parent1+parent2
will add corresponding elements of two arrays!– bro-grammer
Nov 22 '18 at 2:11
@bro-grammer You're right. Forgot that
parent1
and parent2
were arrays and not lists. Fixed it by concatenating them.– normanius
Nov 22 '18 at 2:16
@bro-grammer You're right. Forgot that
parent1
and parent2
were arrays and not lists. Fixed it by concatenating them.– normanius
Nov 22 '18 at 2:16
Brill thanks very much!
– P erry
Nov 22 '18 at 2:29
Brill thanks very much!
– P erry
Nov 22 '18 at 2:29
add a comment |
if you are given parent1
and parent2
, you can use np.choose
:
which_one = np.random.int(2, size=N).astype(np.bool)
out_array = np.choose(which_one, [parent1, parent2])
however, keep in mind that if parent1
and parent2
are both random as per in your code, then you could simply do
out_array = np.random.choice(X, N, replace=True)
add a comment |
if you are given parent1
and parent2
, you can use np.choose
:
which_one = np.random.int(2, size=N).astype(np.bool)
out_array = np.choose(which_one, [parent1, parent2])
however, keep in mind that if parent1
and parent2
are both random as per in your code, then you could simply do
out_array = np.random.choice(X, N, replace=True)
add a comment |
if you are given parent1
and parent2
, you can use np.choose
:
which_one = np.random.int(2, size=N).astype(np.bool)
out_array = np.choose(which_one, [parent1, parent2])
however, keep in mind that if parent1
and parent2
are both random as per in your code, then you could simply do
out_array = np.random.choice(X, N, replace=True)
if you are given parent1
and parent2
, you can use np.choose
:
which_one = np.random.int(2, size=N).astype(np.bool)
out_array = np.choose(which_one, [parent1, parent2])
however, keep in mind that if parent1
and parent2
are both random as per in your code, then you could simply do
out_array = np.random.choice(X, N, replace=True)
answered Nov 22 '18 at 2:14
ealfieealfie
1
1
add a comment |
add a comment |
A few ways to get a uniform random selection from 2 lists.
- Random 0 or 1 to pick list, then chose randomly in list.
- Random number up to the sum of the lists, then use logic to chose from the first or second list according to that number.
.
import random
#### Option 1
for i in range(len(parent1)):
list_number = random.randint(1,3)
output(i) = exec('np.random.choice(parent'+str(list_number)+')')
#### Option 2
output_list = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)
The former represents each list equally in the output regardless of their relative length, the latter samples according to the relative length of the lists.
Option 2 is so simple and exactly what I was looking for. Thanks! :)
– P erry
Nov 22 '18 at 2:28
What does "replace=False" actually mean?
– P erry
Nov 22 '18 at 2:28
When repeatedly picking from the list[parent1, parent2]
, do you "replace" the numbers you've picked (such that they can be picked again), or not?
– Mark_Anderson
Nov 22 '18 at 2:34
No same number can be used twice unless there is a duplicate of the number
– P erry
Nov 22 '18 at 11:01
Sorry, that was my explanation ofReplace=False
. The code above does as you require.
– Mark_Anderson
Nov 22 '18 at 12:55
add a comment |
A few ways to get a uniform random selection from 2 lists.
- Random 0 or 1 to pick list, then chose randomly in list.
- Random number up to the sum of the lists, then use logic to chose from the first or second list according to that number.
.
import random
#### Option 1
for i in range(len(parent1)):
list_number = random.randint(1,3)
output(i) = exec('np.random.choice(parent'+str(list_number)+')')
#### Option 2
output_list = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)
The former represents each list equally in the output regardless of their relative length, the latter samples according to the relative length of the lists.
Option 2 is so simple and exactly what I was looking for. Thanks! :)
– P erry
Nov 22 '18 at 2:28
What does "replace=False" actually mean?
– P erry
Nov 22 '18 at 2:28
When repeatedly picking from the list[parent1, parent2]
, do you "replace" the numbers you've picked (such that they can be picked again), or not?
– Mark_Anderson
Nov 22 '18 at 2:34
No same number can be used twice unless there is a duplicate of the number
– P erry
Nov 22 '18 at 11:01
Sorry, that was my explanation ofReplace=False
. The code above does as you require.
– Mark_Anderson
Nov 22 '18 at 12:55
add a comment |
A few ways to get a uniform random selection from 2 lists.
- Random 0 or 1 to pick list, then chose randomly in list.
- Random number up to the sum of the lists, then use logic to chose from the first or second list according to that number.
.
import random
#### Option 1
for i in range(len(parent1)):
list_number = random.randint(1,3)
output(i) = exec('np.random.choice(parent'+str(list_number)+')')
#### Option 2
output_list = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)
The former represents each list equally in the output regardless of their relative length, the latter samples according to the relative length of the lists.
A few ways to get a uniform random selection from 2 lists.
- Random 0 or 1 to pick list, then chose randomly in list.
- Random number up to the sum of the lists, then use logic to chose from the first or second list according to that number.
.
import random
#### Option 1
for i in range(len(parent1)):
list_number = random.randint(1,3)
output(i) = exec('np.random.choice(parent'+str(list_number)+')')
#### Option 2
output_list = np.random.choice(np.concatenate([parent1,parent2]), N, replace=False)
The former represents each list equally in the output regardless of their relative length, the latter samples according to the relative length of the lists.
edited Nov 22 '18 at 2:18
answered Nov 22 '18 at 2:06
Mark_AndersonMark_Anderson
421216
421216
Option 2 is so simple and exactly what I was looking for. Thanks! :)
– P erry
Nov 22 '18 at 2:28
What does "replace=False" actually mean?
– P erry
Nov 22 '18 at 2:28
When repeatedly picking from the list[parent1, parent2]
, do you "replace" the numbers you've picked (such that they can be picked again), or not?
– Mark_Anderson
Nov 22 '18 at 2:34
No same number can be used twice unless there is a duplicate of the number
– P erry
Nov 22 '18 at 11:01
Sorry, that was my explanation ofReplace=False
. The code above does as you require.
– Mark_Anderson
Nov 22 '18 at 12:55
add a comment |
Option 2 is so simple and exactly what I was looking for. Thanks! :)
– P erry
Nov 22 '18 at 2:28
What does "replace=False" actually mean?
– P erry
Nov 22 '18 at 2:28
When repeatedly picking from the list[parent1, parent2]
, do you "replace" the numbers you've picked (such that they can be picked again), or not?
– Mark_Anderson
Nov 22 '18 at 2:34
No same number can be used twice unless there is a duplicate of the number
– P erry
Nov 22 '18 at 11:01
Sorry, that was my explanation ofReplace=False
. The code above does as you require.
– Mark_Anderson
Nov 22 '18 at 12:55
Option 2 is so simple and exactly what I was looking for. Thanks! :)
– P erry
Nov 22 '18 at 2:28
Option 2 is so simple and exactly what I was looking for. Thanks! :)
– P erry
Nov 22 '18 at 2:28
What does "replace=False" actually mean?
– P erry
Nov 22 '18 at 2:28
What does "replace=False" actually mean?
– P erry
Nov 22 '18 at 2:28
When repeatedly picking from the list
[parent1, parent2]
, do you "replace" the numbers you've picked (such that they can be picked again), or not?– Mark_Anderson
Nov 22 '18 at 2:34
When repeatedly picking from the list
[parent1, parent2]
, do you "replace" the numbers you've picked (such that they can be picked again), or not?– Mark_Anderson
Nov 22 '18 at 2:34
No same number can be used twice unless there is a duplicate of the number
– P erry
Nov 22 '18 at 11:01
No same number can be used twice unless there is a duplicate of the number
– P erry
Nov 22 '18 at 11:01
Sorry, that was my explanation of
Replace=False
. The code above does as you require.– Mark_Anderson
Nov 22 '18 at 12:55
Sorry, that was my explanation of
Replace=False
. The code above does as you require.– Mark_Anderson
Nov 22 '18 at 12:55
add a comment |
One way would be:
N=5
select_one = np.random.randint(0,2,N) # Outputs an array of 1 or 0s
mixed_list = select_one*parent1 + (1 - select_one)*parent2
If you also want to randomly permute the list elements, you can use:
mixed_list = np.random.permutation(rand_fact*parent1 + (1 - rand_fact)*parent2)
add a comment |
One way would be:
N=5
select_one = np.random.randint(0,2,N) # Outputs an array of 1 or 0s
mixed_list = select_one*parent1 + (1 - select_one)*parent2
If you also want to randomly permute the list elements, you can use:
mixed_list = np.random.permutation(rand_fact*parent1 + (1 - rand_fact)*parent2)
add a comment |
One way would be:
N=5
select_one = np.random.randint(0,2,N) # Outputs an array of 1 or 0s
mixed_list = select_one*parent1 + (1 - select_one)*parent2
If you also want to randomly permute the list elements, you can use:
mixed_list = np.random.permutation(rand_fact*parent1 + (1 - rand_fact)*parent2)
One way would be:
N=5
select_one = np.random.randint(0,2,N) # Outputs an array of 1 or 0s
mixed_list = select_one*parent1 + (1 - select_one)*parent2
If you also want to randomly permute the list elements, you can use:
mixed_list = np.random.permutation(rand_fact*parent1 + (1 - rand_fact)*parent2)
answered Nov 22 '18 at 2:20
RR_28023RR_28023
134
134
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422887%2fpython-how-do-i-randomly-mix-two-lists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is it allowed to pick same number multiple times?
– Krishna
Nov 22 '18 at 2:03
random.sample(a+b, N)
would work?– bro-grammer
Nov 22 '18 at 2:06
In a loop; randomly generate a one or a zero; if it is a one pick a random choice from the first list; else pick a random choice from the second list; stop when the item limit is reached,
– wwii
Nov 22 '18 at 2:07
Are the parents necessarily the same length?
– Mark_Anderson
Nov 22 '18 at 2:25