Replacing the Zeros of One List by the (i-1)th Element of Another List
$begingroup$
My goal is to replace the zeros of one list with the (i-1)th element of a second list. For example, if
list1 = {0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0}
and
list2 = {6, 1, 4, 7, 2, 9, 10, 8, 11, 3, 5, 0, 12}
the desired output is {0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}. Note that the first element of the output is defined as 0 still.
My attempt to create a code for this is to first find the zeros of list1
then run a for-loop for $i in$zeroslist1
.
zeroslist1 = Flatten[Position[list1, 0]]
DeleteCases[
Flatten[Reap@
Do[Sow[ReplacePart[vtest1,
i -> vtest2[[i - 1]] & /@ zeroslist1]], {i, zeroslist1}], 2], Null]
The results of the output are:
{{List,1,1,0,0,1,1,0,1,0,0,1,0}, {0,1,1,4,0,1,1,0,1,0,0,1,0}, {0,1,1,0,7,1,1,0,1,0,0,1,0},{0,1,1,0,0,1,1,10,1,0,0,1,0}, {0,1,1,0,0,1,1,0,1,11,0,1,0},{0,1,1,0,0,1,1,0,1,0,3,1,0}, {0,1,1,0,0,1,1,0,1,0,0,1,0}}.
Either a cleaner way to code the desire output or a method of merging the output of my current for-loop to get the desired output would be great.
list-manipulation functions function-construction
$endgroup$
add a comment |
$begingroup$
My goal is to replace the zeros of one list with the (i-1)th element of a second list. For example, if
list1 = {0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0}
and
list2 = {6, 1, 4, 7, 2, 9, 10, 8, 11, 3, 5, 0, 12}
the desired output is {0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}. Note that the first element of the output is defined as 0 still.
My attempt to create a code for this is to first find the zeros of list1
then run a for-loop for $i in$zeroslist1
.
zeroslist1 = Flatten[Position[list1, 0]]
DeleteCases[
Flatten[Reap@
Do[Sow[ReplacePart[vtest1,
i -> vtest2[[i - 1]] & /@ zeroslist1]], {i, zeroslist1}], 2], Null]
The results of the output are:
{{List,1,1,0,0,1,1,0,1,0,0,1,0}, {0,1,1,4,0,1,1,0,1,0,0,1,0}, {0,1,1,0,7,1,1,0,1,0,0,1,0},{0,1,1,0,0,1,1,10,1,0,0,1,0}, {0,1,1,0,0,1,1,0,1,11,0,1,0},{0,1,1,0,0,1,1,0,1,0,3,1,0}, {0,1,1,0,0,1,1,0,1,0,0,1,0}}.
Either a cleaner way to code the desire output or a method of merging the output of my current for-loop to get the desired output would be great.
list-manipulation functions function-construction
$endgroup$
add a comment |
$begingroup$
My goal is to replace the zeros of one list with the (i-1)th element of a second list. For example, if
list1 = {0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0}
and
list2 = {6, 1, 4, 7, 2, 9, 10, 8, 11, 3, 5, 0, 12}
the desired output is {0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}. Note that the first element of the output is defined as 0 still.
My attempt to create a code for this is to first find the zeros of list1
then run a for-loop for $i in$zeroslist1
.
zeroslist1 = Flatten[Position[list1, 0]]
DeleteCases[
Flatten[Reap@
Do[Sow[ReplacePart[vtest1,
i -> vtest2[[i - 1]] & /@ zeroslist1]], {i, zeroslist1}], 2], Null]
The results of the output are:
{{List,1,1,0,0,1,1,0,1,0,0,1,0}, {0,1,1,4,0,1,1,0,1,0,0,1,0}, {0,1,1,0,7,1,1,0,1,0,0,1,0},{0,1,1,0,0,1,1,10,1,0,0,1,0}, {0,1,1,0,0,1,1,0,1,11,0,1,0},{0,1,1,0,0,1,1,0,1,0,3,1,0}, {0,1,1,0,0,1,1,0,1,0,0,1,0}}.
Either a cleaner way to code the desire output or a method of merging the output of my current for-loop to get the desired output would be great.
list-manipulation functions function-construction
$endgroup$
My goal is to replace the zeros of one list with the (i-1)th element of a second list. For example, if
list1 = {0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0}
and
list2 = {6, 1, 4, 7, 2, 9, 10, 8, 11, 3, 5, 0, 12}
the desired output is {0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}. Note that the first element of the output is defined as 0 still.
My attempt to create a code for this is to first find the zeros of list1
then run a for-loop for $i in$zeroslist1
.
zeroslist1 = Flatten[Position[list1, 0]]
DeleteCases[
Flatten[Reap@
Do[Sow[ReplacePart[vtest1,
i -> vtest2[[i - 1]] & /@ zeroslist1]], {i, zeroslist1}], 2], Null]
The results of the output are:
{{List,1,1,0,0,1,1,0,1,0,0,1,0}, {0,1,1,4,0,1,1,0,1,0,0,1,0}, {0,1,1,0,7,1,1,0,1,0,0,1,0},{0,1,1,0,0,1,1,10,1,0,0,1,0}, {0,1,1,0,0,1,1,0,1,11,0,1,0},{0,1,1,0,0,1,1,0,1,0,3,1,0}, {0,1,1,0,0,1,1,0,1,0,0,1,0}}.
Either a cleaner way to code the desire output or a method of merging the output of my current for-loop to get the desired output would be great.
list-manipulation functions function-construction
list-manipulation functions function-construction
asked 11 hours ago
WillWill
3347
3347
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
$begingroup$
You can use a multiplication instead of looping or conditionals:
list1 + (1 - list1) Prepend[Most[list2], 0]
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
The central point is that 0 and 1 in list1
aren't just symbols but numeric quantities.
$endgroup$
$begingroup$
What a simple way to do this. Thank you so much.
$endgroup$
– Will
10 hours ago
add a comment |
$begingroup$
idx = Random`Private`PositionsOf[Rest[list1], 0];
result = list1;
result[[idx + 1]] = list2[[idx]];
result
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
add a comment |
$begingroup$
Here's another possibility:
Module[{tmp=list1},
With[{i = Pick[Range[Length[list1]-1], Rest @ list1, 0]},
tmp[[i+1]]=list2[[i]]
];
tmp
]
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
add a comment |
$begingroup$
Also MapIndexed
works fine
(# /. List -> 0) & /@ MapIndexed[Replace[#1, 0 -> list2[[#2[[1]] - 1]]] &, list1]
The first piece just replaces the special case when a zero element is in the first entry (in Mathematica the 0th element is the Head
, which in this case is List
). Then the function MapIndexed
does its job.
$endgroup$
add a comment |
$begingroup$
MapThread[If[#1 == 0, #2, #1] &, {Join[list1, {0}], Join[{0}, list2]}] // Most
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
Your answer from above
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fmathematica.stackexchange.com%2fquestions%2f191320%2freplacing-the-zeros-of-one-list-by-the-i-1th-element-of-another-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
You can use a multiplication instead of looping or conditionals:
list1 + (1 - list1) Prepend[Most[list2], 0]
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
The central point is that 0 and 1 in list1
aren't just symbols but numeric quantities.
$endgroup$
$begingroup$
What a simple way to do this. Thank you so much.
$endgroup$
– Will
10 hours ago
add a comment |
$begingroup$
You can use a multiplication instead of looping or conditionals:
list1 + (1 - list1) Prepend[Most[list2], 0]
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
The central point is that 0 and 1 in list1
aren't just symbols but numeric quantities.
$endgroup$
$begingroup$
What a simple way to do this. Thank you so much.
$endgroup$
– Will
10 hours ago
add a comment |
$begingroup$
You can use a multiplication instead of looping or conditionals:
list1 + (1 - list1) Prepend[Most[list2], 0]
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
The central point is that 0 and 1 in list1
aren't just symbols but numeric quantities.
$endgroup$
You can use a multiplication instead of looping or conditionals:
list1 + (1 - list1) Prepend[Most[list2], 0]
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
The central point is that 0 and 1 in list1
aren't just symbols but numeric quantities.
edited 10 hours ago
answered 10 hours ago
RomanRoman
1,147511
1,147511
$begingroup$
What a simple way to do this. Thank you so much.
$endgroup$
– Will
10 hours ago
add a comment |
$begingroup$
What a simple way to do this. Thank you so much.
$endgroup$
– Will
10 hours ago
$begingroup$
What a simple way to do this. Thank you so much.
$endgroup$
– Will
10 hours ago
$begingroup$
What a simple way to do this. Thank you so much.
$endgroup$
– Will
10 hours ago
add a comment |
$begingroup$
idx = Random`Private`PositionsOf[Rest[list1], 0];
result = list1;
result[[idx + 1]] = list2[[idx]];
result
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
add a comment |
$begingroup$
idx = Random`Private`PositionsOf[Rest[list1], 0];
result = list1;
result[[idx + 1]] = list2[[idx]];
result
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
add a comment |
$begingroup$
idx = Random`Private`PositionsOf[Rest[list1], 0];
result = list1;
result[[idx + 1]] = list2[[idx]];
result
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
idx = Random`Private`PositionsOf[Rest[list1], 0];
result = list1;
result[[idx + 1]] = list2[[idx]];
result
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
answered 11 hours ago
Henrik SchumacherHenrik Schumacher
52.9k471148
52.9k471148
add a comment |
add a comment |
$begingroup$
Here's another possibility:
Module[{tmp=list1},
With[{i = Pick[Range[Length[list1]-1], Rest @ list1, 0]},
tmp[[i+1]]=list2[[i]]
];
tmp
]
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
add a comment |
$begingroup$
Here's another possibility:
Module[{tmp=list1},
With[{i = Pick[Range[Length[list1]-1], Rest @ list1, 0]},
tmp[[i+1]]=list2[[i]]
];
tmp
]
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
add a comment |
$begingroup$
Here's another possibility:
Module[{tmp=list1},
With[{i = Pick[Range[Length[list1]-1], Rest @ list1, 0]},
tmp[[i+1]]=list2[[i]]
];
tmp
]
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
Here's another possibility:
Module[{tmp=list1},
With[{i = Pick[Range[Length[list1]-1], Rest @ list1, 0]},
tmp[[i+1]]=list2[[i]]
];
tmp
]
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
answered 11 hours ago
Carl WollCarl Woll
67.9k389176
67.9k389176
add a comment |
add a comment |
$begingroup$
Also MapIndexed
works fine
(# /. List -> 0) & /@ MapIndexed[Replace[#1, 0 -> list2[[#2[[1]] - 1]]] &, list1]
The first piece just replaces the special case when a zero element is in the first entry (in Mathematica the 0th element is the Head
, which in this case is List
). Then the function MapIndexed
does its job.
$endgroup$
add a comment |
$begingroup$
Also MapIndexed
works fine
(# /. List -> 0) & /@ MapIndexed[Replace[#1, 0 -> list2[[#2[[1]] - 1]]] &, list1]
The first piece just replaces the special case when a zero element is in the first entry (in Mathematica the 0th element is the Head
, which in this case is List
). Then the function MapIndexed
does its job.
$endgroup$
add a comment |
$begingroup$
Also MapIndexed
works fine
(# /. List -> 0) & /@ MapIndexed[Replace[#1, 0 -> list2[[#2[[1]] - 1]]] &, list1]
The first piece just replaces the special case when a zero element is in the first entry (in Mathematica the 0th element is the Head
, which in this case is List
). Then the function MapIndexed
does its job.
$endgroup$
Also MapIndexed
works fine
(# /. List -> 0) & /@ MapIndexed[Replace[#1, 0 -> list2[[#2[[1]] - 1]]] &, list1]
The first piece just replaces the special case when a zero element is in the first entry (in Mathematica the 0th element is the Head
, which in this case is List
). Then the function MapIndexed
does its job.
answered 11 hours ago
Mane.andreaMane.andrea
1366
1366
add a comment |
add a comment |
$begingroup$
MapThread[If[#1 == 0, #2, #1] &, {Join[list1, {0}], Join[{0}, list2]}] // Most
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
Your answer from above
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
add a comment |
$begingroup$
MapThread[If[#1 == 0, #2, #1] &, {Join[list1, {0}], Join[{0}, list2]}] // Most
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
Your answer from above
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
add a comment |
$begingroup$
MapThread[If[#1 == 0, #2, #1] &, {Join[list1, {0}], Join[{0}, list2]}] // Most
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
Your answer from above
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
$endgroup$
MapThread[If[#1 == 0, #2, #1] &, {Join[list1, {0}], Join[{0}, list2]}] // Most
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
Your answer from above
{0, 1, 1, 4, 7, 1, 1, 10, 1, 11, 3, 1, 0}
answered 11 hours ago
MikeYMikeY
2,697412
2,697412
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fmathematica.stackexchange.com%2fquestions%2f191320%2freplacing-the-zeros-of-one-list-by-the-i-1th-element-of-another-list%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