For example, a = "abcdefg", b = "krtol", they have no intersection, c = "hflsfjg", then a and c have intersaction.
What's the easiest way to check this? just need a True or False result
2 Answers
def hasIntersection(a, b):
return not set(a).isdisjoint(b)
5 Comments
Sven Marnach
Performance probably isn't relevant to the OP's use case. Since you started discussing performance in your comment to Constantinius' answer, I'll treat you to my performance nit as well: Why bother converting
b to a set? set(a).isdisjoint(b) will be faster.wong2
@sven I think that your comment is the best answer till now
hochl
+1 @sven: of course you're right, my fault. Just wanted to point out that calculating an entire intersection just to determine if they intersect at all is a bad idea.
Sven Marnach
Valid point, of course. But it's also a bad idea to iterate over the whole of
b if not necessary. Imagine a is "a" and b is a one-million-character string starting with "a". Your code would need to iterate over all of b, while the variant I proposed can exit on the first character of b (and the implementation of set.isdisjoint() actually does). An even better optimization would be to convert the shorter string to a set and use the longer one as parameter to isdisjoint().hochl
You maybe won't believe it, but I thought about something similar when I drove home from work, but then the answer wouldn't be a one-liner anymore ^^ I guess the bottom line is: for small strings it maybe does not matter, for large strings there is a lot of room for optimization!
You can use the built in set class:
a = set("abcdefg")
b = set("krtol")
c = set("hflsfjg")
if a.intersection(b): # or simply a & b
pass
4 Comments
Haes
actually, you dont need to call len() on the intersection set. An empty set (or list) will result to false and make the statement more readable.
hochl
Actually, this solution potentially has bad performance, because you tell Python to create the intersection just to check if it is empty. What if both sets have 1 million records and intersect 90%? You're generating a ton of objects that you discard again immediately. Telling Python about your intention is, basically, better, because it knows it won't need the result set, giving it the opportunity to optimize. Using isdisjoint is a better approch.
Sven Marnach
@Haes: Not only is the call to
len() redundant, it also violates Python's style guide PEP 8 which explicitly discourages the use of if len(...):.Constantinius
@SvenMarnach: I did not read
PEP 8, thanks for the hint. Updated my answer.
aandbhave an intersection. It's 'f'. Maybe you should define what you mean by "intersection".