Skip to main content
added 833 characters in body
Source Link
a20
  • 922
  • 5
  • 18

I am sure there are a hundred other solutions, but this one does the job and should be reasonably efficient:

In: Map[If[ListQ[#], #, Replace[#, 0 -> 1]] &, {{0, 0}, 0}]
Out: {{0,0},1}

Another better solution:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 1]
Out: {{0, 0}, 1, 1, 1, {0, 0, 0}, {{0, 0}, 0}, 1, 1}

The final '1' in the Replace[] expression indicates the level to act on. If you change it to '2' you instead get:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 2]
Out: {{1, 1}, 1, 1, 1, {1, 1, 1}, {{0, 0}, 1}, 1, 1}

EDIT

A word of caution on the solution using Replace[], this will only work if all elements with zeros that you want to replace only contain zeros. Say that you want to replace 'x' in the following:

In: Replace[{x, x + a, {x}}, x -> 1, 1]
Out: {1, a + x, {x}}

you see that the 'x' in the second element was not replaced. This is because it is at level 2, and not level 1. A possible solution to this is to use the Map[] as follows:

In: Map[If[ListQ[#], #, Replace[#, x -> 1, All]] &, {x, x + a, {x}}]
Out: {1, 1 + a, {x}}

note the 'All' at the level specification for Replace here. This means that 'x' on any level will be replaced, but only if the element is not a list.

Once again, there is probably a neater solution here to this more general case. Maybe someone else has a suggestion.

I am sure there are a hundred other solutions, but this one does the job and should be reasonably efficient:

In: Map[If[ListQ[#], #, Replace[#, 0 -> 1]] &, {{0, 0}, 0}]
Out: {{0,0},1}

Another better solution:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 1]
Out: {{0, 0}, 1, 1, 1, {0, 0, 0}, {{0, 0}, 0}, 1, 1}

The final '1' in the Replace[] expression indicates the level to act on. If you change it to '2' you instead get:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 2]
Out: {{1, 1}, 1, 1, 1, {1, 1, 1}, {{0, 0}, 1}, 1, 1}

I am sure there are a hundred other solutions, but this one does the job and should be reasonably efficient:

In: Map[If[ListQ[#], #, Replace[#, 0 -> 1]] &, {{0, 0}, 0}]
Out: {{0,0},1}

Another better solution:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 1]
Out: {{0, 0}, 1, 1, 1, {0, 0, 0}, {{0, 0}, 0}, 1, 1}

The final '1' in the Replace[] expression indicates the level to act on. If you change it to '2' you instead get:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 2]
Out: {{1, 1}, 1, 1, 1, {1, 1, 1}, {{0, 0}, 1}, 1, 1}

EDIT

A word of caution on the solution using Replace[], this will only work if all elements with zeros that you want to replace only contain zeros. Say that you want to replace 'x' in the following:

In: Replace[{x, x + a, {x}}, x -> 1, 1]
Out: {1, a + x, {x}}

you see that the 'x' in the second element was not replaced. This is because it is at level 2, and not level 1. A possible solution to this is to use the Map[] as follows:

In: Map[If[ListQ[#], #, Replace[#, x -> 1, All]] &, {x, x + a, {x}}]
Out: {1, 1 + a, {x}}

note the 'All' at the level specification for Replace here. This means that 'x' on any level will be replaced, but only if the element is not a list.

Once again, there is probably a neater solution here to this more general case. Maybe someone else has a suggestion.

added 180 characters in body
Source Link
a20
  • 922
  • 5
  • 18

I am sure there are a hundred other solutions, but this one does the job and should be reasonably efficient:

In: Map[If[ListQ[#], #, Replace[#, 0 -> 1]] &, {{0, 0}, 0}]
Out: {{0,0},1}

Another better solution:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 1]
Out: {{0, 0}, 1, 1, 1, {0, 0, 0}, {{0, 0}, 0}, 1, 1}

The final '1' in the Replace[] expression indicates the level to act on. If you change it to '2' you instead get:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 2]
Out: {{1, 1}, 1, 1, 1, {1, 1, 1}, {{0, 0}, 1}, 1, 1}

I am sure there are a hundred other solutions, but this one does the job and should be reasonably efficient:

In: Map[If[ListQ[#], #, Replace[#, 0 -> 1]] &, {{0, 0}, 0}]
Out: {{0,0},1}

Another better solution:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 1]
Out: {{0, 0}, 1, 1, 1, {0, 0, 0}, {{0, 0}, 0}, 1, 1}

The final '1' in the Replace[] expression indicates the level to act on.

I am sure there are a hundred other solutions, but this one does the job and should be reasonably efficient:

In: Map[If[ListQ[#], #, Replace[#, 0 -> 1]] &, {{0, 0}, 0}]
Out: {{0,0},1}

Another better solution:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 1]
Out: {{0, 0}, 1, 1, 1, {0, 0, 0}, {{0, 0}, 0}, 1, 1}

The final '1' in the Replace[] expression indicates the level to act on. If you change it to '2' you instead get:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 2]
Out: {{1, 1}, 1, 1, 1, {1, 1, 1}, {{0, 0}, 1}, 1, 1}
Source Link
a20
  • 922
  • 5
  • 18

I am sure there are a hundred other solutions, but this one does the job and should be reasonably efficient:

In: Map[If[ListQ[#], #, Replace[#, 0 -> 1]] &, {{0, 0}, 0}]
Out: {{0,0},1}

Another better solution:

In: Replace[{{0, 0}, 0, 0, 0, {0, 0, 0}, {{0, 0}, 0}, 0, 0}, 0 -> 1, 1]
Out: {{0, 0}, 1, 1, 1, {0, 0, 0}, {{0, 0}, 0}, 1, 1}

The final '1' in the Replace[] expression indicates the level to act on.