I have two strings:
a = "hello world"
b = "world hello"
which would run faster?
set(a)==set(b)
or
from collections import Counter
Counter(a)==Counter(b)
Why? and is there anything that runs faster?
The set
comparison would run faster. There are two main reasons for this:
1) There is more information in Counter()
compared to set()
2) There are more operations required to build up a Counter()
object compare to a set()
.
Roughly speaking, Counter()
is a dictionary, which consist of key value pairs. In Counter the values have the special meaning of counting the number of occurrences of the key. By contrast, set()
only contains information about the keys.
When it comes to addressing the question of which one should you use, this depends on what are you trying to achieve, given that the two expressions are not equivalent:
from collections import Counter
a = "hello world"
b = "world helo"
set(a) == set(b)
# True
Counter(a) == Counter(b)
# False
Therefore it is hard to give more hints in this direction.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.