intersection of 2 strings

Larry Bates lbates at swamisoft.com
Tue Aug 3 09:47:02 EDT 2004


At the risk of doing your homework,
one way would be:

list_1=string_1.split('_')
list_2=string_2.split('_')
#
# The next line uses some "magic" to eliminate duplicates"
#
list_3=[x for x in list_1 if x in list_2 if x not in
locals()['_[1]'].__self__]
string_3="_".join(list_3)
print string_3

If you have Python 2.3 you also could use sets() module
and it would be even easier.

HTH,
Larry Bates


"Antoine Logean" <al at biolinux.ch> 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





More information about the Python-list mailing list