Re: Searching for multiple strings in a list
- To: mathgroup at smc.vnet.net
- Subject: [mg86301] Re: [mg86280] Searching for multiple strings in a list
- From: Oyvind Tafjord <tafjord at wolfram.com>
- Date: Sat, 8 Mar 2008 05:39:11 -0500 (EST)
- References: <200803070730.CAA20006@smc.vnet.net>
Coleman, Mark wrote:
> Greetings,
>
> I'm doing some work with very long lists of strings. As part of the
> effort I need to identify strings that contain all of set of keywords
> (regardless of order) that a user may specify. The function StringCases
> will take multiple keywords as an argument, but it will return a list
> giving those elements that contain any one of the keywords, not *all* of
> the keywords. I know this can be done using multiple calls to
> StringCases and comparing the results for each keyword, but this seems
> very cumbersome, particularly if there are many keywords specified.
>
> Any help would be most appreciated.
>
>
> Thanks,
>
> -Mark
Here's a reasonably efficient way:
In[44]:=
list = {"foo foo", "foo bar", "bar foo none",
"bar foo bar none", "foobar"};
keywords = Union[{"foo", "bar"}];
Pick[list,
Union /@ StringCases[list,
WordBoundary ~~ keywords ~~ WordBoundary], keywords]
Out[46]= {"foo bar", "bar foo none", "bar foo bar none"}
Taking advantage of the List support in the first argument of StringCases
can save a fair amount of time for large lists.
Oyvind Tafjord
Wolfram Research
- References:
- Searching for multiple strings in a list
- From: "Coleman, Mark" <Mark.Coleman@LibertyMutual.com>
- Searching for multiple strings in a list