Skip to main content
added 606 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
expr = {{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 4};

You can also get the desired result by Mapping List on expr with level spec {-1}

Map[List, expr, {-1}]
{{{{{1}, {2}}, {{1}, {3}}, {{2}, {3}}}, {{3}, {2}, {1}}}, {4}}

Level >> Details and Options:

enter image description here

Level[expr, {-1}]
{1, 2, 1, 3, 2, 3, 3, 2, 1, 4}

Update:

"as a clarification: I mean all integer elements of a list, not all integers in the list, so in the following example, only 4 should be replaced by {4}":

You can use the replacement rule x_?AtomQ :> {x} (or x_ /; AtomQ[x] :> {x}) and level specification 1 in Replace:

Replace[
  {Sin[xx], 
   xx, 
   1 + xx, 
   {{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 
   {5}, 
   4}, 
  x_?AtomQ :> {x}, 
  1]
{Sin[xx], 
 {xx}, 
 1 + xx, 
 {{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 
 {5}, 
 {4}}
expr = {{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 4};

You can also get the desired result by Mapping List on expr with level spec {-1}

Map[List, expr, {-1}]
{{{{{1}, {2}}, {{1}, {3}}, {{2}, {3}}}, {{3}, {2}, {1}}}, {4}}

Level >> Details and Options:

enter image description here

Level[expr, {-1}]
{1, 2, 1, 3, 2, 3, 3, 2, 1, 4}
expr = {{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 4};

You can also get the desired result by Mapping List on expr with level spec {-1}

Map[List, expr, {-1}]
{{{{{1}, {2}}, {{1}, {3}}, {{2}, {3}}}, {{3}, {2}, {1}}}, {4}}

Level >> Details and Options:

enter image description here

Level[expr, {-1}]
{1, 2, 1, 3, 2, 3, 3, 2, 1, 4}

Update:

"as a clarification: I mean all integer elements of a list, not all integers in the list, so in the following example, only 4 should be replaced by {4}":

You can use the replacement rule x_?AtomQ :> {x} (or x_ /; AtomQ[x] :> {x}) and level specification 1 in Replace:

Replace[
  {Sin[xx], 
   xx, 
   1 + xx, 
   {{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 
   {5}, 
   4}, 
  x_?AtomQ :> {x}, 
  1]
{Sin[xx], 
 {xx}, 
 1 + xx, 
 {{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 
 {5}, 
 {4}}
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

expr = {{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 4};

You can also get the desired result by Mapping List on expr with level spec {-1}

Map[List, expr, {-1}]
{{{{{1}, {2}}, {{1}, {3}}, {{2}, {3}}}, {{3}, {2}, {1}}}, {4}}

Level >> Details and Options:

enter image description here

Level[expr, {-1}]
{1, 2, 1, 3, 2, 3, 3, 2, 1, 4}