Velocity Reviews > How to search for substrings of a string in a list?

# How to search for substrings of a string in a list?

Girish Sahani
Guest
Posts: n/a

 06-05-2006
Given a length k string,i want to search for 2 substrings (overlap
possible) in a list consisting of length k-1 strings. These 2 substrings
when 'united' give the original string.

e.g given 'abc' i want to search in the list of 2-length strings
['ab',ac','cd','bc','bd'] to extract either
1) 'ab and 'ac' OR ('a' common)
2) 'ab' and 'bc' OR ('b' common)
3) 'ac' and 'bc' ('c' common)
In all these cases, one of the letter is common in the 2 strings.
Out of the k-1 letters in each length k-1 string,k-2 will be common.
Another e.g is:
Given 'abcd' and list ['abc,'abd','bcd'],i must extract
1)abc and abd OR ('ab' common)
2)abc and bcd OR
3)abd and bcd OR
Here 2 letters are common in all the solutions.
I havent been able to figure out a method. Pleeez help!!

girish

Christoph Zwerschke
Guest
Posts: n/a

 06-05-2006
Girish Sahani schrieb:
> Given a length k string,i want to search for 2 substrings (overlap
> possible) in a list consisting of length k-1 strings. These 2 substrings
> when 'united' give the original string.
> e.g given 'abc' i want to search in the list of 2-length strings
> ['ab',ac','cd','bc','bd'] to extract either
> 1) 'ab and 'ac' OR ('a' common)
> 2) 'ab' and 'bc' OR ('b' common)
> 3) 'ac' and 'bc' ('c' common)

Here is a simple brute force solution that also works for different

complete = 'abc'
partial = ['ab','ac','cd','bc','bd']

for i1, s1 in enumerate(partial):
for s2 in partial[i1+1:]:
if set(s1).union(set(s2)) == set(complete):
print s1, s2

-- Christoph

Girish Sahani
Guest
Posts: n/a

 06-06-2006
I want to generate all substrings of size k-1 from a string of size k.
e.g 'abcd' should give me ['abc','abd','bcd','acd']
Order of these strings in the list doesnt matter.
Also order doesnt matter inside the string e.g 'abc' or 'bca' or 'bac' is
the same.
I wrote the following code but it doesnt give the full output:

subsetList = []
for element in prunedNew:
for i in range(0,2):
subsetList.append(element[i:i+len(element)-1])
continue
continue
return prunedNew

girish

K.S.Sreeram
Guest
Posts: n/a

 06-06-2006
Girish Sahani wrote:
> I want to generate all substrings of size k-1 from a string of size k.
> e.g 'abcd' should give me ['abc','abd','bcd','acd']

def get_sub_set( s ) :
return [s[:i]+s[i+1:] for i in range(len(s))]

>>> print get_sub_set( 'abcd' )

['bcd', 'acd', 'abd', 'abc']

Regards
Sreeram

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEhPFyrgn0plK5qqURAoMaAKC7oRnEPkQKMeX5xhz2eX hH4pSlTQCgp8Qu
gPiJ3A029tq5jgAJI33DLUY=
=3E6C
-----END PGP SIGNATURE-----