7
$\begingroup$

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.

$\endgroup$
10
$\begingroup$
list // Map[Flatten]

{{Position, Code}, {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, {4, 1000, 1001}, {5, 1100, 1011}, {6, 1110, 1111}}

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ That's an original alternative to the – more common, I presume – Flatten /@. $\endgroup$ – corey979 Oct 20 at 19:50
6
$\begingroup$

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]
| improve this answer | |
$\endgroup$
6
$\begingroup$

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}}

| improve this answer | |
$\endgroup$
6
$\begingroup$

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:

enter image description here

☺ = ## & @@@ # & /@ # &;

☺ @ list
{{Position, Code}, {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, {4, 1000, 1001}, 
{5, 1100, 1011}, {6, 1110, 1111}}

enter image description here

☺☺ = {#, ## & @@ #2} & @@@ # &;

☺☺ @ list
{{Position, Code}, {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, {4, 1000, 1001}, 
{5, 1100, 1011}, {6, 1110, 1111}}
| improve this answer | |
$\endgroup$
  • 3
    $\begingroup$ A little explanation: ## & @@@ # & nearly == Flatten , a nice trick ;) . So the first 'riddle' == Flatten /@ list. $\endgroup$ – wuyudi Oct 20 at 12:05
  • $\begingroup$ Showing off, but still a... Wow! $\endgroup$ – nilo de roock Oct 22 at 9:36
5
$\begingroup$

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}}
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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