intersection of 2 strings

Tim Churches tchur at optushome.com.au
Tue Aug 3 17:01:05 EDT 2004


On Tue, 2004-08-03 at 22:37, Antoine Logean wrote:
> 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

>From your example, I suspect that you really want to find the "longest
common subsequence" of the two strings, rather than the intersection,
because surely the first occurrences of "my" and "is" in string_1 also
qualify for being in the the intersection set of string_1 and string_2.
It is a bit hard to know what you mean by "intersection" when it is not
clear whether you regard the strings as sets of characters or sets of
words.

There is a public domain implementation of an LCS algorithm by Yusuke
Shinyama at http://www.unixuser.org/~euske/python/lcs.py - and it
produces the result you are expecting from your test data.

-- 

Tim C

PGP/GnuPG Key 1024D/EAF993D0 available from keyservers everywhere
or at http://members.optushome.com.au/tchur/pubkey.asc
Key fingerprint = 8C22 BF76 33BA B3B5 1D5B  EB37 7891 46A9 EAF9 93D0






More information about the Python-list mailing list