459,296 Members | 1,446 Online
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

# intersection of 2 strings

 P: n/a 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_girl friend_is_more_beautifull" string_2 = "my_girlfriend_is_more_beautifull_and_has_blue_eye s" and the intersection : string_1 "and" string_2 = "my_girlfriend_is_more_beautifull" thanks for your help Antoine Jul 18 '05 #1
5 Replies

 P: n/a 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" wrote in message news:e3*************************@posting.google.co m... 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_girl friend_is_more_beautifull" string_2 = "my_girlfriend_is_more_beautifull_and_has_blue_eye s" and the intersection : string_1 "and" string_2 = "my_girlfriend_is_more_beautifull" thanks for your help Antoine Jul 18 '05 #2

 P: n/a 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_girl friend_is_more_beautifull" string_2 = "my_girlfriend_is_more_beautifull_and_has_blue_eye s" and the intersection : string_1 "and" string_2 = "my_girlfriend_is_more_beautifull" thanks for your help Antoine .... You might try the sets module .... The following provides the intersection, but I don't know how to maintain the order from the original strings .... sk@cpq1 : /mnt/win_k/Python/py_Work/Sets \$ python Python 2.3.4 (#2, Jul 5 2004, 09:15:05) [GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. str_1 = "the_car_of_my_fried_is_bigger_as_mine_but_my_girl friend_is_more_beautifull" str_2 = "my_girlfriend_is_more_beautifull_and_has_blue_eye s" list_1 = str_1.split( '_' ) list_2 = str_2.split( '_' ) import sets set_1 = sets.Set( list_1 ) set_2 = sets.Set( list_2 ) is_12 = set_1.intersection( set_2 ) print is_12 Set(['girlfriend', 'is', 'my', 'beautifull', 'more']) -- Cousin Stanley Human Being Phoenix, Arizona Jul 18 '05 #3

 P: n/a 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_girl friend_is_more_beautifull" string_2 = "my_girlfriend_is_more_beautifull_and_has_blue_eye s" and the intersection : string_1 "and" string_2 = "my_girlfriend_is_more_beautifull" thanks for your help Antoine Hi difflib seems to appropiate, http://docs.python.org/lib/module-difflib.html and especially http://docs.python.org/lib/sequence-matcher.html Here is some code that works from difflib import SequenceMatcher s1 = "my_girlfriend_is_a_python" s2 = "my_girlfriend_is_more_beautifull" m = SequenceMatcher(None, s1, s2) print "Matches between: %s and %s" %(s1,s2) for match in m.get_matching_blocks(): i,j,n = match if n>1: # We don't want to match single chars. print s1[i:i+n] HTH Jorgen Cederberg Jul 18 '05 #4

 P: n/a 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_girl friend_is_more_beautifull" string_2 = "my_girlfriend_is_more_beautifull_and_has_blue_eye s" 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 Jul 18 '05 #5

 P: n/a al@biolinux.ch (Antoine Logean) wrote in message news:... 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_girl friend_is_more_beautifull" string_2 = "my_girlfriend_is_more_beautifull_and_has_blue_eye s" 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 Jul 18 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.