Mathematica Asked by GlomGive on December 30, 2020
If I have the result
{{Position, {Code}},
{1, {0000, 0001}},
{2, {0100, 0011}},
{3, {0110, 0111}},
{4, {1000, 1001}},
{5, {1100,1011}},
{6, {1110, 1111}}}
and I want it to be
{{Position, Code},
{1, 0000, 0001},
{2, 0100, 0011},
{3, 0110, 0111},
{4, 1000, 1001},
{5, 1100, 1011},
{6, 1110, 1111}}
I tried
//.{x_,{y_}}:> {x,y}
but that seems to work only for the first element.
Any help would be appreciated Thanks.
Try
list={{Position,{Code}},{1,{0000,0001}},{2,{0100,0011}},{3,{0110,0111}},{4,{1000,1001}},{5,{1100,1011}},{6,{1110,1111}}}
list /. {a_ , b_List } -> Join[{a}, b]
Answered by Ulrich Neumann on December 30, 2020
list // Map[Flatten]
{{Position, Code}, {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, {4, 1000, 1001}, {5, 1100, 1011}, {6, 1110, 1111}}
Answered by wuyudi on December 30, 2020
A few additional methods:
Level[#, {-1}] & /@ list
Apply[Sequence, list, {-2}]
Map[Splice, list, {-2}]
Join[{#}, #2] & @@@ list
Prepend[#2, #]& @@@ list
MapAt[Splice, {All, 2}] @ list
ReplacePart[{_, 2, 0} -> Sequence] @ list
Map[Map @ Apply @ Sequence] @ list
Delete[{2, 0}] /@ list
FlattenAt[#, 2] & /@ list
... and for the Halloween:
☺ = ## & @@@ # & /@ # &;
☺ @ list
{{Position, Code}, {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, {4, 1000, 1001}, {5, 1100, 1011}, {6, 1110, 1111}}
☺☺ = {#, ## & @@ #2} & @@@ # &;
☺☺ @ list
{{Position, Code}, {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, {4, 1000, 1001}, {5, 1100, 1011}, {6, 1110, 1111}}
Answered by kglr on December 30, 2020
ReplacePart
can do it.
data =
{{Position, {Code}},
{1, {0000, 0001}}, {2, {0100, 0011}}, {3, {0110, 0111}},
{4, {1000, 1001}}, {5, {1100, 1011}}, {6, {1110, 1111}}};
ReplacePart[data, {i_, 2} :> Sequence @@ data[[i, 2]]]
{{Position, Code}, {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, {4, 1000, 1001}, {5, 1100, 1011}, {6, 1110, 1111}}
Answered by m_goldberg on December 30, 2020
You were really close. You just need to add another underscore to y_
as
list = {
{Position, {Code}},
{1, {0000, 0001}},
{2, {0100, 0011}},
{3, {0110, 0111}},
{4, {1000, 1001}},
{5, {1100,1011}},
{6, {1110, 1111}}
};
list /. {x_, {y__}} :> {x, y}
{{Position, Code}, {1, 0000, 0001}, {2, 0100, 0011}, {3, 0110, 0111}, {4, 1000, 1001}, {5, 1100, 1011}, {6, 1110, 1111}}
Answered by NonDairyNeutrino on December 30, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP