Skip to main content
added 165 characters in body
Source Link

I need to replace all integers k in a list with {k} and tried:

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

(# /; AtomQ[#] -> {#} &) /@ {{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 4}

which gives the result

{{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}} /; 
   AtomQ[{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}] -> {{{{1, 2}, {1, 
      3}, {2, 3}}, {3, 2, 1}}}, 4 /; AtomQ[4] -> {4}}

the command

(# -> {#}) & /@ {1, {2}}

works fine (i.e. encloses all elements in brackets). Replacements can be restricted via conditionals according to Mathematica documentation.

I am not clear on why

(# /; AtomQ[#] -> {#} &) 

does not have the desired effect of enclosing 4 in brackets (in the example given at the outset)

I need to replace all integers k in a list with {k} and tried:

(# /; AtomQ[#] -> {#} &) /@ {{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 4}

which gives the result

{{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}} /; 
   AtomQ[{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}] -> {{{{1, 2}, {1, 
      3}, {2, 3}}, {3, 2, 1}}}, 4 /; AtomQ[4] -> {4}}

the command

(# -> {#}) & /@ {1, {2}}

works fine (i.e. encloses all elements in brackets). Replacements can be restricted via conditionals according to Mathematica documentation.

I am not clear on why

(# /; AtomQ[#] -> {#} &) 

does not have the desired effect of enclosing 4 in brackets (in the example given at the outset)

I need to replace all integers k in a list with {k} and tried:

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

(# /; AtomQ[#] -> {#} &) /@ {{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 4}

which gives the result

{{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}} /; 
   AtomQ[{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}] -> {{{{1, 2}, {1, 
      3}, {2, 3}}, {3, 2, 1}}}, 4 /; AtomQ[4] -> {4}}

the command

(# -> {#}) & /@ {1, {2}}

works fine (i.e. encloses all elements in brackets). Replacements can be restricted via conditionals according to Mathematica documentation.

I am not clear on why

(# /; AtomQ[#] -> {#} &) 

does not have the desired effect of enclosing 4 in brackets (in the example given at the outset)

Source Link

Replacements: using condition to restrict to parts that satisfy a Boolean expression

I need to replace all integers k in a list with {k} and tried:

(# /; AtomQ[#] -> {#} &) /@ {{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}, 4}

which gives the result

{{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}} /; 
   AtomQ[{{{1, 2}, {1, 3}, {2, 3}}, {3, 2, 1}}] -> {{{{1, 2}, {1, 
      3}, {2, 3}}, {3, 2, 1}}}, 4 /; AtomQ[4] -> {4}}

the command

(# -> {#}) & /@ {1, {2}}

works fine (i.e. encloses all elements in brackets). Replacements can be restricted via conditionals according to Mathematica documentation.

I am not clear on why

(# /; AtomQ[#] -> {#} &) 

does not have the desired effect of enclosing 4 in brackets (in the example given at the outset)