How to apply differences on part of a list and keep the restIs there a good way to map a function over a list to lists exclusively of a certain depth?Monitor the list for changesHow to apply a function of several arguments to a list?Split according to List and apply ruleSetting the value of a list itemCombining Map with DropHow to combine Nest and list PartDifferences applied to a list of matricesApplying Rest and Most to sublists of listMultiplying a list a vectors by the same matrix
Is there an idiom that support the idea that "inflation is bad"?
What to use instead of cling film to wrap pastry
What property of a BJT transistor makes it an amplifier?
I need a disease
Did we get closer to another plane than we were supposed to, or was the pilot just protecting our delicate sensibilities?
Understanding trademark infringements in a world where many dictionary words are trademarks?
BOOM! Perfect Clear for Mr. T
Can Infinity Stones be retrieved more than once?
Why was the battle set up *outside* Winterfell?
Why do only some White Walkers shatter into ice chips?
Missing Piece of Pie - Can you find it?
How does this change to the opportunity attack rule impact combat?
What was the design of the Macintosh II's MMU replacement?
What does this colon mean? It is not labeling, it is not ternary operator
Why wasn't the Night King naked in S08E03?
If I readied a spell with the trigger "When I take damage", do I have to make a constitution saving throw to avoid losing Concentration?
What is the most remote airport from the center of the city it supposedly serves?
String won't reverse using reverse_copy
Why does this rising edge detector using a capacitor and a resistor work?
How might a mountain bowl form?
Why is Arya visibly scared in the library in S8E3?
Why is this entry signal showing green?
what to look for in luxury cars like Acura/Lexus
Which module had more 'comfort' in terms of living space, the Lunar Module or the Command module?
How to apply differences on part of a list and keep the rest
Is there a good way to map a function over a list to lists exclusively of a certain depth?Monitor the list for changesHow to apply a function of several arguments to a list?Split according to List and apply ruleSetting the value of a list itemCombining Map with DropHow to combine Nest and list PartDifferences applied to a list of matricesApplying Rest and Most to sublists of listMultiplying a list a vectors by the same matrix
$begingroup$
I have a list,
l1 = a, b, 3, c, e, f, 5, k, n, k, 12, m, s, t, 1, y
and want to apply differences on the third parts and keep the parts right of the numerals collected.
My result should be
l2 = 2, c, k, 7, k, m, -11, m, y
I tried Map and MapAt, but I could not get anywhere. I could work around split things up and connect again. But is there a better way to do it?
list-manipulation
$endgroup$
add a comment |
$begingroup$
I have a list,
l1 = a, b, 3, c, e, f, 5, k, n, k, 12, m, s, t, 1, y
and want to apply differences on the third parts and keep the parts right of the numerals collected.
My result should be
l2 = 2, c, k, 7, k, m, -11, m, y
I tried Map and MapAt, but I could not get anywhere. I could work around split things up and connect again. But is there a better way to do it?
list-manipulation
$endgroup$
add a comment |
$begingroup$
I have a list,
l1 = a, b, 3, c, e, f, 5, k, n, k, 12, m, s, t, 1, y
and want to apply differences on the third parts and keep the parts right of the numerals collected.
My result should be
l2 = 2, c, k, 7, k, m, -11, m, y
I tried Map and MapAt, but I could not get anywhere. I could work around split things up and connect again. But is there a better way to do it?
list-manipulation
$endgroup$
I have a list,
l1 = a, b, 3, c, e, f, 5, k, n, k, 12, m, s, t, 1, y
and want to apply differences on the third parts and keep the parts right of the numerals collected.
My result should be
l2 = 2, c, k, 7, k, m, -11, m, y
I tried Map and MapAt, but I could not get anywhere. I could work around split things up and connect again. But is there a better way to do it?
list-manipulation
list-manipulation
edited 1 hour ago
m_goldberg
89.4k873201
89.4k873201
asked 9 hours ago
user57467user57467
563
563
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
Perhaps this?:
l1 = a, b, 3, c, e, f, 5, k, n, k, 12, m, s, t, 1, y;
l2 = Differences[l1[[All, 3 ;;]]] /. b_ - a_ :> Sequence[a, b]
(* 2, c, k, 7, k, m, -11, m, y *)
It assumes the letter symbols are simple and not complicated expressions.
This is more complicated, but more robust:
Flatten /@
Transpose@
MapAt[Differences,
Partition[Transpose@l1[[All, 3 ;;]], 1, 2, 1, 1], 1, All, 1]
$endgroup$
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
8 hours ago
$begingroup$
How would you do it if l1= z,a, b, 3, c, w,e, f, 5, k, q,n, k, 12, m, p,s, t, 1, y;
$endgroup$
– user57467
8 hours ago
$begingroup$
@user57467 I would change the third column to the fourth:l1[[All, 4 ;;]]
. You can programmatically get the column withcol = Position[First@l1, _?NumericQ][[1, 1]]
orcol = Position[First@l1, _Integer][[1, 1]]
; then usel1[[All, col ;;]]
.
$endgroup$
– Michael E2
7 hours ago
add a comment |
$begingroup$
You can also use BlockMap
as follows:
BlockMap[#[[3]].-1, 1, ## & @@ Flatten@#[[4 ;;]] &@* Transpose, l1, 2, 1]
2, c, k, 7, k, m, -11, m, y
or
BlockMap[#[[1]].-1, 1, ## & @@ Flatten@ #[[2 ;;]] &@*Transpose, l1[[All, 3 ;;]], 2, 1]
2, c, k, 7, k, m, -11, m, y
$endgroup$
$begingroup$
I am impressed!
$endgroup$
– user57467
7 hours ago
add a comment |
$begingroup$
This is very similar to kglr's first solution but picks the relevant quantities a bit more explicitly:
l2 = BlockMap[#[[2, 3]] - #[[1, 3]], #[[1, 4]], #[[2, 4]] &, l1, 2, 1]
2, c, k, 7, k, m, -11, m, y
With a parameter to change the symbolic column quickly:
l2 = With[col = 3,
BlockMap[#[[2,col]] - #[[1,col]], #[[1,col+1]], #[[2,col+1]] &, l1, 2, 1]]
2, c, k, 7, k, m, -11, m, y
$endgroup$
add a comment |
$begingroup$
A solution with MapThread
on an offset Partition
.
MapThread[Sequence @@ #@#2 &, Differences, Identity, Transpose@#] & /@
Partition[l1[[All, 3 ;;]], 2, 1]
2, c, k, 7, k, m, -11, m, y
Differences
is applied to the integers while Identity
preserves the form of the symbols.
Hope this helps.
$endgroup$
add a comment |
Your Answer
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%2f197441%2fhow-to-apply-differences-on-part-of-a-list-and-keep-the-rest%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
$begingroup$
Perhaps this?:
l1 = a, b, 3, c, e, f, 5, k, n, k, 12, m, s, t, 1, y;
l2 = Differences[l1[[All, 3 ;;]]] /. b_ - a_ :> Sequence[a, b]
(* 2, c, k, 7, k, m, -11, m, y *)
It assumes the letter symbols are simple and not complicated expressions.
This is more complicated, but more robust:
Flatten /@
Transpose@
MapAt[Differences,
Partition[Transpose@l1[[All, 3 ;;]], 1, 2, 1, 1], 1, All, 1]
$endgroup$
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
8 hours ago
$begingroup$
How would you do it if l1= z,a, b, 3, c, w,e, f, 5, k, q,n, k, 12, m, p,s, t, 1, y;
$endgroup$
– user57467
8 hours ago
$begingroup$
@user57467 I would change the third column to the fourth:l1[[All, 4 ;;]]
. You can programmatically get the column withcol = Position[First@l1, _?NumericQ][[1, 1]]
orcol = Position[First@l1, _Integer][[1, 1]]
; then usel1[[All, col ;;]]
.
$endgroup$
– Michael E2
7 hours ago
add a comment |
$begingroup$
Perhaps this?:
l1 = a, b, 3, c, e, f, 5, k, n, k, 12, m, s, t, 1, y;
l2 = Differences[l1[[All, 3 ;;]]] /. b_ - a_ :> Sequence[a, b]
(* 2, c, k, 7, k, m, -11, m, y *)
It assumes the letter symbols are simple and not complicated expressions.
This is more complicated, but more robust:
Flatten /@
Transpose@
MapAt[Differences,
Partition[Transpose@l1[[All, 3 ;;]], 1, 2, 1, 1], 1, All, 1]
$endgroup$
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
8 hours ago
$begingroup$
How would you do it if l1= z,a, b, 3, c, w,e, f, 5, k, q,n, k, 12, m, p,s, t, 1, y;
$endgroup$
– user57467
8 hours ago
$begingroup$
@user57467 I would change the third column to the fourth:l1[[All, 4 ;;]]
. You can programmatically get the column withcol = Position[First@l1, _?NumericQ][[1, 1]]
orcol = Position[First@l1, _Integer][[1, 1]]
; then usel1[[All, col ;;]]
.
$endgroup$
– Michael E2
7 hours ago
add a comment |
$begingroup$
Perhaps this?:
l1 = a, b, 3, c, e, f, 5, k, n, k, 12, m, s, t, 1, y;
l2 = Differences[l1[[All, 3 ;;]]] /. b_ - a_ :> Sequence[a, b]
(* 2, c, k, 7, k, m, -11, m, y *)
It assumes the letter symbols are simple and not complicated expressions.
This is more complicated, but more robust:
Flatten /@
Transpose@
MapAt[Differences,
Partition[Transpose@l1[[All, 3 ;;]], 1, 2, 1, 1], 1, All, 1]
$endgroup$
Perhaps this?:
l1 = a, b, 3, c, e, f, 5, k, n, k, 12, m, s, t, 1, y;
l2 = Differences[l1[[All, 3 ;;]]] /. b_ - a_ :> Sequence[a, b]
(* 2, c, k, 7, k, m, -11, m, y *)
It assumes the letter symbols are simple and not complicated expressions.
This is more complicated, but more robust:
Flatten /@
Transpose@
MapAt[Differences,
Partition[Transpose@l1[[All, 3 ;;]], 1, 2, 1, 1], 1, All, 1]
edited 8 hours ago
answered 9 hours ago
Michael E2Michael E2
151k12204485
151k12204485
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
8 hours ago
$begingroup$
How would you do it if l1= z,a, b, 3, c, w,e, f, 5, k, q,n, k, 12, m, p,s, t, 1, y;
$endgroup$
– user57467
8 hours ago
$begingroup$
@user57467 I would change the third column to the fourth:l1[[All, 4 ;;]]
. You can programmatically get the column withcol = Position[First@l1, _?NumericQ][[1, 1]]
orcol = Position[First@l1, _Integer][[1, 1]]
; then usel1[[All, col ;;]]
.
$endgroup$
– Michael E2
7 hours ago
add a comment |
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
8 hours ago
$begingroup$
How would you do it if l1= z,a, b, 3, c, w,e, f, 5, k, q,n, k, 12, m, p,s, t, 1, y;
$endgroup$
– user57467
8 hours ago
$begingroup$
@user57467 I would change the third column to the fourth:l1[[All, 4 ;;]]
. You can programmatically get the column withcol = Position[First@l1, _?NumericQ][[1, 1]]
orcol = Position[First@l1, _Integer][[1, 1]]
; then usel1[[All, col ;;]]
.
$endgroup$
– Michael E2
7 hours ago
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
8 hours ago
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
8 hours ago
$begingroup$
How would you do it if l1= z,a, b, 3, c, w,e, f, 5, k, q,n, k, 12, m, p,s, t, 1, y;
$endgroup$
– user57467
8 hours ago
$begingroup$
How would you do it if l1= z,a, b, 3, c, w,e, f, 5, k, q,n, k, 12, m, p,s, t, 1, y;
$endgroup$
– user57467
8 hours ago
$begingroup$
@user57467 I would change the third column to the fourth:
l1[[All, 4 ;;]]
. You can programmatically get the column with col = Position[First@l1, _?NumericQ][[1, 1]]
or col = Position[First@l1, _Integer][[1, 1]]
; then use l1[[All, col ;;]]
.$endgroup$
– Michael E2
7 hours ago
$begingroup$
@user57467 I would change the third column to the fourth:
l1[[All, 4 ;;]]
. You can programmatically get the column with col = Position[First@l1, _?NumericQ][[1, 1]]
or col = Position[First@l1, _Integer][[1, 1]]
; then use l1[[All, col ;;]]
.$endgroup$
– Michael E2
7 hours ago
add a comment |
$begingroup$
You can also use BlockMap
as follows:
BlockMap[#[[3]].-1, 1, ## & @@ Flatten@#[[4 ;;]] &@* Transpose, l1, 2, 1]
2, c, k, 7, k, m, -11, m, y
or
BlockMap[#[[1]].-1, 1, ## & @@ Flatten@ #[[2 ;;]] &@*Transpose, l1[[All, 3 ;;]], 2, 1]
2, c, k, 7, k, m, -11, m, y
$endgroup$
$begingroup$
I am impressed!
$endgroup$
– user57467
7 hours ago
add a comment |
$begingroup$
You can also use BlockMap
as follows:
BlockMap[#[[3]].-1, 1, ## & @@ Flatten@#[[4 ;;]] &@* Transpose, l1, 2, 1]
2, c, k, 7, k, m, -11, m, y
or
BlockMap[#[[1]].-1, 1, ## & @@ Flatten@ #[[2 ;;]] &@*Transpose, l1[[All, 3 ;;]], 2, 1]
2, c, k, 7, k, m, -11, m, y
$endgroup$
$begingroup$
I am impressed!
$endgroup$
– user57467
7 hours ago
add a comment |
$begingroup$
You can also use BlockMap
as follows:
BlockMap[#[[3]].-1, 1, ## & @@ Flatten@#[[4 ;;]] &@* Transpose, l1, 2, 1]
2, c, k, 7, k, m, -11, m, y
or
BlockMap[#[[1]].-1, 1, ## & @@ Flatten@ #[[2 ;;]] &@*Transpose, l1[[All, 3 ;;]], 2, 1]
2, c, k, 7, k, m, -11, m, y
$endgroup$
You can also use BlockMap
as follows:
BlockMap[#[[3]].-1, 1, ## & @@ Flatten@#[[4 ;;]] &@* Transpose, l1, 2, 1]
2, c, k, 7, k, m, -11, m, y
or
BlockMap[#[[1]].-1, 1, ## & @@ Flatten@ #[[2 ;;]] &@*Transpose, l1[[All, 3 ;;]], 2, 1]
2, c, k, 7, k, m, -11, m, y
edited 7 hours ago
answered 7 hours ago
kglrkglr
190k10209427
190k10209427
$begingroup$
I am impressed!
$endgroup$
– user57467
7 hours ago
add a comment |
$begingroup$
I am impressed!
$endgroup$
– user57467
7 hours ago
$begingroup$
I am impressed!
$endgroup$
– user57467
7 hours ago
$begingroup$
I am impressed!
$endgroup$
– user57467
7 hours ago
add a comment |
$begingroup$
This is very similar to kglr's first solution but picks the relevant quantities a bit more explicitly:
l2 = BlockMap[#[[2, 3]] - #[[1, 3]], #[[1, 4]], #[[2, 4]] &, l1, 2, 1]
2, c, k, 7, k, m, -11, m, y
With a parameter to change the symbolic column quickly:
l2 = With[col = 3,
BlockMap[#[[2,col]] - #[[1,col]], #[[1,col+1]], #[[2,col+1]] &, l1, 2, 1]]
2, c, k, 7, k, m, -11, m, y
$endgroup$
add a comment |
$begingroup$
This is very similar to kglr's first solution but picks the relevant quantities a bit more explicitly:
l2 = BlockMap[#[[2, 3]] - #[[1, 3]], #[[1, 4]], #[[2, 4]] &, l1, 2, 1]
2, c, k, 7, k, m, -11, m, y
With a parameter to change the symbolic column quickly:
l2 = With[col = 3,
BlockMap[#[[2,col]] - #[[1,col]], #[[1,col+1]], #[[2,col+1]] &, l1, 2, 1]]
2, c, k, 7, k, m, -11, m, y
$endgroup$
add a comment |
$begingroup$
This is very similar to kglr's first solution but picks the relevant quantities a bit more explicitly:
l2 = BlockMap[#[[2, 3]] - #[[1, 3]], #[[1, 4]], #[[2, 4]] &, l1, 2, 1]
2, c, k, 7, k, m, -11, m, y
With a parameter to change the symbolic column quickly:
l2 = With[col = 3,
BlockMap[#[[2,col]] - #[[1,col]], #[[1,col+1]], #[[2,col+1]] &, l1, 2, 1]]
2, c, k, 7, k, m, -11, m, y
$endgroup$
This is very similar to kglr's first solution but picks the relevant quantities a bit more explicitly:
l2 = BlockMap[#[[2, 3]] - #[[1, 3]], #[[1, 4]], #[[2, 4]] &, l1, 2, 1]
2, c, k, 7, k, m, -11, m, y
With a parameter to change the symbolic column quickly:
l2 = With[col = 3,
BlockMap[#[[2,col]] - #[[1,col]], #[[1,col+1]], #[[2,col+1]] &, l1, 2, 1]]
2, c, k, 7, k, m, -11, m, y
edited 6 hours ago
answered 7 hours ago
RomanRoman
6,86011134
6,86011134
add a comment |
add a comment |
$begingroup$
A solution with MapThread
on an offset Partition
.
MapThread[Sequence @@ #@#2 &, Differences, Identity, Transpose@#] & /@
Partition[l1[[All, 3 ;;]], 2, 1]
2, c, k, 7, k, m, -11, m, y
Differences
is applied to the integers while Identity
preserves the form of the symbols.
Hope this helps.
$endgroup$
add a comment |
$begingroup$
A solution with MapThread
on an offset Partition
.
MapThread[Sequence @@ #@#2 &, Differences, Identity, Transpose@#] & /@
Partition[l1[[All, 3 ;;]], 2, 1]
2, c, k, 7, k, m, -11, m, y
Differences
is applied to the integers while Identity
preserves the form of the symbols.
Hope this helps.
$endgroup$
add a comment |
$begingroup$
A solution with MapThread
on an offset Partition
.
MapThread[Sequence @@ #@#2 &, Differences, Identity, Transpose@#] & /@
Partition[l1[[All, 3 ;;]], 2, 1]
2, c, k, 7, k, m, -11, m, y
Differences
is applied to the integers while Identity
preserves the form of the symbols.
Hope this helps.
$endgroup$
A solution with MapThread
on an offset Partition
.
MapThread[Sequence @@ #@#2 &, Differences, Identity, Transpose@#] & /@
Partition[l1[[All, 3 ;;]], 2, 1]
2, c, k, 7, k, m, -11, m, y
Differences
is applied to the integers while Identity
preserves the form of the symbols.
Hope this helps.
answered 2 hours ago
EdmundEdmund
26.9k330103
26.9k330103
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%2f197441%2fhow-to-apply-differences-on-part-of-a-list-and-keep-the-rest%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