5
$\begingroup$

If I have this three lists such as the following:

list1={{0.01,0.},{0.03,0.18353},{0.1,0.494987},{0.3,0.899803},{1.,1.08076},{3.,1.10593},{10.,1.04781},{10.,1.02449},{10.,0.964193},{30.,1.0602},{30.,1.04075},{30.,1.05987},{100.,1.14661},{100.,1.00639},{100.,1.09384},{300.,1.067},{300.,1.15047},{300.,1.10715},{1000.,1.05152},{1000.,1.06942},{1000.,1.17143},{3000.,1.12162},{10000.,1.13136}}

list2={{0.01,0.},{0.03,0.},{0.1,0.12702},{0.3,0.284862},{1.,0.330209},{3.,0.490592},{10.,0.864811},{30.,0.951112},{100.,0.924481},{300.,1.02702},{1000.,1.12306},{3000.,1.061},{10000.,1.08021}}

list3={{0.01,0.},{0.03,0.},{0.1,0.215019},{0.3,0.702618},{1.,1.27573},{3.,1.02397},{10.,1.1375},{10.,1.14245},{10.,0.945541},{30.,1.33324},{30.,1.07789},{30.,1.12324},{100.,1.14999},{100.,0.985026},{100.,1.1228},{300.,1.25291},{300.,1.29771},{300.,1.40179},{1000.,1.26045},{1000.,1.17871},{1000.,1.32166},{3000.,1.27041},{10000.,1.23387}}

How can I put a given number in between each data set of each list?. For example, if I want to place the number 10 in between in list 1 it should look like: list1={{0.01,10,0.},{0.03,10,0.18353},{0.1,10,0.494987}...etc. If I want to put the number 20 in between list 2 it should look like {{0.01,20,0.},{0.03,20,0.},{0.1,20,0.12702}....etc and so on for list 3.

Thank you in advanced,

$\endgroup$
2
  • 3
    $\begingroup$ Riffle[#, 10] & /@ list1 and Riffle[#, 20] & /@ list2 etc... $\endgroup$
    – kglr
    May 2, 2020 at 4:10
  • $\begingroup$ @kglr Thank you very much! This works great! Exactly what I wanted $\endgroup$
    – John
    May 2, 2020 at 4:15

4 Answers 4

2
$\begingroup$
ClearAll[threadRiffle]
threadRiffle = MapThread[Map[Function[x, Riffle[x, #2]]]@# &, {##}] &;

threadRiffle[{list1, list2, list3}, {10, 20, xx}]
 {{0.01, 10, 0.}, {0.03, 10, 0.18353}, {0.1, 10, 0.494987}, {0.3, 10, 0.899803}, 
   {1., 10, 1.08076}, {3., 10, 1.10593}, {10., 10,  1.04781}, {10., 10, 1.02449}, 
   {10., 10, 0.964193}, {30., 10, 1.0602}, {30., 10, 1.04075}, {30., 10, 1.05987}, 
   {100., 10,  1.14661}, {100., 10, 1.00639}, {100., 10, 1.09384}, {300., 10,  1.067},
   {300., 10, 1.15047}, {300., 10, 1.10715}, {1000., 10,   1.05152}, 
   {1000., 10, 1.06942}, {1000., 10, 1.17143}, {3000., 10,  1.12162}, 
   {10000., 10, 1.13136}},
  {{0.01, 20, 0.}, {0.03, 20,  0.}, {0.1, 20, 0.12702}, {0.3, 20, 0.284862},
   {1., 20,  0.330209}, {3., 20, 0.490592}, {10., 20, 0.864811},
   {30., 20,  0.951112}, {100., 20, 0.924481}, {300., 20, 1.02702}, 
   {1000., 20,  1.12306}, {3000., 20, 1.061}, {10000., 20, 1.08021}}, 
   {{0.01, xx,  0.}, {0.03, xx, 0.}, {0.1, xx, 0.215019}, {0.3, xx, 0.702618}, 
  {1.,  xx, 1.27573}, {3., xx, 1.02397}, {10., xx, 1.1375}, 
   {10., xx,  1.14245}, {10., xx, 0.945541}, {30., xx, 1.33324}, 
   {30., xx,  1.07789}, {30., xx, 1.12324}, {100., xx, 1.14999}, 
   {100., xx,  0.985026}, {100., xx, 1.1228}, {300., xx, 1.25291}, 
   {300., xx, 1.29771}, {300., xx, 1.40179}, {1000., xx, 1.26045}, 
   {1000., xx,  1.17871}, {1000., xx, 1.32166}, {3000., xx, 1.27041}, 
   {10000., xx, 1.23387}}}

Alternatively, you can use Insert instead of Riffle:

ClearAll[threadInsert]
threadInsert[p_: 2] := MapThread[Map[Function[x, Insert[x, #2, p]]]@# &, {##}] &;

threadInsert[][{list1, list2, list3}, {10, 20, xx}] == 
  threadRiffle[{list1, list2, list3}, {10, 20, xx}]
True
$\endgroup$
1
$\begingroup$
a = {{0.1, 0.2}, {0.3, 0.1}, {0.1, 0.4}, {0.5, 0.8}};

b = {{0.2, 0.3}, {0.90, 0.4}};

c = {{0.3, 0.5}, {0.7, 0.1}, {0.2, 0.4}};

Using MapThread with Replace

MapThread[Replace[#1, {x_, y_} :> {x, #2, y}, {1}] &, {{a, b, c}, {10, 20, 30}}]

returns

{{{0.1, 10, 0.2}, {0.3, 10, 0.1}, {0.1, 10, 0.4}, {0.5, 10, 0.8}}, 
 {{0.2, 20, 0.3}, {0.9, 20, 0.4}}, 
 {{0.3, 30, 0.5}, {0.7, 30, 0.1}, {0.2, 30, 0.4}}}
$\endgroup$
1
$\begingroup$
l1 = {{0.1, 0.2}, {0.3, 0.1}, {0.1, 0.4}, {0.5, 0.8}};
l2 = {{0.2, 0.3}, {0.90, 0.4}};
l3 = {{0.3, 0.5}, {0.7, 0.1}, {0.2, 0.4}};

Grabbing the @eldo's list and using SequenceCases:

f = SequenceCases[#1, {{a_, b_}} :> {a, #2, b}] &;

MapThread[f, {{l1, l2, l3}, {10, 20, 30}}]

Results:

{{{0.1, 10, 0.2}, {0.3, 10, 0.1}, {0.1, 10, 0.4}, {0.5, 10, 0.8}}, {{0.2, 20, 0.3}, {0.9, 20, 0.4}},
{{0.3, 30, 0.5}, {0.7, 30, 0.1}, {0.2, 30, 0.4}}}

Or using Insert:

f[l_, n_] := Insert[#, n, 2] & @@@ List /@ l;

MapThread[f[#1, #2] &, {{l1, l2, l3}, {10, 20, 30}}]

Results:

{{{0.1, 10, 0.2}, {0.3, 10, 0.1}, {0.1, 10, 0.4}, {0.5, 10, 0.8}}, {{0.2, 20, 0.3}, {0.9, 20, 0.4}},
{{0.3, 30, 0.5}, {0.7, 30, 0.1}, {0.2, 30, 0.4}}}

$\endgroup$
0
$\begingroup$

Here is a simple way

{#1, 10, #2} & @@@ list1

or use MapApply introduced in Mathematica 13.1 (it's the same function, @@@ is syntactic sugar)

MapApply[{#1, 10., #2} &, list1]

Both give

{{0.01, 10, 0.}, {0.03, 10, 0.18353}, {0.1, 10, 0.494987} ... {10000., 10, 1.13136}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.