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
Is throwing dice a stochastic or a deterministic process?
Page count conversion from single to double-space for submissions
How to remap repeating commands i.e. <number><command>?
What's the 2-minute timer on mobile Deutsche Bahn tickets?
Should I simplify my writing in a foreign country?
Krull dimension of the ring of global sections
Has the United States ever had a non-Christian President?
Is there a closed form, or cleaner way of writing this function?
My first C++ game (snake console game)
As a GM, is it bad form to ask for a moment to think when improvising?
GitLab account hacked and repo wiped
The origin of list data structure
Meaning of the (idiomatic?) expression "seghe mentali"
Why did WWI include Japan?
How to deal with employer who keeps me at work after working hours
Which US defense organization would respond to an invasion like this?
Which "exotic salt" can lower water's freezing point by –70 °C?
Is any special diet an effective treatment of autism?
Sheared off exhasut pipe: How to fix without a welder?
Dihedral group D4 composition with custom labels
weird pluperfect subjunctive in Eutropius
no sense/need/point
Where to draw the line between quantum mechanics theory and its interpretation(s)?
Is 'contemporary' ambiguous and if so is there a better word?
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 May 2 at 3:53
user64494
3,65111222
3,65111222
asked May 1 at 14:21
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
May 1 at 15:03
$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
May 1 at 15:36
$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
May 1 at 15:44
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
May 1 at 16:08
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$
$begingroup$
Wow! Good! I enjoy the clarity!
$endgroup$
– user57467
2 days ago
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
May 1 at 15:03
$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
May 1 at 15:36
$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
May 1 at 15:44
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
May 1 at 15:03
$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
May 1 at 15:36
$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
May 1 at 15:44
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 May 1 at 14:39
answered May 1 at 14:32
Michael E2Michael E2
151k12204485
151k12204485
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
May 1 at 15:03
$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
May 1 at 15:36
$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
May 1 at 15:44
add a comment |
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
May 1 at 15:03
$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
May 1 at 15:36
$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
May 1 at 15:44
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
May 1 at 15:03
$begingroup$
Thank you. I have to digest your answer.
$endgroup$
– user57467
May 1 at 15:03
$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
May 1 at 15:36
$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
May 1 at 15:36
$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
May 1 at 15:44
$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
May 1 at 15:44
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
May 1 at 16:08
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
May 1 at 16:08
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 May 1 at 15:57
answered May 1 at 15:49
kglrkglr
191k10211430
191k10211430
$begingroup$
I am impressed!
$endgroup$
– user57467
May 1 at 16:08
add a comment |
$begingroup$
I am impressed!
$endgroup$
– user57467
May 1 at 16:08
$begingroup$
I am impressed!
$endgroup$
– user57467
May 1 at 16:08
$begingroup$
I am impressed!
$endgroup$
– user57467
May 1 at 16:08
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$
$begingroup$
Wow! Good! I enjoy the clarity!
$endgroup$
– user57467
2 days 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$
$begingroup$
Wow! Good! I enjoy the clarity!
$endgroup$
– user57467
2 days 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$
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 May 1 at 17:09
answered May 1 at 16:31
RomanRoman
7,15511134
7,15511134
$begingroup$
Wow! Good! I enjoy the clarity!
$endgroup$
– user57467
2 days ago
add a comment |
$begingroup$
Wow! Good! I enjoy the clarity!
$endgroup$
– user57467
2 days ago
$begingroup$
Wow! Good! I enjoy the clarity!
$endgroup$
– user57467
2 days ago
$begingroup$
Wow! Good! I enjoy the clarity!
$endgroup$
– user57467
2 days ago
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 May 1 at 21:11
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