intersection of 2 strings
Peter Abel
PeterAbel at gmx.net
Tue Aug 3 17:48:34 EDT 2004
al at biolinux.ch (Antoine Logean) wrote in message news:<e3dbc412.0408030437.bc62176 at posting.google.com>...
> Hi,
>
> What is the easiest way to get the intersection of two strings in
> python (a kind a "and" operator) ?
> ex:
>
> string_1 = "the_car_of_my_fried_is_bigger_as_mine_but_my_girlfriend_is_more_beautifull"
>
> string_2 =
> "my_girlfriend_is_more_beautifull_and_has_blue_eyes"
>
> and the intersection :
> string_1 "and" string_2 = "my_girlfriend_is_more_beautifull"
>
> thanks for your help
>
> Antoine
If you take Jørgen Cederberg's solution you can inherit from
str and create your own "&" operator.
>>> class mystring(str):
... def __and__(self,other):
... m = SequenceMatcher(None, self, other)
... equals=[]
... for (i,j,n) in m.get_matching_blocks():
... if n>1:
... equals.append(self[i:i+n])
... return equals
...
>>> s1=mystring(string_1)
>>> s1 & string_2
['my_girlfriend_is_more_beautifull']
Regrads
Peter
More information about the Python-list
mailing list