I have two lists, one contains the variables and the other the corresponding values that the variables should take.
letters = {"a","m","t","h","e"}
bonus = {10, 20, 30, 40, 50}
words = {math, the, gps, log}
I know that this works (as in the manual of Mathematica):
{a,b,c} = {10,20,30}
But the lists are rather long, so I would like to write:
letters = bonus
Which does not work. Output: I would like to have the variables (letters) taking the values (bonus) a=10, b=20, c=30. Having the weight of the letters, it should be possible to compute the weight of every word in list "words" [math (100), the (120) etc.] I would appreciate some pointers to get me started. Thank you for your time.
List
areStrings
notSymbols
and therefore can't be assigned values. You might be better off creating aRule
for the letter values. $\endgroup$ – Quantum_Oli Mar 12 '16 at 20:28Clear[letters]; Evaluate@(Symbol /@ letters) = bonus
. This will create symbols from your strings, then assign them a value, i.e.a=10
etc. Beware that theClear
bit is important if you plan to run this code more than once. Notice, however, that you are NOT assigning a value TO THE STRING"a"
, but to the symbola
. This may still not work for what you want. AnAssociation
construct would probably work a lot better. $\endgroup$ – MarcoB Mar 12 '16 at 20:31"// Timing"
to time the processes. The three answers below demand different times with a dictionary of about 240 thousand words. Plus: 0.005087, Total: 0.013299 and Map: 0.039653. The Plus solution is thus the quickest. $\endgroup$ – JSP Mar 14 '16 at 15:48