Yes, but it also means hash(b) == hash(x), so equality of the items isn't enough to make them the same.
Yes, but it also means hash(b) == hash(x), so equality of the items isn't enough to make them the same.
That's right. You could try it in the interpreter like this:
>>> a_set = set(['a', 'b', 'c'])
>>> 'a' in a_set
True
>>>'d' in a_set
False
Honest Question: Why don't Python strings have a "contains" method?
Python: See if one set contains another entirely? - Stack Overflow
Method to check if an element is in a set and add if not - Ideas - Discussions on Python.org
Contains of HashSet<Integer> in Python - Stack Overflow
I know there is the in keyword which does the job and does it well, but it's a question of consistency. Isn't one of those python zens all about consistency?
When I can do str.startswith('Foo') and also str.endswith('Foo'), then str.contains('Foo') should be quite obvious and intuitive isn't it? Instead of that, I have to do this:
if 'Foo' in str:
do_something()
While this does the job and does it great, the contains() method is more practical and intuitive, isn't it? And it's not that the core team even has to do a lot of effort for that. We already have str.__contains__('Foo') which works, so all they have to do is to turn it into a proper method!
Those are lists, but if you really mean sets you can use the issubset method.
>>> s = set([1,2,3])
>>> t = set([1,2])
>>> t.issubset(s)
True
>>> s.issuperset(t)
True
For a list, you will not be able to do better than checking each element.
For completeness: this is equivalent to issubset (although arguably a bit less explicit/readable):
>>> set([2,1]).issubset(set([1,2,3]))
True
>>> set([2,1]) <= set([1,2,3])
True
>>> set([3,5,9]).issubset(set([1,2,3]))
False
>>> set([3,5,9]) <= set([1,2,3])
False
Just use a set:
>>> l = set()
>>> l.add(1)
>>> l.add(2)
>>> 1 in l
True
>>> 34 in l
False
The same works for lists:
>>> ll = [1,2,3]
>>> 2 in ll
True
>>> 23 in ll
False
Edit:
Note @bholagabbar's comment below that the time complexity for in checks in lists and tuples is O(n) on average (see the python docs here), whereas for sets it is on average O(1) (worst case also O(n), but is very uncommon and might only happen if __hash__ is implemented poorly).
In Python, there is a built-in type, set.
The major difference from the hashmap in Java is that the Python set is not typed,
i.e., it is legal to have a set {'2', 2} in Python.
Out of the box, the class set does not have a contains() method implemented.
We typically use the Python keyword in to do what you want, i.e.,
A = [1, 2, 3]
S = set()
S.add(2)
for x in A:
if x in S:
print("Example")
If that does not work for you, you can invoke the special method __contains__(), which is NOT encouraged.
A = [1, 2, 3]
S = set()
S.add(2)
for x in A:
if S.__contains__(x):
print("Example")
The __contains__ method on an object doesn't call in; rather, it is what the in operator calls.
When you write
if circle1 in circle2:
The python interpreter will see that circle2 is a Circle object, and will look for a __contains__ method defined for it. It will essentially try to call
circle2.__contains__(circle1)
This means that you need to write your __contains__ method without using in, or else you will be writing a recursive method that never ends.
Your __contains__ method must use the same logic as your original contains method. Otherwise how will Python know what it means for one circle to contain another? You have to tell it, that's what the __contains__ method is for. You can either get __contains__ to call contains, or just put the whole code in that method instead.