3
$\begingroup$

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?

$\endgroup$

1 Answer 1

5
$\begingroup$
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]
$\endgroup$
2
  • $\begingroup$ Thank you for your answer. That will help immensely. $\endgroup$
    – hare
    Jun 14, 2023 at 5:10
  • $\begingroup$ @hare, my pleasure. Welcome to mma.se. $\endgroup$
    – kglr
    Jun 14, 2023 at 5:31

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.