Re: Occurrence of a substring inside a list of strings
- To: mathgroup at smc.vnet.net
- Subject: [mg51238] Re: [mg51203] Occurrence of a substring inside a list of strings
- From: DrBob <drbob at bigfoot.com>
- Date: Sat, 9 Oct 2004 04:19:16 -0400 (EDT)
- References: <200410080654.CAA24974@smc.vnet.net>
- Reply-to: drbob at bigfoot.com
- Sender: owner-wri-mathgroup at wolfram.com
Possibly StringMatchQ yields a nicer solution. lst = {"abc", "abcd", "aabccaddbacdda", "adbacca"}; frag = "cca"; boole[True] = 1; boole[a_] = 0; superString::usage = "superString[ a][b] returns 1 if b is a super-string of a (a is a substring of b), else 0."; superString[frag_String][ a_String] := boole@StringMatchQ[a, "*" <> frag <> "*"] Total[superString[frag] /@ lst] Tr[superString[frag] /@ lst] Plus @@ (superString[frag] /@ lst) 2 2 2 Bobby On Fri, 8 Oct 2004 02:54:53 -0400 (EDT), János <janos.lobb at yale.edu> wrote: > 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) > > > > -- DrBob at bigfoot.com www.eclecticdreams.net
- References:
- Occurrence of a substring inside a list of strings
- From: János <janos.lobb@yale.edu>
- Occurrence of a substring inside a list of strings