'Python - Intersectiing strings
Trying to write a for function that takes two strings and returns the characters that intersect in the order that they appear in the first string.
Here's what I tried:
def strIntersection(str1, str2):
for i in str1:
str3 = ''
str3 = str3.join(i for i in str1 if i in str2 not in str3)
return str3
str1 = 'asdfasdfasfd'
str2 = 'qazwsxedc'
strIntersection(str1,str2)
=> 'asdasdasd'
however I only want the the intersection characters to appear once and in order of the first string ie. 'asd'
Can anyone help?
I've found some similar problems on other forums but the solutions all seem to involve lists whereas I'd like my output to be a string
Solution 1:[1]
You want a string consisting of the unique characters that are common to str1 and str2, in the order they appear in str1.
Uniqueness and commonality imply set operations: that is, we're looking for the set of characters that appear in both str1 and str2. A set is fundamentally unordered, but we can re-order the data by sorting the characters according to their "index" of first occurrence in str1. Then it's a simple matter of creating a string from the sorted sequence.
Putting it all together, we get:
''.join(sorted(set(str1) & set(str2), key = str1.index))
Solution 2:[2]
You can use python sets http://docs.python.org/library/stdtypes.html#set to do this, like so:
>>> set("asdfasdfasfd") & set("qazwsxedc")
set(['a', 's', 'd'])
Solution 3:[3]
easiest is to use sets in python
>>> a='asdfasdfasfd'
>>> b='qazwsxedc'
>>> set(a).intersection(b)
set(['a', 's', 'd'])
Solution 4:[4]
It looks like your current script should do it if you fix the typo on the fourth line:
str3 = str3.join(i for i in str1 if i in str2 not in str3)
should be
str3 = str3.join(i for i in str1 if i in str2 and i not in str3)
I wouldn't recommend using a set for this simpy because they don't guarantee order. Your script is also likely to be faster.
Solution 5:[5]
def str_intersection(str1, str2):
common_letters = set(str1) & set(str2)
str3 = ''
for c in str1:
if (c in common_letters) and (c not in str3):
str3 += c
return str3
Solution 6:[6]
All answers help, however, you may need to ignore or not ignore the orders. It may be important, especially finding misspellings. Here more sophisticated version of the function for finding string intersection:
def str_intersection(str_left, str_right, ignore_order = True ):
common_letters = set(str_left) & set(str_right)
str_intersected = ''
for c in str_left:
if (c in common_letters) and (c not in str_intersected):
str_intersected += c
if ignore_order:
pass
else:
if str_intersected in str_left and str_intersected in str_right:
return str_intersected
else:
return None
return str_intersected
If you run str_intersection("AB","XAB") result will be AB. But main handicap of this way is the result of str_intersection("god","dog") will be god. If you want to get nothing use this function like this: str_intersection("god", "dog", ignore_order=False) in that case result will be None because god and dog share the same characters but the order is different. I hope this sophisticated may help you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Karl Knechtel |
| Solution 2 | supakeen |
| Solution 3 | Rohit Malgaonkar |
| Solution 4 | |
| Solution 5 | vartec |
| Solution 6 | Suat Atan PhD |
