Is there a way of smarter way of joining list of the form
l1 = {a,{b,c}};
l2 = {d,{e,f}};
l3 = {g,{h,i}};
To obtain
{a,d,g,{b,c,e,f,h,i}}
The code I have is
{Sequence @@ #1, Flatten[#2]} & @@ Transpose[{l1, l2, l3}]
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityIs there a way of smarter way of joining list of the form
l1 = {a,{b,c}};
l2 = {d,{e,f}};
l3 = {g,{h,i}};
To obtain
{a,d,g,{b,c,e,f,h,i}}
The code I have is
{Sequence @@ #1, Flatten[#2]} & @@ Transpose[{l1, l2, l3}]
How about this
MapThread[Join, {l1, l2, l3}] /. Join -> Sequence
{a, d, g, {b, c, e, f, h, i}}
This seems like a good way:
l1 = {a, {b, c}};
l2 = {d, {e, f}};
l3 = {g, {h, i}};
Apply[Sequence, Thread@{l1, l2, l3}, {-2}]
(* Out: {a, d, g, {b, c, e, f, h, i}} *)
It's been a few days, but I got distracted this morning from work and revisited this... My reward was seeing poetry (+1 btw), but I also killed a couple minutes running some timings on a sampling of the various answers:
ClearAll@t;
SetAttributes[t, HoldFirst];
t[e_, n_] := First@AbsoluteTiming[Do[e, {n}];]
OP:
t[
{Sequence @@ #1, Flatten[#2]} & @@ Transpose[{l1, l2, l3}],
10^6
]
(* Out: 3.849385 *)
This answer:
t[
Apply[Sequence, Thread@{l1, l2, l3}, {-2}],
10^6
]
(* Out: 2.882288 *)
Snapshot of a few other answers:
t[
MapThread[Join, {l1, l2, l3}] /. Join -> Sequence,
10^6
]
(* Out: 5.532553 *)
t[
MapAt[Sequence @@ # &, Transpose[{l1, l2, l3}], {{1}, {2, All}}],
10^6
]
(* Out: 6.293629 *)
t[
FlattenAt[Flatten /@ Transpose[{l1, l2, l3}], 1],
10^6
]
(* Out: 3.969397*)
(Performance of this operation is probably irrelevant for the OP's purposes, but I always enjoy playing on the performance side of things.)
-n
consists of all parts of expr
with depth n
." See ref/Depth for more explanation.
$\endgroup$
– William
Sep 26 '14 at 14:41
I don't know if this is "smarter". Anyway:
ClearAll@k; SetAttributes[k, Listable];
k @@ {l1, l2, l3} /. k -> Sequence
(* {a, d, g, {b, e, h, c, f, i}} *)
{a,d,g,{b,c,e,f,h,i}}
(Why he accepted this 囧 ?)
$\endgroup$
– xzczd
Sep 27 '14 at 4:44
♯ = {## & @@ #, ## & @@@ #2} & @@ ({##}) & ;
♯[l1, l2, l3]
(* {a, d, g, {b, c, e, f, h, i}} *)
See also: ♭ = ## & @@@ (## & @@@ {## & @@@ # & /@ #} & /@ #) &
m
with \[FivePointedStar]
.
$\endgroup$
– Michael E2
Sep 26 '14 at 17:51
#~♯~# == 42
. @Mr.Wizard remember to give attribution.
$\endgroup$
– Dr. belisarius
Sep 27 '14 at 5:20
My take:
MapAt[Sequence @@ # &, Transpose[{l1, l2, l3}], {{1}, {2, All}}]
{a, d, g, {b, c, e, f, h, i}}
This is another way:
FlattenAt[Flatten /@ Transpose[{l1, l2, l3}], 1]
{a, d, g, {b, c, e, f, h, i}}
One option:
l1 + l2 + l3 /. Plus -> Sequence
{a, d, g, {b, c, e, f, h, i}}
Re[l1, l2, l3] /. Re@x__ :> x
$\endgroup$
– Mr.Wizard
Sep 26 '14 at 22:19
{a, d, g, {b, e, h, c, f, i}}
rather than {a, d, g, {b, c, e, f, h, i}}
.
$\endgroup$
– xzczd
Sep 27 '14 at 4:50
Two very intuitive ways. First the Flattinator:
#2[#1[#1 /@ #1[{##3}, {2}], {1}], 1] &[Flatten, FlattenAt, l1, l2, l3]
and here the Padding-Miss-User
PadLeft[{Flatten[#2]}, 4, RotateLeft[#1]] & @@ Transpose[{l1, l2, l3}]
(Please don't use them. Just look and shake your head)
Rule based alternatives for completeness sake:
{l1, l2, l3} //. {{a__, {b__}}, {c_, {d__}},
rest : {_, {__}} ...} :> {{a, c, {b, d}}, rest} // First
{a, d, g, {b, c, e, f, h, i}}
And:
Flatten[{l1, l2, l3}, {2, 1}] /. {start__, rest : {_, _} ...} :> {start, Join[rest]}
{a, d, g, {b, c, e, f, h, i}}
Another solution :
Join[#[[1]], {#[[2]]}] &@(Flatten /@ Transpose@{l1, l2, l3})
(* {a, d, g, {b, c, e, f, h, i}} *)
Update
Transpose @ {l1, l2, l3} /. a:{__Symbol} :> Sequence @@ a
{a, d, g, {b, c, e, f, h, i}}
Original answer:
Append[First /@ #, Flatten[Last /@ #]] & [{l1, l2, l3}]
{a, d, g, {b, c, e, f, h, i}}
{a, d, g, {b, c, e, f, h, i}}
:D
$\endgroup$
– Dr. belisarius
Sep 25 '14 at 23:02
I would just add something for general case:
l1 = {a, h, u, {b, t, c}};
l2 = {d, e, t, y, {e, f}};
l3 = {g, {h, i}};
Append @@ ({Cases[#, _?(Head[#] =!= List &)],
Flatten[Cases[#, _List]]} &[Flatten[{l1, l2, l3}, 1]])
(*{a, h, u, d, e, t, y, g, {b, t, c, e, f, h, i}}*)