3
$\begingroup$

A sample of the list looks like this:

{{"September 29", "2.2300"}
, {"September 30", "2.2075"}
, {"October 1", "2.1825"}
, {"October 2", "2.1400"}
, {"October 3", "2.1500"}
, {"Sep 29 - Oct 3", "2.1820-AVG"}
, {"October 6", "2.1500"}
, {"October 7", "2.1600"}
, {"October 8", "2.1600"}
, {"October 9", "2.1650"}
, {"October 10", "2.1975"}
, {"Oct 6 -10", "AVG: 2.1665"}}

All values are strings.

I want to remove the weekly summary list items that contain "AVG". As you can see, "AVG" can occur at the beginning or at the end of a string.

I could count the items of the list and simply Delete the offending items... but I'd kinda like to do it in one fell swoop since this occurs in multiple lists (one year of data in each list).

I've tried lots of things and checked quite a few topics here without success. Could you help me out? Thank you.

$\endgroup$
  • $\begingroup$ Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 May 12 '16 at 12:27
  • 1
    $\begingroup$ There are many questions here about filtering list-manipulation you can use Cases Select Pick and many others. DeleteCases[data, {_, _?(! StringFreeQ[#, "AVG"] &)}] $\endgroup$ – Kuba May 12 '16 at 12:28
  • $\begingroup$ Things move fast here! Kuba's suggestion is shorter than the other answer that was there until a minute ago... but they both work. May not be exciting to you folks, but I've spent a lot of time digging through the examples in the Wolfram Help and looking for similar questions here (I was very impressed by the suggestions for other topics that appeared while I was typing my own question!) Anyway, my problem has been solved... but since the two methods that were suggested are comments (and one is gone!) how do I mark an Answer? I can't believe how quickly this turned around. Thank you! $\endgroup$ – serbach May 12 '16 at 12:41
1
$\begingroup$

This is your list:

lst = {{"September 29", "2.2300"}, {"September 30", 
    "2.2075"}, {"October 1", "2.1825"}, {"October 2", 
    "2.1400"}, {"October 3", "2.1500"}, {"Sep 29 - Oct 3", 
    "2.1820-AVG"}, {"October 6", "2.1500"}, {"October 7", 
    "2.1600"}, {"October 8", "2.1600"}, {"October 9", 
    "2.1650"}, {"October 10", "2.1975"}, {"Oct 6 -10", 
    "AVG: 2.1665"}};

Try the following.

 lst1 = Map[StringReplace[StringReplace[#, "AVG" ~~ __ -> "a"], __ ~~ "AVG" -> "a"] &, lst];

Delete[lst1, Position[lst1, {str_, "a"}]]

(*   
{{"September 29", "2.2300"}, {"September 30", "2.2075"}, {"October 1",
   "2.1825"}, {"October 2", "2.1400"}, {"October 3", 
  "2.1500"}, {"October 6", "2.1500"}, {"October 7", 
  "2.1600"}, {"October 8", "2.1600"}, {"October 9", 
  "2.1650"}, {"October 10", "2.1975"}}
*)

Have fun!

$\endgroup$
  • $\begingroup$ Thank you, Alexei! I had been trying Position[] to isolate the list elements containing "AVG" but I could never get it to recognize a pattern like ___~~"AVG"~~___. Yours is the third suggestion I've received in under an hour. I very much appreciate it. $\endgroup$ – serbach May 12 '16 at 13:42
  • $\begingroup$ Or one could do it in one fell swoop using Delete[lst, Position[lst, {date_, value_} /; StringMatchQ[value, {"AVG" ~~ __, __ ~~ "AVG"}]]] $\endgroup$ – Jack LaVigne May 12 '16 at 14:32
4
$\begingroup$
sample = {{"September 29", "2.2300"},
   {"September 30", "2.2075"},
   {"October 1", "2.1825"},
   {"October 2", "2.1400"},
   {"October 3", "2.1500"},
   {"Sep 29 - Oct 3", "2.1820-AVG"},
   {"October 6", "2.1500"},
   {"October 7", "2.1600"},
   {"October 8", "2.1600"},
   {"October 9", "2.1650"},
   {"October 10", "2.1975"},
   {"Oct 6 -10", "AVG: 2.1665"}};

Pick[sample, StringMatchQ[Last /@ sample, "*AVG*"], False]

or

Select[sample, StringMatchQ[Last@#, NumberString] &]
$\endgroup$
  • $\begingroup$ ...or, Pick[sample, StringFreeQ[sample[[All, -1]], "AVG"]] $\endgroup$ – J. M. is away May 12 '16 at 14:02
  • $\begingroup$ My head is starting to spin now! I believe that's seven ways to solve my problem. What I can't figure out is why I couldn't find these on my own. I spent lots of time on this -- here in the forum and in the Help system -- before actually posting my question, I don't mind telling you. Sheesh! $\endgroup$ – serbach May 12 '16 at 19:49

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.