Generate certain list from two listsBest way to apply a list of functions to a list of values?Distributing elements across a list of listsHow to find the distance of two lists?One to Many Lists MergePattern matching - comparing two listsContract two listsMatching the order of a master list of lists from a random list of listsEfficiently exchange elements between two listsJoining 100 lists to make one big listSelecting cases from a list based on two conditionsRagged Transpose

In native German words, is Q always followed by U, as in English?

Dold-Kan correspondence in the category of symmetric spectra

Do we or do we not observe (measure) superpositions all the time?

How can I check type T is among parameter pack Ts... in C++?

The difference between Rad1 and Rfd1

What are good ways to spray paint a QR code on a footpath?

Disabling automatic add after resolving git conflict

How well known and how commonly used was Huffman coding in 1979?

Are there any vegetarian astronauts?

Did Chinese school textbook maps (c. 1951) "depict China as stretching even into the central Asian republics"?

Set vertical spacing between two particular items

Is there any set of 2-6 notes that doesn't have a chord name?

Signing using digital signatures?

Do sudoku answers always have a single minimal clue set?

Was "I have the farts, again" broadcast from the Moon to the whole world?

Why is Madam Hooch not a professor?

How exactly is a normal force exerted, at the molecular level?

How can I create ribbons like these in Microsoft word 2010?

How can I bypass the confirmation for granting permissions to files or folders?

Why isn’t the tax system continuous rather than bracketed?

One folder two different locations on ubuntu 18.04

Is there a short way to check uniqueness of values without using 'if' and multiple 'and's?

How was film developed in the late 1920s?

Analog is Obtuse!



Generate certain list from two lists


Best way to apply a list of functions to a list of values?Distributing elements across a list of listsHow to find the distance of two lists?One to Many Lists MergePattern matching - comparing two listsContract two listsMatching the order of a master list of lists from a random list of listsEfficiently exchange elements between two listsJoining 100 lists to make one big listSelecting cases from a list based on two conditionsRagged Transpose






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4












$begingroup$


I have two lists.



l1=
"Mn", "Mn1", 1., "B", 1.4,
"Al", "Al1", 1., "B", 1.4
;

l2=
1, 1, 0., 11, 11, 0.,

2, 2, 0., 22, 22, 0., 222, 222, 0.



This is a short version of the lists. The two lists always have the same Length so that their level-1 elements have a one-to-one relation. However, the elements of l2 can have varying Length as shown here.



I'd like to generate a new list as follows.



l3=

"Mn", "Mn1", 1, 1, 0., 1., "B", 1.4,
"Mn", "Mn1", 11, 11, 0., 1., "B", 1.4,

"Al", "Al1", 2, 2, 0., 1., "B", 1.4,
"Al", "Al1", 22, 22, 0., 1., "B", 1.4,
"Al", "Al1", 222, 222, 0., 1., "B", 1.4



I think MapThread might be the direction to go, but I cannot think of any function to obtain the result. I'm not stick to MapThread. Any function that can do the job is okay as long as it's a vertorization method since that's what MMA favors.



Thank you.










share|improve this question











$endgroup$











  • $begingroup$
    Can you elaborate your receipt for l3 in detail? I understand nothing. BTW, the notation "l" is not good: compare with "I" and "1".
    $endgroup$
    – user64494
    Jun 9 at 19:51










  • $begingroup$
    @user64494, it's really difficult for me to think of a good way to describe the format of l3 for English isn't my first language. That's why I use newlines to separate elements of l1 and l2 and change values of l2 to 1,11 and 2, 22, 222 for clarity. Maybe you could help me with that. But I think the answers provided understood my need and returns the desired format of l3. Also, I appreciate the suggestions of l1/2/3 may not be a good variable name. Thanks.
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:40


















4












$begingroup$


I have two lists.



l1=
"Mn", "Mn1", 1., "B", 1.4,
"Al", "Al1", 1., "B", 1.4
;

l2=
1, 1, 0., 11, 11, 0.,

2, 2, 0., 22, 22, 0., 222, 222, 0.



This is a short version of the lists. The two lists always have the same Length so that their level-1 elements have a one-to-one relation. However, the elements of l2 can have varying Length as shown here.



I'd like to generate a new list as follows.



l3=

"Mn", "Mn1", 1, 1, 0., 1., "B", 1.4,
"Mn", "Mn1", 11, 11, 0., 1., "B", 1.4,

"Al", "Al1", 2, 2, 0., 1., "B", 1.4,
"Al", "Al1", 22, 22, 0., 1., "B", 1.4,
"Al", "Al1", 222, 222, 0., 1., "B", 1.4



I think MapThread might be the direction to go, but I cannot think of any function to obtain the result. I'm not stick to MapThread. Any function that can do the job is okay as long as it's a vertorization method since that's what MMA favors.



Thank you.










share|improve this question











$endgroup$











  • $begingroup$
    Can you elaborate your receipt for l3 in detail? I understand nothing. BTW, the notation "l" is not good: compare with "I" and "1".
    $endgroup$
    – user64494
    Jun 9 at 19:51










  • $begingroup$
    @user64494, it's really difficult for me to think of a good way to describe the format of l3 for English isn't my first language. That's why I use newlines to separate elements of l1 and l2 and change values of l2 to 1,11 and 2, 22, 222 for clarity. Maybe you could help me with that. But I think the answers provided understood my need and returns the desired format of l3. Also, I appreciate the suggestions of l1/2/3 may not be a good variable name. Thanks.
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:40














4












4








4


1



$begingroup$


I have two lists.



l1=
"Mn", "Mn1", 1., "B", 1.4,
"Al", "Al1", 1., "B", 1.4
;

l2=
1, 1, 0., 11, 11, 0.,

2, 2, 0., 22, 22, 0., 222, 222, 0.



This is a short version of the lists. The two lists always have the same Length so that their level-1 elements have a one-to-one relation. However, the elements of l2 can have varying Length as shown here.



I'd like to generate a new list as follows.



l3=

"Mn", "Mn1", 1, 1, 0., 1., "B", 1.4,
"Mn", "Mn1", 11, 11, 0., 1., "B", 1.4,

"Al", "Al1", 2, 2, 0., 1., "B", 1.4,
"Al", "Al1", 22, 22, 0., 1., "B", 1.4,
"Al", "Al1", 222, 222, 0., 1., "B", 1.4



I think MapThread might be the direction to go, but I cannot think of any function to obtain the result. I'm not stick to MapThread. Any function that can do the job is okay as long as it's a vertorization method since that's what MMA favors.



Thank you.










share|improve this question











$endgroup$




I have two lists.



l1=
"Mn", "Mn1", 1., "B", 1.4,
"Al", "Al1", 1., "B", 1.4
;

l2=
1, 1, 0., 11, 11, 0.,

2, 2, 0., 22, 22, 0., 222, 222, 0.



This is a short version of the lists. The two lists always have the same Length so that their level-1 elements have a one-to-one relation. However, the elements of l2 can have varying Length as shown here.



I'd like to generate a new list as follows.



l3=

"Mn", "Mn1", 1, 1, 0., 1., "B", 1.4,
"Mn", "Mn1", 11, 11, 0., 1., "B", 1.4,

"Al", "Al1", 2, 2, 0., 1., "B", 1.4,
"Al", "Al1", 22, 22, 0., 1., "B", 1.4,
"Al", "Al1", 222, 222, 0., 1., "B", 1.4



I think MapThread might be the direction to go, but I cannot think of any function to obtain the result. I'm not stick to MapThread. Any function that can do the job is okay as long as it's a vertorization method since that's what MMA favors.



Thank you.







list-manipulation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 9 at 19:46









user64494

4,1162 gold badges13 silver badges23 bronze badges




4,1162 gold badges13 silver badges23 bronze badges










asked Jun 9 at 19:44









Bemtevi77Bemtevi77

796 bronze badges




796 bronze badges











  • $begingroup$
    Can you elaborate your receipt for l3 in detail? I understand nothing. BTW, the notation "l" is not good: compare with "I" and "1".
    $endgroup$
    – user64494
    Jun 9 at 19:51










  • $begingroup$
    @user64494, it's really difficult for me to think of a good way to describe the format of l3 for English isn't my first language. That's why I use newlines to separate elements of l1 and l2 and change values of l2 to 1,11 and 2, 22, 222 for clarity. Maybe you could help me with that. But I think the answers provided understood my need and returns the desired format of l3. Also, I appreciate the suggestions of l1/2/3 may not be a good variable name. Thanks.
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:40

















  • $begingroup$
    Can you elaborate your receipt for l3 in detail? I understand nothing. BTW, the notation "l" is not good: compare with "I" and "1".
    $endgroup$
    – user64494
    Jun 9 at 19:51










  • $begingroup$
    @user64494, it's really difficult for me to think of a good way to describe the format of l3 for English isn't my first language. That's why I use newlines to separate elements of l1 and l2 and change values of l2 to 1,11 and 2, 22, 222 for clarity. Maybe you could help me with that. But I think the answers provided understood my need and returns the desired format of l3. Also, I appreciate the suggestions of l1/2/3 may not be a good variable name. Thanks.
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:40
















$begingroup$
Can you elaborate your receipt for l3 in detail? I understand nothing. BTW, the notation "l" is not good: compare with "I" and "1".
$endgroup$
– user64494
Jun 9 at 19:51




$begingroup$
Can you elaborate your receipt for l3 in detail? I understand nothing. BTW, the notation "l" is not good: compare with "I" and "1".
$endgroup$
– user64494
Jun 9 at 19:51












$begingroup$
@user64494, it's really difficult for me to think of a good way to describe the format of l3 for English isn't my first language. That's why I use newlines to separate elements of l1 and l2 and change values of l2 to 1,11 and 2, 22, 222 for clarity. Maybe you could help me with that. But I think the answers provided understood my need and returns the desired format of l3. Also, I appreciate the suggestions of l1/2/3 may not be a good variable name. Thanks.
$endgroup$
– Bemtevi77
Jun 9 at 22:40





$begingroup$
@user64494, it's really difficult for me to think of a good way to describe the format of l3 for English isn't my first language. That's why I use newlines to separate elements of l1 and l2 and change values of l2 to 1,11 and 2, 22, 222 for clarity. Maybe you could help me with that. But I think the answers provided understood my need and returns the desired format of l3. Also, I appreciate the suggestions of l1/2/3 may not be a good variable name. Thanks.
$endgroup$
– Bemtevi77
Jun 9 at 22:40











2 Answers
2






active

oldest

votes


















5












$begingroup$

Yes you can use MapThread:



l3 = Join @@ MapThread[Function[x, y, Insert[x, #, 3] & /@ y], l1, l2]


Here's a more esoteric version that builds lists of mapping operators from l2 and then applies them to the elements of l1:



l3 = Join @@ MapThread[Through[#1[#2]] &, Map[Insert[#, 3] &, l2, 2], l1]


See here for a discussion of the Through[#1[#2]]& operator.






share|improve this answer











$endgroup$












  • $begingroup$
    A good code is a commented code. Comments are useful to both readers and authors.
    $endgroup$
    – user64494
    Jun 9 at 19:52







  • 4




    $begingroup$
    @user64494 I expect some effort from the reader: the analysis and exegesis of other people's code snippets is a great learning tool. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
    $endgroup$
    – Roman
    Jun 9 at 20:32










  • $begingroup$
    @Roman, I like the 1st solution you provided because that's what I can remember in brain once I learn it. I was struggling with Part, but yours enlightened me. I'll need to understand better the Through approach. First time I heard of this function. Thanks.
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:12


















5












$begingroup$

You can also MapThread the function Thread[Insert[#, #2, 3]] & on the pair of lists l1,l2:



Join @@ MapThread[Thread[Insert[#, #2, 3]] &, l1, l2]



Mn, Mn1, 1, 1, 0., 1., B, 1.4, Mn, Mn1, 11, 11, 0., 1., B, 1.4,

Al, Al1, 2, 2, 0., 1., B, 1.4, Al, Al1, 22, 22, 0., 1., B, 1.4, Al, Al1, 222, 222, 0., 1., B, 1.4




Alternatively, use the MapThread/Thread combination to create pairings appended with 3 and apply Insert to the resulting triples:



Join @@ Apply[Insert, 
MapThread[Thread[##, 3, List, 2] &, l1, l2],
2]



same result







share|improve this answer











$endgroup$








  • 1




    $begingroup$
    Yes that's what I was looking for! Thanks. Prefix it with Join@@ to match the spec.
    $endgroup$
    – Roman
    Jun 9 at 21:05










  • $begingroup$
    @kglr, I never think of using Thread function before reading your answer. It's a little bit difficult for me to appreciate the mechanism of Thread. It's written "threads" f over any lists that appear in args in MMA's help page. But elements of l1 and l2 are both lists. I think Insert plays a role here so that the function only threads over element of l2. Am I understanding correctly? Thanks
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:19










  • $begingroup$
    @Yaofeng, you are right for the first one. In the second, the second and third arguments of Thread controls what to thread over and in which positions.
    $endgroup$
    – kglr
    Jun 9 at 22:37










  • $begingroup$
    @kglr, I compared the AbsoluteTiming for your Thread solution and Roman's Function solution. Yours is faster. Although it's not intuitive for me at the moment, but I guess that's the direction for me to go, in line with MMA's vectorization. Thanks again!
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:48










  • $begingroup$
    Although I think your Thread[Insert[##,3]] method is the most poetic, it's also the most brittle: Inserting first and Threading second makes the assumption that none of the elements of the lists in l1 are themselves lists. Example: with l1 = "Mn", "Mn1", 1., "B", 1.4, "Al", "Al1", 1., "B", 1.4 this method throws a Thread::tdlen. To be more robust it's probably advisable to Thread first and Insert second, as in your second method.
    $endgroup$
    – Roman
    Jun 10 at 7:55














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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f200024%2fgenerate-certain-list-from-two-lists%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5












$begingroup$

Yes you can use MapThread:



l3 = Join @@ MapThread[Function[x, y, Insert[x, #, 3] & /@ y], l1, l2]


Here's a more esoteric version that builds lists of mapping operators from l2 and then applies them to the elements of l1:



l3 = Join @@ MapThread[Through[#1[#2]] &, Map[Insert[#, 3] &, l2, 2], l1]


See here for a discussion of the Through[#1[#2]]& operator.






share|improve this answer











$endgroup$












  • $begingroup$
    A good code is a commented code. Comments are useful to both readers and authors.
    $endgroup$
    – user64494
    Jun 9 at 19:52







  • 4




    $begingroup$
    @user64494 I expect some effort from the reader: the analysis and exegesis of other people's code snippets is a great learning tool. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
    $endgroup$
    – Roman
    Jun 9 at 20:32










  • $begingroup$
    @Roman, I like the 1st solution you provided because that's what I can remember in brain once I learn it. I was struggling with Part, but yours enlightened me. I'll need to understand better the Through approach. First time I heard of this function. Thanks.
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:12















5












$begingroup$

Yes you can use MapThread:



l3 = Join @@ MapThread[Function[x, y, Insert[x, #, 3] & /@ y], l1, l2]


Here's a more esoteric version that builds lists of mapping operators from l2 and then applies them to the elements of l1:



l3 = Join @@ MapThread[Through[#1[#2]] &, Map[Insert[#, 3] &, l2, 2], l1]


See here for a discussion of the Through[#1[#2]]& operator.






share|improve this answer











$endgroup$












  • $begingroup$
    A good code is a commented code. Comments are useful to both readers and authors.
    $endgroup$
    – user64494
    Jun 9 at 19:52







  • 4




    $begingroup$
    @user64494 I expect some effort from the reader: the analysis and exegesis of other people's code snippets is a great learning tool. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
    $endgroup$
    – Roman
    Jun 9 at 20:32










  • $begingroup$
    @Roman, I like the 1st solution you provided because that's what I can remember in brain once I learn it. I was struggling with Part, but yours enlightened me. I'll need to understand better the Through approach. First time I heard of this function. Thanks.
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:12













5












5








5





$begingroup$

Yes you can use MapThread:



l3 = Join @@ MapThread[Function[x, y, Insert[x, #, 3] & /@ y], l1, l2]


Here's a more esoteric version that builds lists of mapping operators from l2 and then applies them to the elements of l1:



l3 = Join @@ MapThread[Through[#1[#2]] &, Map[Insert[#, 3] &, l2, 2], l1]


See here for a discussion of the Through[#1[#2]]& operator.






share|improve this answer











$endgroup$



Yes you can use MapThread:



l3 = Join @@ MapThread[Function[x, y, Insert[x, #, 3] & /@ y], l1, l2]


Here's a more esoteric version that builds lists of mapping operators from l2 and then applies them to the elements of l1:



l3 = Join @@ MapThread[Through[#1[#2]] &, Map[Insert[#, 3] &, l2, 2], l1]


See here for a discussion of the Through[#1[#2]]& operator.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 10 at 11:24

























answered Jun 9 at 19:51









RomanRoman

11.6k1 gold badge19 silver badges45 bronze badges




11.6k1 gold badge19 silver badges45 bronze badges











  • $begingroup$
    A good code is a commented code. Comments are useful to both readers and authors.
    $endgroup$
    – user64494
    Jun 9 at 19:52







  • 4




    $begingroup$
    @user64494 I expect some effort from the reader: the analysis and exegesis of other people's code snippets is a great learning tool. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
    $endgroup$
    – Roman
    Jun 9 at 20:32










  • $begingroup$
    @Roman, I like the 1st solution you provided because that's what I can remember in brain once I learn it. I was struggling with Part, but yours enlightened me. I'll need to understand better the Through approach. First time I heard of this function. Thanks.
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:12
















  • $begingroup$
    A good code is a commented code. Comments are useful to both readers and authors.
    $endgroup$
    – user64494
    Jun 9 at 19:52







  • 4




    $begingroup$
    @user64494 I expect some effort from the reader: the analysis and exegesis of other people's code snippets is a great learning tool. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
    $endgroup$
    – Roman
    Jun 9 at 20:32










  • $begingroup$
    @Roman, I like the 1st solution you provided because that's what I can remember in brain once I learn it. I was struggling with Part, but yours enlightened me. I'll need to understand better the Through approach. First time I heard of this function. Thanks.
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:12















$begingroup$
A good code is a commented code. Comments are useful to both readers and authors.
$endgroup$
– user64494
Jun 9 at 19:52





$begingroup$
A good code is a commented code. Comments are useful to both readers and authors.
$endgroup$
– user64494
Jun 9 at 19:52





4




4




$begingroup$
@user64494 I expect some effort from the reader: the analysis and exegesis of other people's code snippets is a great learning tool. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
$endgroup$
– Roman
Jun 9 at 20:32




$begingroup$
@user64494 I expect some effort from the reader: the analysis and exegesis of other people's code snippets is a great learning tool. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
$endgroup$
– Roman
Jun 9 at 20:32












$begingroup$
@Roman, I like the 1st solution you provided because that's what I can remember in brain once I learn it. I was struggling with Part, but yours enlightened me. I'll need to understand better the Through approach. First time I heard of this function. Thanks.
$endgroup$
– Bemtevi77
Jun 9 at 22:12




$begingroup$
@Roman, I like the 1st solution you provided because that's what I can remember in brain once I learn it. I was struggling with Part, but yours enlightened me. I'll need to understand better the Through approach. First time I heard of this function. Thanks.
$endgroup$
– Bemtevi77
Jun 9 at 22:12













5












$begingroup$

You can also MapThread the function Thread[Insert[#, #2, 3]] & on the pair of lists l1,l2:



Join @@ MapThread[Thread[Insert[#, #2, 3]] &, l1, l2]



Mn, Mn1, 1, 1, 0., 1., B, 1.4, Mn, Mn1, 11, 11, 0., 1., B, 1.4,

Al, Al1, 2, 2, 0., 1., B, 1.4, Al, Al1, 22, 22, 0., 1., B, 1.4, Al, Al1, 222, 222, 0., 1., B, 1.4




Alternatively, use the MapThread/Thread combination to create pairings appended with 3 and apply Insert to the resulting triples:



Join @@ Apply[Insert, 
MapThread[Thread[##, 3, List, 2] &, l1, l2],
2]



same result







share|improve this answer











$endgroup$








  • 1




    $begingroup$
    Yes that's what I was looking for! Thanks. Prefix it with Join@@ to match the spec.
    $endgroup$
    – Roman
    Jun 9 at 21:05










  • $begingroup$
    @kglr, I never think of using Thread function before reading your answer. It's a little bit difficult for me to appreciate the mechanism of Thread. It's written "threads" f over any lists that appear in args in MMA's help page. But elements of l1 and l2 are both lists. I think Insert plays a role here so that the function only threads over element of l2. Am I understanding correctly? Thanks
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:19










  • $begingroup$
    @Yaofeng, you are right for the first one. In the second, the second and third arguments of Thread controls what to thread over and in which positions.
    $endgroup$
    – kglr
    Jun 9 at 22:37










  • $begingroup$
    @kglr, I compared the AbsoluteTiming for your Thread solution and Roman's Function solution. Yours is faster. Although it's not intuitive for me at the moment, but I guess that's the direction for me to go, in line with MMA's vectorization. Thanks again!
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:48










  • $begingroup$
    Although I think your Thread[Insert[##,3]] method is the most poetic, it's also the most brittle: Inserting first and Threading second makes the assumption that none of the elements of the lists in l1 are themselves lists. Example: with l1 = "Mn", "Mn1", 1., "B", 1.4, "Al", "Al1", 1., "B", 1.4 this method throws a Thread::tdlen. To be more robust it's probably advisable to Thread first and Insert second, as in your second method.
    $endgroup$
    – Roman
    Jun 10 at 7:55
















5












$begingroup$

You can also MapThread the function Thread[Insert[#, #2, 3]] & on the pair of lists l1,l2:



Join @@ MapThread[Thread[Insert[#, #2, 3]] &, l1, l2]



Mn, Mn1, 1, 1, 0., 1., B, 1.4, Mn, Mn1, 11, 11, 0., 1., B, 1.4,

Al, Al1, 2, 2, 0., 1., B, 1.4, Al, Al1, 22, 22, 0., 1., B, 1.4, Al, Al1, 222, 222, 0., 1., B, 1.4




Alternatively, use the MapThread/Thread combination to create pairings appended with 3 and apply Insert to the resulting triples:



Join @@ Apply[Insert, 
MapThread[Thread[##, 3, List, 2] &, l1, l2],
2]



same result







share|improve this answer











$endgroup$








  • 1




    $begingroup$
    Yes that's what I was looking for! Thanks. Prefix it with Join@@ to match the spec.
    $endgroup$
    – Roman
    Jun 9 at 21:05










  • $begingroup$
    @kglr, I never think of using Thread function before reading your answer. It's a little bit difficult for me to appreciate the mechanism of Thread. It's written "threads" f over any lists that appear in args in MMA's help page. But elements of l1 and l2 are both lists. I think Insert plays a role here so that the function only threads over element of l2. Am I understanding correctly? Thanks
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:19










  • $begingroup$
    @Yaofeng, you are right for the first one. In the second, the second and third arguments of Thread controls what to thread over and in which positions.
    $endgroup$
    – kglr
    Jun 9 at 22:37










  • $begingroup$
    @kglr, I compared the AbsoluteTiming for your Thread solution and Roman's Function solution. Yours is faster. Although it's not intuitive for me at the moment, but I guess that's the direction for me to go, in line with MMA's vectorization. Thanks again!
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:48










  • $begingroup$
    Although I think your Thread[Insert[##,3]] method is the most poetic, it's also the most brittle: Inserting first and Threading second makes the assumption that none of the elements of the lists in l1 are themselves lists. Example: with l1 = "Mn", "Mn1", 1., "B", 1.4, "Al", "Al1", 1., "B", 1.4 this method throws a Thread::tdlen. To be more robust it's probably advisable to Thread first and Insert second, as in your second method.
    $endgroup$
    – Roman
    Jun 10 at 7:55














5












5








5





$begingroup$

You can also MapThread the function Thread[Insert[#, #2, 3]] & on the pair of lists l1,l2:



Join @@ MapThread[Thread[Insert[#, #2, 3]] &, l1, l2]



Mn, Mn1, 1, 1, 0., 1., B, 1.4, Mn, Mn1, 11, 11, 0., 1., B, 1.4,

Al, Al1, 2, 2, 0., 1., B, 1.4, Al, Al1, 22, 22, 0., 1., B, 1.4, Al, Al1, 222, 222, 0., 1., B, 1.4




Alternatively, use the MapThread/Thread combination to create pairings appended with 3 and apply Insert to the resulting triples:



Join @@ Apply[Insert, 
MapThread[Thread[##, 3, List, 2] &, l1, l2],
2]



same result







share|improve this answer











$endgroup$



You can also MapThread the function Thread[Insert[#, #2, 3]] & on the pair of lists l1,l2:



Join @@ MapThread[Thread[Insert[#, #2, 3]] &, l1, l2]



Mn, Mn1, 1, 1, 0., 1., B, 1.4, Mn, Mn1, 11, 11, 0., 1., B, 1.4,

Al, Al1, 2, 2, 0., 1., B, 1.4, Al, Al1, 22, 22, 0., 1., B, 1.4, Al, Al1, 222, 222, 0., 1., B, 1.4




Alternatively, use the MapThread/Thread combination to create pairings appended with 3 and apply Insert to the resulting triples:



Join @@ Apply[Insert, 
MapThread[Thread[##, 3, List, 2] &, l1, l2],
2]



same result








share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 10 at 1:04

























answered Jun 9 at 21:02









kglrkglr

200k10 gold badges230 silver badges456 bronze badges




200k10 gold badges230 silver badges456 bronze badges







  • 1




    $begingroup$
    Yes that's what I was looking for! Thanks. Prefix it with Join@@ to match the spec.
    $endgroup$
    – Roman
    Jun 9 at 21:05










  • $begingroup$
    @kglr, I never think of using Thread function before reading your answer. It's a little bit difficult for me to appreciate the mechanism of Thread. It's written "threads" f over any lists that appear in args in MMA's help page. But elements of l1 and l2 are both lists. I think Insert plays a role here so that the function only threads over element of l2. Am I understanding correctly? Thanks
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:19










  • $begingroup$
    @Yaofeng, you are right for the first one. In the second, the second and third arguments of Thread controls what to thread over and in which positions.
    $endgroup$
    – kglr
    Jun 9 at 22:37










  • $begingroup$
    @kglr, I compared the AbsoluteTiming for your Thread solution and Roman's Function solution. Yours is faster. Although it's not intuitive for me at the moment, but I guess that's the direction for me to go, in line with MMA's vectorization. Thanks again!
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:48










  • $begingroup$
    Although I think your Thread[Insert[##,3]] method is the most poetic, it's also the most brittle: Inserting first and Threading second makes the assumption that none of the elements of the lists in l1 are themselves lists. Example: with l1 = "Mn", "Mn1", 1., "B", 1.4, "Al", "Al1", 1., "B", 1.4 this method throws a Thread::tdlen. To be more robust it's probably advisable to Thread first and Insert second, as in your second method.
    $endgroup$
    – Roman
    Jun 10 at 7:55













  • 1




    $begingroup$
    Yes that's what I was looking for! Thanks. Prefix it with Join@@ to match the spec.
    $endgroup$
    – Roman
    Jun 9 at 21:05










  • $begingroup$
    @kglr, I never think of using Thread function before reading your answer. It's a little bit difficult for me to appreciate the mechanism of Thread. It's written "threads" f over any lists that appear in args in MMA's help page. But elements of l1 and l2 are both lists. I think Insert plays a role here so that the function only threads over element of l2. Am I understanding correctly? Thanks
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:19










  • $begingroup$
    @Yaofeng, you are right for the first one. In the second, the second and third arguments of Thread controls what to thread over and in which positions.
    $endgroup$
    – kglr
    Jun 9 at 22:37










  • $begingroup$
    @kglr, I compared the AbsoluteTiming for your Thread solution and Roman's Function solution. Yours is faster. Although it's not intuitive for me at the moment, but I guess that's the direction for me to go, in line with MMA's vectorization. Thanks again!
    $endgroup$
    – Bemtevi77
    Jun 9 at 22:48










  • $begingroup$
    Although I think your Thread[Insert[##,3]] method is the most poetic, it's also the most brittle: Inserting first and Threading second makes the assumption that none of the elements of the lists in l1 are themselves lists. Example: with l1 = "Mn", "Mn1", 1., "B", 1.4, "Al", "Al1", 1., "B", 1.4 this method throws a Thread::tdlen. To be more robust it's probably advisable to Thread first and Insert second, as in your second method.
    $endgroup$
    – Roman
    Jun 10 at 7:55








1




1




$begingroup$
Yes that's what I was looking for! Thanks. Prefix it with Join@@ to match the spec.
$endgroup$
– Roman
Jun 9 at 21:05




$begingroup$
Yes that's what I was looking for! Thanks. Prefix it with Join@@ to match the spec.
$endgroup$
– Roman
Jun 9 at 21:05












$begingroup$
@kglr, I never think of using Thread function before reading your answer. It's a little bit difficult for me to appreciate the mechanism of Thread. It's written "threads" f over any lists that appear in args in MMA's help page. But elements of l1 and l2 are both lists. I think Insert plays a role here so that the function only threads over element of l2. Am I understanding correctly? Thanks
$endgroup$
– Bemtevi77
Jun 9 at 22:19




$begingroup$
@kglr, I never think of using Thread function before reading your answer. It's a little bit difficult for me to appreciate the mechanism of Thread. It's written "threads" f over any lists that appear in args in MMA's help page. But elements of l1 and l2 are both lists. I think Insert plays a role here so that the function only threads over element of l2. Am I understanding correctly? Thanks
$endgroup$
– Bemtevi77
Jun 9 at 22:19












$begingroup$
@Yaofeng, you are right for the first one. In the second, the second and third arguments of Thread controls what to thread over and in which positions.
$endgroup$
– kglr
Jun 9 at 22:37




$begingroup$
@Yaofeng, you are right for the first one. In the second, the second and third arguments of Thread controls what to thread over and in which positions.
$endgroup$
– kglr
Jun 9 at 22:37












$begingroup$
@kglr, I compared the AbsoluteTiming for your Thread solution and Roman's Function solution. Yours is faster. Although it's not intuitive for me at the moment, but I guess that's the direction for me to go, in line with MMA's vectorization. Thanks again!
$endgroup$
– Bemtevi77
Jun 9 at 22:48




$begingroup$
@kglr, I compared the AbsoluteTiming for your Thread solution and Roman's Function solution. Yours is faster. Although it's not intuitive for me at the moment, but I guess that's the direction for me to go, in line with MMA's vectorization. Thanks again!
$endgroup$
– Bemtevi77
Jun 9 at 22:48












$begingroup$
Although I think your Thread[Insert[##,3]] method is the most poetic, it's also the most brittle: Inserting first and Threading second makes the assumption that none of the elements of the lists in l1 are themselves lists. Example: with l1 = "Mn", "Mn1", 1., "B", 1.4, "Al", "Al1", 1., "B", 1.4 this method throws a Thread::tdlen. To be more robust it's probably advisable to Thread first and Insert second, as in your second method.
$endgroup$
– Roman
Jun 10 at 7:55





$begingroup$
Although I think your Thread[Insert[##,3]] method is the most poetic, it's also the most brittle: Inserting first and Threading second makes the assumption that none of the elements of the lists in l1 are themselves lists. Example: with l1 = "Mn", "Mn1", 1., "B", 1.4, "Al", "Al1", 1., "B", 1.4 this method throws a Thread::tdlen. To be more robust it's probably advisable to Thread first and Insert second, as in your second method.
$endgroup$
– Roman
Jun 10 at 7:55


















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f200024%2fgenerate-certain-list-from-two-lists%23new-answer', 'question_page');

);

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







Popular posts from this blog

Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070