Adding elements to some sublists of unequal lengthHow to Gather a list with some elements considered uniqueAdding elements in the sublistsHow to select 3-tuples of positions from a list with variable time-steps?Eliminating elements from sublists under a global conditionThreading sublists on listSplit list based on positions contained in another listData selection by comparing elements from different sublists in a nested listList replacements
How does throwing and catching ints work?
How to understand "No she bludgering well won't!"
Would it have been possible to re-fuel the planes in the air?
How to Handle Competitive and Blame Culture Within Team
What to do if some panel members walk out while I'm being interviewed?
Affection vs. Affliction
Texas gun laws - can an overseas visitor buy ammunition?
Is the number of federal judges appointed by Trump unusual?
Using a heater and toaster oven trips the breaker. Can an electrician fix this?
What is the relative return point (i.e. the "space it left") of a creature banished by the Banishment spell?
In Excel, is there a shortcut to hide a wide range of columns without mouse-dragging?
SQL Server Truncates Transaction Logs with Copy Only Backups
How bad is 1. e4 c5 2. Nf3 d6 3. a3?
Is there a way to use logic operators in Blender Shader nodes?
3.3M resistors not registering on multimeter - are they faulty?
Was Jumanji intended to be a co-op game?
Resolution of potentiometer
Identifying Wires behind Light Switch
Can every manifold be turned into a Lie group?
(x | y) - y why can't it simply be x or even `x | 0`
What would happen if a Cleric blessed a Warlock with a fiend patron?
How can I uninstall a site that was installed as a Web Application?
Creating 2020 in the fewest number of steps
Expressing the Riemann Zeta function in terms of GCD and LCM
Adding elements to some sublists of unequal length
How to Gather a list with some elements considered uniqueAdding elements in the sublistsHow to select 3-tuples of positions from a list with variable time-steps?Eliminating elements from sublists under a global conditionThreading sublists on listSplit list based on positions contained in another listData selection by comparing elements from different sublists in a nested listList replacements
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
.everyonelovesstackoverflowposition:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;
$begingroup$
Complicated title for a simple problem:
I have list
a = 1, 3, 5, 2, 2, 6, 2, 3, 5, 6, 1, 2, 4, 2
In which the first element of each sublist is basically an index. The following values are the actual data.
Then I want to add data to this list, e.g.,:
b = 2, 1, 4, 3
Which means that to the list with the index '2' the '1' should be added and the list with the index '4' the '3' should be added, so the result reads:
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
I found a couple of rather complicated, i.e., time consuming solutions involving loops. However, the actual datset is huge and is part of a numerical simulation, i.e., this procedure needs to be very fast.
list-manipulation symbolic
$endgroup$
add a comment
|
$begingroup$
Complicated title for a simple problem:
I have list
a = 1, 3, 5, 2, 2, 6, 2, 3, 5, 6, 1, 2, 4, 2
In which the first element of each sublist is basically an index. The following values are the actual data.
Then I want to add data to this list, e.g.,:
b = 2, 1, 4, 3
Which means that to the list with the index '2' the '1' should be added and the list with the index '4' the '3' should be added, so the result reads:
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
I found a couple of rather complicated, i.e., time consuming solutions involving loops. However, the actual datset is huge and is part of a numerical simulation, i.e., this procedure needs to be very fast.
list-manipulation symbolic
$endgroup$
add a comment
|
$begingroup$
Complicated title for a simple problem:
I have list
a = 1, 3, 5, 2, 2, 6, 2, 3, 5, 6, 1, 2, 4, 2
In which the first element of each sublist is basically an index. The following values are the actual data.
Then I want to add data to this list, e.g.,:
b = 2, 1, 4, 3
Which means that to the list with the index '2' the '1' should be added and the list with the index '4' the '3' should be added, so the result reads:
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
I found a couple of rather complicated, i.e., time consuming solutions involving loops. However, the actual datset is huge and is part of a numerical simulation, i.e., this procedure needs to be very fast.
list-manipulation symbolic
$endgroup$
Complicated title for a simple problem:
I have list
a = 1, 3, 5, 2, 2, 6, 2, 3, 5, 6, 1, 2, 4, 2
In which the first element of each sublist is basically an index. The following values are the actual data.
Then I want to add data to this list, e.g.,:
b = 2, 1, 4, 3
Which means that to the list with the index '2' the '1' should be added and the list with the index '4' the '3' should be added, so the result reads:
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
I found a couple of rather complicated, i.e., time consuming solutions involving loops. However, the actual datset is huge and is part of a numerical simulation, i.e., this procedure needs to be very fast.
list-manipulation symbolic
list-manipulation symbolic
edited Oct 2 at 16:17
Vitaliy Kaurov
60.9k6 gold badges167 silver badges288 bronze badges
60.9k6 gold badges167 silver badges288 bronze badges
asked Oct 2 at 15:55
Mockup DungeonMockup Dungeon
1,6829 silver badges13 bronze badges
1,6829 silver badges13 bronze badges
add a comment
|
add a comment
|
3 Answers
3
active
oldest
votes
$begingroup$
Fold[Insert[#1, Last[#2], First[#2], -1] &, a, b]
$endgroup$
add a comment
|
$begingroup$
Perhaps something like this:
ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b]
If data are large and speedup is needed you might want to try Join
ReplacePart[a, #1 -> Join[a[[#1]], #2] & @@@ b]
In case you want value of a
to be the new list with added elements, you whether can use AppendTo
:
ReplacePart[a, #1 -> AppendTo[a[[#1]], #2] & @@@ b]
or simply reassign:
a = ReplacePart[a, #1 -> Append[a[[#1]], #2] & @@@ b]
$endgroup$
add a comment
|
$begingroup$
You can make b
into a list of rules
rules = #, a__ :> #, a, #2 & @@@ b;
and use it with ReplaceAll
:
a /. rules
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
or with Replace
:
Replace[a, rules, All]
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
This approach works for any ordering of the elements in the input list:
c = RandomSample[a]
2, 6, 2, 3, 5, 6, 1, 2, 1, 3, 5, 2, 4, 2
c /. rules
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
Replace[c, rules, All]
2, 6, 2, 1, 3, 5, 6, 1, 2, 1, 3, 5, 2, 4, 2, 3
$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/4.0/"u003ecc by-sa 4.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%2f207226%2fadding-elements-to-some-sublists-of-unequal-length%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Fold[Insert[#1, Last[#2], First[#2], -1] &, a, b]
$endgroup$
add a comment
|
$begingroup$
Fold[Insert[#1, Last[#2], First[#2], -1] &, a, b]
$endgroup$
add a comment
|
$begingroup$
Fold[Insert[#1, Last[#2], First[#2], -1] &, a, b]
$endgroup$
Fold[Insert[#1, Last[#2], First[#2], -1] &, a, b]
answered Oct 2 at 16:31
Suba ThomasSuba Thomas
5,11411 silver badges20 bronze badges
5,11411 silver badges20 bronze badges
add a comment
|
add a comment
|
$begingroup$
Perhaps something like this:
ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b]
If data are large and speedup is needed you might want to try Join
ReplacePart[a, #1 -> Join[a[[#1]], #2] & @@@ b]
In case you want value of a
to be the new list with added elements, you whether can use AppendTo
:
ReplacePart[a, #1 -> AppendTo[a[[#1]], #2] & @@@ b]
or simply reassign:
a = ReplacePart[a, #1 -> Append[a[[#1]], #2] & @@@ b]
$endgroup$
add a comment
|
$begingroup$
Perhaps something like this:
ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b]
If data are large and speedup is needed you might want to try Join
ReplacePart[a, #1 -> Join[a[[#1]], #2] & @@@ b]
In case you want value of a
to be the new list with added elements, you whether can use AppendTo
:
ReplacePart[a, #1 -> AppendTo[a[[#1]], #2] & @@@ b]
or simply reassign:
a = ReplacePart[a, #1 -> Append[a[[#1]], #2] & @@@ b]
$endgroup$
add a comment
|
$begingroup$
Perhaps something like this:
ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b]
If data are large and speedup is needed you might want to try Join
ReplacePart[a, #1 -> Join[a[[#1]], #2] & @@@ b]
In case you want value of a
to be the new list with added elements, you whether can use AppendTo
:
ReplacePart[a, #1 -> AppendTo[a[[#1]], #2] & @@@ b]
or simply reassign:
a = ReplacePart[a, #1 -> Append[a[[#1]], #2] & @@@ b]
$endgroup$
Perhaps something like this:
ReplacePart[a, #1 -> Append[a[[#1]], #2]& @@@ b]
If data are large and speedup is needed you might want to try Join
ReplacePart[a, #1 -> Join[a[[#1]], #2] & @@@ b]
In case you want value of a
to be the new list with added elements, you whether can use AppendTo
:
ReplacePart[a, #1 -> AppendTo[a[[#1]], #2] & @@@ b]
or simply reassign:
a = ReplacePart[a, #1 -> Append[a[[#1]], #2] & @@@ b]
edited Oct 2 at 16:43
answered Oct 2 at 16:08
Vitaliy KaurovVitaliy Kaurov
60.9k6 gold badges167 silver badges288 bronze badges
60.9k6 gold badges167 silver badges288 bronze badges
add a comment
|
add a comment
|
$begingroup$
You can make b
into a list of rules
rules = #, a__ :> #, a, #2 & @@@ b;
and use it with ReplaceAll
:
a /. rules
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
or with Replace
:
Replace[a, rules, All]
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
This approach works for any ordering of the elements in the input list:
c = RandomSample[a]
2, 6, 2, 3, 5, 6, 1, 2, 1, 3, 5, 2, 4, 2
c /. rules
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
Replace[c, rules, All]
2, 6, 2, 1, 3, 5, 6, 1, 2, 1, 3, 5, 2, 4, 2, 3
$endgroup$
add a comment
|
$begingroup$
You can make b
into a list of rules
rules = #, a__ :> #, a, #2 & @@@ b;
and use it with ReplaceAll
:
a /. rules
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
or with Replace
:
Replace[a, rules, All]
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
This approach works for any ordering of the elements in the input list:
c = RandomSample[a]
2, 6, 2, 3, 5, 6, 1, 2, 1, 3, 5, 2, 4, 2
c /. rules
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
Replace[c, rules, All]
2, 6, 2, 1, 3, 5, 6, 1, 2, 1, 3, 5, 2, 4, 2, 3
$endgroup$
add a comment
|
$begingroup$
You can make b
into a list of rules
rules = #, a__ :> #, a, #2 & @@@ b;
and use it with ReplaceAll
:
a /. rules
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
or with Replace
:
Replace[a, rules, All]
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
This approach works for any ordering of the elements in the input list:
c = RandomSample[a]
2, 6, 2, 3, 5, 6, 1, 2, 1, 3, 5, 2, 4, 2
c /. rules
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
Replace[c, rules, All]
2, 6, 2, 1, 3, 5, 6, 1, 2, 1, 3, 5, 2, 4, 2, 3
$endgroup$
You can make b
into a list of rules
rules = #, a__ :> #, a, #2 & @@@ b;
and use it with ReplaceAll
:
a /. rules
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
or with Replace
:
Replace[a, rules, All]
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
This approach works for any ordering of the elements in the input list:
c = RandomSample[a]
2, 6, 2, 3, 5, 6, 1, 2, 1, 3, 5, 2, 4, 2
c /. rules
1, 3, 5, 2, 2, 6, 2, 1, 3, 5, 6, 1, 2, 4, 2, 3
Replace[c, rules, All]
2, 6, 2, 1, 3, 5, 6, 1, 2, 1, 3, 5, 2, 4, 2, 3
edited Oct 2 at 17:02
answered Oct 2 at 16:55
kglrkglr
230k10 gold badges260 silver badges522 bronze badges
230k10 gold badges260 silver badges522 bronze badges
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%2f207226%2fadding-elements-to-some-sublists-of-unequal-length%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