Populate a list with missing members of a sequence
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I have a collection of lists containing random elements of a sequence, say integers from 0 to 3.
sequence=0., 1., 2., 3.;
Table[RandomSample[sequence, RandomInteger[1, 4]], 5]
3., 0., 1., 3., 1., 0., 2., 1., 3., 1., 3., 0., 2., 0., 2.,
3.
How do I populate the lists so that the missing sequence members are filled?
3., 0., 1., 2., 3., 1., 0., 2., 1., 3., 0., 2., 1., 3., 0.,
2., 0., 2., 3., 1.
Note: answers where elements are Integer
s are fine but I'm also looking for answers which work for Real
elements.
list-manipulation
add a comment |Â
up vote
3
down vote
favorite
I have a collection of lists containing random elements of a sequence, say integers from 0 to 3.
sequence=0., 1., 2., 3.;
Table[RandomSample[sequence, RandomInteger[1, 4]], 5]
3., 0., 1., 3., 1., 0., 2., 1., 3., 1., 3., 0., 2., 0., 2.,
3.
How do I populate the lists so that the missing sequence members are filled?
3., 0., 1., 2., 3., 1., 0., 2., 1., 3., 0., 2., 1., 3., 0.,
2., 0., 2., 3., 1.
Note: answers where elements are Integer
s are fine but I'm also looking for answers which work for Real
elements.
list-manipulation
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a collection of lists containing random elements of a sequence, say integers from 0 to 3.
sequence=0., 1., 2., 3.;
Table[RandomSample[sequence, RandomInteger[1, 4]], 5]
3., 0., 1., 3., 1., 0., 2., 1., 3., 1., 3., 0., 2., 0., 2.,
3.
How do I populate the lists so that the missing sequence members are filled?
3., 0., 1., 2., 3., 1., 0., 2., 1., 3., 0., 2., 1., 3., 0.,
2., 0., 2., 3., 1.
Note: answers where elements are Integer
s are fine but I'm also looking for answers which work for Real
elements.
list-manipulation
I have a collection of lists containing random elements of a sequence, say integers from 0 to 3.
sequence=0., 1., 2., 3.;
Table[RandomSample[sequence, RandomInteger[1, 4]], 5]
3., 0., 1., 3., 1., 0., 2., 1., 3., 1., 3., 0., 2., 0., 2.,
3.
How do I populate the lists so that the missing sequence members are filled?
3., 0., 1., 2., 3., 1., 0., 2., 1., 3., 0., 2., 1., 3., 0.,
2., 0., 2., 3., 1.
Note: answers where elements are Integer
s are fine but I'm also looking for answers which work for Real
elements.
list-manipulation
list-manipulation
asked Aug 23 at 17:14
Musang
7341618
7341618
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
7
down vote
accepted
This should work for any kind of list, not just numeric ones :)
sequence = 0., 1., 2., 3.;
list = Table[RandomSample[sequence, RandomInteger[1, 4]], 5]
2., 0., 0., 3., 2., 0., 3., 1., 1., 0., 2., 3.
#~Join~Complement[sequence, #] & /@ list
2., 0., 1., 3., 0., 3., 1., 2., 2., 0., 1., 3., 0., 3., 1.,
2., 1., 0., 2., 3.
1
I could have sworn I looked for thatComplement
function in the docs... Nice one-liner!
â Musang
Aug 23 at 19:26
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
This should work for any kind of list, not just numeric ones :)
sequence = 0., 1., 2., 3.;
list = Table[RandomSample[sequence, RandomInteger[1, 4]], 5]
2., 0., 0., 3., 2., 0., 3., 1., 1., 0., 2., 3.
#~Join~Complement[sequence, #] & /@ list
2., 0., 1., 3., 0., 3., 1., 2., 2., 0., 1., 3., 0., 3., 1.,
2., 1., 0., 2., 3.
1
I could have sworn I looked for thatComplement
function in the docs... Nice one-liner!
â Musang
Aug 23 at 19:26
add a comment |Â
up vote
7
down vote
accepted
This should work for any kind of list, not just numeric ones :)
sequence = 0., 1., 2., 3.;
list = Table[RandomSample[sequence, RandomInteger[1, 4]], 5]
2., 0., 0., 3., 2., 0., 3., 1., 1., 0., 2., 3.
#~Join~Complement[sequence, #] & /@ list
2., 0., 1., 3., 0., 3., 1., 2., 2., 0., 1., 3., 0., 3., 1.,
2., 1., 0., 2., 3.
1
I could have sworn I looked for thatComplement
function in the docs... Nice one-liner!
â Musang
Aug 23 at 19:26
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
This should work for any kind of list, not just numeric ones :)
sequence = 0., 1., 2., 3.;
list = Table[RandomSample[sequence, RandomInteger[1, 4]], 5]
2., 0., 0., 3., 2., 0., 3., 1., 1., 0., 2., 3.
#~Join~Complement[sequence, #] & /@ list
2., 0., 1., 3., 0., 3., 1., 2., 2., 0., 1., 3., 0., 3., 1.,
2., 1., 0., 2., 3.
This should work for any kind of list, not just numeric ones :)
sequence = 0., 1., 2., 3.;
list = Table[RandomSample[sequence, RandomInteger[1, 4]], 5]
2., 0., 0., 3., 2., 0., 3., 1., 1., 0., 2., 3.
#~Join~Complement[sequence, #] & /@ list
2., 0., 1., 3., 0., 3., 1., 2., 2., 0., 1., 3., 0., 3., 1.,
2., 1., 0., 2., 3.
answered Aug 23 at 17:18
Fraccalo
2,219517
2,219517
1
I could have sworn I looked for thatComplement
function in the docs... Nice one-liner!
â Musang
Aug 23 at 19:26
add a comment |Â
1
I could have sworn I looked for thatComplement
function in the docs... Nice one-liner!
â Musang
Aug 23 at 19:26
1
1
I could have sworn I looked for that
Complement
function in the docs... Nice one-liner!â Musang
Aug 23 at 19:26
I could have sworn I looked for that
Complement
function in the docs... Nice one-liner!â Musang
Aug 23 at 19:26
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180528%2fpopulate-a-list-with-missing-members-of-a-sequence%23new-answer', 'question_page');
);
Post as a guest
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
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
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