Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Occurrence of a substring inside a list of strings

  • To: mathgroup at smc.vnet.net
  • Subject: [mg51225] Re: [mg51203] Occurrence of a substring inside a list of strings
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sat, 9 Oct 2004 04:18:39 -0400 (EDT)
  • References: <200410080654.CAA24974@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 8 Oct 2004, at 15:54, János wrote:

> *This message was transferred with a trial version of CommuniGate(tm) 
> Pro*
> Hi,
>
> I have a simple list of strings like lst={"abc", "abcd",
> "aabccaddbacdda", "adbacca",....}.  Let's say I have a fragment called
> frag="dba".  I would like to know how many strings in lst contain
> minimum once the fragment frag.
>
> This is what I did:
>
> Length[Flatten[Map[Part[StringPosition[#, frag], All, 1] &,lst] ] ]
>
> Is there a better/faster way to calculate it ?
>
> Thanks ahead,
>
> János
> ----------------------------------------------
> Trying to argue with a politician is like lifting up the head of a
> corpse.
> (S. Lem: His Master Voice)
>
>

I am not sure about "better" or faster (it is quicker to write this 
sort of program than to test how fast it is). One can produce lots of 
version using ideas similar to yours, e.g.

Count[(StringPosition[#1, frag, 1] & ) /@ lst, _?(#1 != {} & )]

2

or

Length[Join @@ (StringPosition[#1, frag, 1] & ) /@ lst]

2

But the method that seems the most elegant to me (I have not tested it 
for speed though) is:


Count[lst, _?(StringMatchQ[#1, StringJoin["*", frag, "*"]] & )]

2



Andrzej Kozlowski
Chiba, Japan
http://www.akikoz.net/~andrzej/
http://www.mimuw.edu.pl/~akoz/


  • Prev by Date: Re: Need Help: 1st order nonlinear differential equation
  • Next by Date: Re: Occurrence of a substring inside a list of strings
  • Previous by thread: Occurrence of a substring inside a list of strings
  • Next by thread: Re: Occurrence of a substring inside a list of strings