TransWikia.com

How to replace one parameter with multiple values

Mathematica Asked by schd on May 1, 2021

I am a beginner. I have a function with one parameter and I would like to replace a list of values that have names. I would like a result so that the names are not lost.

values = {a-> 1, b-> 2};

If I ignore the names, I get what I want:

Evaluate[x/6 /.x -> {1, 2}]
(*  {1/6, 1/3}  *)

But I would like to get a result like:

{a -> 1/6, b -> 1/3}

Is it possible?

2 Answers

values = {a -> 1, b -> 2};
asso = Association[values]
(#/6) & /@ asso // Normal

Answered by cvgmt on May 1, 2021

values = {a -> 1, b -> 2};
Association[values]/6
<|a -> 1/6, b -> 1/3|>
Normal @ %
{a -> 1/6, b -> 1/3}

Also

values /. x_?NumericQ :> x/6

MapAt[#/6 &, values, {All, 2}]

SubsetMap[#/6&, values, {All ,2}]

ReplacePart[values, {i_,2}:> values[[i,2]]/6]

Module[{v = #}, v[[All, 2]] = v[[All, 2]]/6; v] &@values

all give

{a -> 1/6, b -> 1/3}

Answered by kglr on May 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP