Is there something wrong with the way I am using sets in python?
Question:
I want to use sets to check for anagram, and I think I am doing it correctly. First I add all elements of string A to the set and then returning a flag based on elements of string B.
def return_anagram(a, b):
flag = True
test_set = set()
for char in a:
test_set.add(char)
for char in b:
if b not in test_set:
flag = False
break
return flag
print(return_anagram("abc", "cba"))
but it doesn’t work for some reason, it returns false even though the elements are clearly matching. Anyone know the issue?
Answers:
You just needed to change the order of your code, you should be using char
in each loop not b
. So if char not in
instead of what you have here.
Also, you can instantly create a set using set(a)
.
def return_anagram(a, b):
flag = True
test_set = set(a) # instantiate your set here
for char in b:
if char not in test_set: #change this part to char instead of b
flag = False
break
return flag
print(return_anagram("abc", "cba"))
My output is True
.
As the comment below mentioned, you could also do,
def return_anagram(a, b):
return set(a) == set(b)
print(return_anagram("abc", "cba"))
You will also get True
.
However, you might want to rethink using sets and using a different data structure, because the words abbc
and bcca
will return True
as sets will remove the duplicate characters.
As, suggested in the comments, instead of using a list or sets, you should use a multiset as the data structure has already been implemented.
Below I have made 2 implementations, one using the multiset module which you have to install, and the next using the Counter module from the built-in standard python library.
Option1:
First install the module by using pip install multiset
.
Next all you have to do is import it and replace set
with Multiset
like this.
from multiset import Multiset
def return_anagram(a, b):
return Multiset(a) == Multiset(b)
Option2:
You could also use the built in Counter
tool from the standard collections
library.
from collections import Counter
def multiset(array):
return set(Counter(array).items())
def return_anagram(a, b):
return multiset(a) == multiset(b)
Now,
print(return_anagram("bca", "abc"))
returns True
,
while,
print(return_anagram("abbc", "ccba"))
returns, False
with both codes.
I want to use sets to check for anagram, and I think I am doing it correctly. First I add all elements of string A to the set and then returning a flag based on elements of string B.
def return_anagram(a, b):
flag = True
test_set = set()
for char in a:
test_set.add(char)
for char in b:
if b not in test_set:
flag = False
break
return flag
print(return_anagram("abc", "cba"))
but it doesn’t work for some reason, it returns false even though the elements are clearly matching. Anyone know the issue?
You just needed to change the order of your code, you should be using char
in each loop not b
. So if char not in
instead of what you have here.
Also, you can instantly create a set using set(a)
.
def return_anagram(a, b):
flag = True
test_set = set(a) # instantiate your set here
for char in b:
if char not in test_set: #change this part to char instead of b
flag = False
break
return flag
print(return_anagram("abc", "cba"))
My output is True
.
As the comment below mentioned, you could also do,
def return_anagram(a, b):
return set(a) == set(b)
print(return_anagram("abc", "cba"))
You will also get True
.
However, you might want to rethink using sets and using a different data structure, because the words abbc
and bcca
will return True
as sets will remove the duplicate characters.
As, suggested in the comments, instead of using a list or sets, you should use a multiset as the data structure has already been implemented.
Below I have made 2 implementations, one using the multiset module which you have to install, and the next using the Counter module from the built-in standard python library.
Option1:
First install the module by using pip install multiset
.
Next all you have to do is import it and replace set
with Multiset
like this.
from multiset import Multiset
def return_anagram(a, b):
return Multiset(a) == Multiset(b)
Option2:
You could also use the built in Counter
tool from the standard collections
library.
from collections import Counter
def multiset(array):
return set(Counter(array).items())
def return_anagram(a, b):
return multiset(a) == multiset(b)
Now,
print(return_anagram("bca", "abc"))
returns True
,
while,
print(return_anagram("abbc", "ccba"))
returns, False
with both codes.