6
$\begingroup$

I am trying to join entries of binary numbers at the lowest level of a nested list, e.g.

testlist = {{{100, 100}, {101, 101}}, {{110, 110}, {111, 111}}, {{111,
     100}, {111, 110}}, {{101, 111}, {110, 101}}}

Using the Row function seems to work

rowlist = Table[Row[testlist[[i, j]]], {i, 4}, {j, 2}]

with the resulting output displayed as

{{100100,101101},{110110,111111},{111100,111110},{101111,110101}

which is the desired result. However when I check the first entry at the third level, i.e. rowlist[[1,1,1]] I get {100, 100} which suggests that this is merely a change of display rather than fundamentally changing the list, as does the output from rowlist when pasted here

{{Row[{100, 100}], Row[{101, 101}]}, {Row[{110, 110}], 
  Row[{111, 111}]}, {Row[{111, 100}], 
  Row[{111, 110}]}, {Row[{101, 111}], Row[{110, 101}]}}

How can I change the list so that the first entry at the deepest level is rowlist[[1,1]]=100100 ?

$\endgroup$
1
  • 2
    $\begingroup$ Map[{10^Floor[Log10[#[[2]]] + 1], 1} . # &, testlist, {2}] $\endgroup$
    – Bob Hanlon
    Apr 13, 2022 at 3:38

4 Answers 4

3
$\begingroup$
testlist = {{{100, 100}, {101, 101}}, {{110, 110}, {111, 111}}, {{111,
     100}, {111, 110}}, {{101, 111}, {110, 101}}}

Map[StringJoin,
 Map[ToString, testlist, {-1}]  
 , {-2}
 ]
{{"100100", "101101"}, {"110110", "111111"}, {"111100", 
  "111110"}, {"101111", "110101"}}

The inner Map applies to the leaves and converts the entries using ToString. Depending on the entries, some zero padding may be required in the general case.

The outer Map applies one level up and joins the strings produced in the first step using StringJoin.


EDIT

If you want to convert these strings (at leaf level now) to a decimal number, then add another Map. A string can be passed to FromDigits directly and uses base 10 as default.

Map[FromDigits,
 Map[StringJoin,
  Map[ToString, testlist, {-1}
   ]
  , {-2}
  ]
 , {-1}
 ] 
$\endgroup$
8
$\begingroup$
Map[FromDigits @* Flatten @* IntegerDigits, testlist, {2}]
{{100100, 101101}, {110110, 111111}, {111100, 111110}, {101111, 110101}}
$\endgroup$
7
$\begingroup$

Let's define a function that joins together two of the entries:

join[{x_, y_}] := ToExpression[StringJoin[{ToString[x], ToString[y]}]];

Now we can use this function to answer your question. With testlist as you have defined it above, you can get your desired form by:

Partition[join /@ Flatten[testlist, 1], 2]
{{100100, 101101}, {110110, 111111}, {111100, 111110}, {101111, 110101}}
$\endgroup$
2
$\begingroup$

1

{100, 100} -> 100100

f[{x_,y_}] = x*10^IntegerLength[x] + y;
f[{100,100}];

2

g = Map[f]
g[{{100, 100}, {101, 101}}]

get {100100,101101}

3

 {{{100, 100}, {101, 101}}, {{110, 110}, {111, 111}}, {{111,
     100}, {111, 110}}, {{101, 111}, {110, 101}}} // Map[g]
{{100100, 101101}, {110110, 111111}, {111100, 111110}, {101111, 110101}}

just step by step.

$\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.