2
$\begingroup$

I have a list with string and integers and want to replace one word in the string with an integer.

For Example if I have

 list={{1,"the dog jumped", "Tues : Wed},
       {2,"the fox ran", "Mon : Tues : Fri"},
       {3,"a dog jumped", "Wed : Mon : Tues"}}

I replace part of the string with integer values so Mon->{1,0,0,0} Tues->{0,1,0,0} Wed->{0,0,1,0} Fri->{0,0,0,1} :->+.

so I get

 list={{1, "the dog jumped", {0,1,0,0}+{0,0,1,0}},
       {2, "the fox ran", {1,0,0,0}+{0,1,0,0}+{0,0,0,1}},
       {3, "a dog jumped". {0,0,1,0}+{1,0,0,0}+{0,1,0,0}}

StringReplace gives me and error and ReplaceAll only works for the full string

$\endgroup$
  • $\begingroup$ Show the StringReplace and ReplaceAll expressions you have tried so far. $\endgroup$ – MarcoB Jul 9 '16 at 23:01
  • $\begingroup$ ReplaceAll[list, {"Mon" -> {1, 0, 0, 0}, "Tues" -> {0, 1, 0, 0}}] StringReplace[list, "Mon" -> {1, 0, 0, 0}] $\endgroup$ – MTR Jul 9 '16 at 23:05
  • $\begingroup$ I noticed ReplaceAll only works for the whole string so StringReplace from Wolfram Documentation is what I wanted but doesn't seem to work for list. The error was String or list of strings expected at position 1 in \ StringReplace[{{1,the dog jumped ,Tues : Wed : Sat },{2,the fox ran \ ,Mon : Tues : Fri },{3,a dog jumped ,Wed : Mon : Tues }},Mon \ ->{1,0,0,0}]. >> $\endgroup$ – MTR Jul 9 '16 at 23:06
3
$\begingroup$

Note that you have a "." in your original expression, "a dog jumped". instead of , and also I do not know why sat is not there, so I made one up for it. But any way, is this what you wanted?

list = {{1, "the dog jumped", "Tues : Wed : Sat"}, {2, "the fox ran", 
    "Mon : Tues : Fri"}, {3, "a dog jumped", "Wed : Mon : Tues"}};
rep = {":" -> "+", "Mon" -> "{1,0,0,0}" , "Tues" -> "{0,1,0,0}" , 
   "Wed" -> "{0,0,1,0}" , "Fri" -> "{0,0,0,1}", "Sat" -> "{0,0,1,1}"};
list[[All, 3]] = StringReplace[#[[3]], rep] & /@ list;
list

gives

Mathematica graphics

If you want to add these {0,0,1,1}+.... etc... this can also be done by converting them to Expressions.

edit:

If you want to add these {0,0,1,1}+.... you can use ToExpression as follows

list = {{1, "the dog jumped", "Tues : Wed : Sat"}, {2, "the fox ran", 
    "Mon : Tues : Fri"}, {3, "a dog jumped", "Wed : Mon : Tues"}};
rep = {":" -> "+", "Mon" -> "{1,0,0,0}" , "Tues" -> "{0,1,0,0}" , 
   "Wed" -> "{0,0,1,0}" , "Fri" -> "{0,0,0,1}", "Sat" -> "{0,0,1,1}"};
list[[All, 3]] = ToExpression[StringReplace[#[[3]], rep] & /@ list];
list

Mathematica graphics

$\endgroup$
  • $\begingroup$ The . was an error. Fixed typos thanks. This is a toy model for a bigger data set that I have. So The solution seems to be with StringReplace so it look like you have to specify what column to replace. Correct? $\endgroup$ – MTR Jul 9 '16 at 23:30
  • $\begingroup$ @MTR Well, yes, you have to tell it which part to replace ofcourse. You have a list of lists. And you want to only apply the replace on part of each list. $\endgroup$ – Nasser Jul 9 '16 at 23:32
  • $\begingroup$ OK cool thanks so lets just say my second column also had days of the week and wanted to replace in all columns would I have to list[[All, 2;;3]] = StringReplace[#[[2;;3]], rep] & /@ list; My notation might be wrong to to say column 2-3. but is that the right idea $\endgroup$ – MTR Jul 9 '16 at 23:34
  • $\begingroup$ Also yes my goal is to add them together $\endgroup$ – MTR Jul 9 '16 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.