3

Could you give me some advice?

Suppose I have the following list.

box[1,1]={{4.4, {1,1}, {a}}, {1.5, {1,1}, {a}}, {10.8, {2,1}, {3a}}, {10, {1, 3}, {3a}}}

I want to do the following.

When the location of Part[box[1, 1][[x]], 3] is {a}, apply it to the contents of the brackets as a=1.

When the location is not {a} (here are {3a} and{4a}), apply it to the contents of the brackets as a=0.1.

So,I want to construct formula that yields the following answer.

box[1,1]={{4.4, {1, 1}, {1}}, {1.5, {1, 1}, {1}}, {10.8, {5, 2}, {0.3}}, {10, {1, 3}, {0.3}}, {10, {1, 3}, {0.4}}}

I tryed to used "If" and "MenberQ". At that time, it was all calculated as a=1 or a=0.1. I felt like MenberQ is not distinct "a" and x"a". What should I do?

hare
  • 406
  • 1
  • 8

1 Answers1

5
box[1, 1] = 
  {{4.4, {1, 1}, {a}}, 
   {1.5, {1, 1}, {a}},
   {10.8, {2, 1}, {3 a}},
   {10, {1, 3}, {4 a}}};

box[1, 1] /. {{a} -> {1}, a -> .1}
 {{4.4, {1, 1}, {1}}, 
  {1.5, {1, 1}, {1}},
  {10.8, {2, 1}, {0.3}},
  {10, {1, 3}, {0.4}}}

Alternatively,

box[1, 1] /. {x_ a :> .1 x, a -> 1}

box[1, 1] /. x_. a :> If[x == 1, 1, .1 x]

kglr
  • 394,356
  • 18
  • 477
  • 896