Message Boards Message Boards

0
|
5706 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to assign numerical values to a list of strings?

Posted 10 years ago

(*I am a novice in Mathematica,and am finding a hard time in \ associating numerical values with a list of strings.Here is what I \ have been trying to do:*) list = ReadList[ "file path", Word, WordSeparators -> {","}];

(the list is a single column of several multi-character variables) cLength = Length[list]; cLength For[i = 1, i < cLength, i++, Keys[<|list[[i]] -> (0.0 + 0.005*i)|>]; Print[list[[i]]]]; x = 1; y = 2; x y

(* I get the below. While assigning "1" to x, and "2" to y, gives the correct association, The code above does not assign the numerical values to variable1 and variable2 as instructed in the "For" loop)

177

variable1 variable2 .... 1 2

Attachments:
POSTED BY: Randa Nakib
4 Replies
POSTED BY: David Reiss
Posted 10 years ago
Attachments:
POSTED BY: Randa Nakib
Posted 10 years ago
POSTED BY: Randa Nakib

Here is a more Mathematica-like approach to what I think you are looking for. Note that it is not using procedural constructs, and studiously avoids the Print statement. But I'd also suggest that Associations are perhaps not the best starting point for what you want to do, if only because they are a quite recent addition to Mathematica. And you may want to consider working with a simple list of rules first. However, here's my interpretation of what you are trying to do:

First I make some data like what I think you are trying to use:

list = StringSplit[
  "This is an interesting string with words that do not repeat"]

giving

{"This", "is", "an", "interesting", "string", "with", "words", \
"that", "do", "not", "repeat"}

Now build an association from it and name it:

anAssociation = <| Table[list[[i]] -> (0.0 + 0.005*i), {i, 1, Length[list]}]|>

This gives:

<|"This" -> 0.005, "is" -> 0.01, "an" -> 0.015, "interesting" -> 0.02,
  "string" -> 0.025, "with" -> 0.03, "words" -> 0.035, "that" -> 0.04,
  "do" -> 0.045, "not" -> 0.05, "repeat" -> 0.055|>

Here are the keys from this Association:

Keys[anAssociation]

which gives

{"This", "is", "an", "interesting", "string", "with", "words", \
"that", "do", "not", "repeat"}

And let's extract the numerical values associated with the strings

Table[anAssociation[[list[[i]]]], {i, Length[list]}]

which gives:

{0.005, 0.01, 0.015, 0.02, 0.025, 0.03, 0.035, 0.04, 0.045, 0.05, 0.055}

or you could do this with a pure function

anAssociation[[#]] & /@ Keys[anAssociation]

which gives the same result.

But a perfectly workable approach uses replacement rules directly.

Define a set of rules for replacing strings by numerical values

stringRules =  Table[list[[i]] -> (0.0 + 0.005*i), {i, 1, Length[list]}]

giving

{"This" -> 0.005, "is" -> 0.01, "an" -> 0.015, "interesting" -> 0.02, 
 "string" -> 0.025, "with" -> 0.03, "words" -> 0.035, "that" -> 0.04, 
 "do" -> 0.045, "not" -> 0.05, "repeat" -> 0.055}

Now pick any of the strings and use the rule to get the first value in the rule list corresponding to that string. For example

 "words" /. stringRules

Gives

 0.035

A good place to start learning about Mathematica programming is Paul Wellin's book

http://www.amazon.com/Programming-Mathematica-Introduction-Paul-Wellin/dp/1107009464/

It's a pretty up-to-date treatment--through version 9.

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract