Making dictionary item frequency
Question:
I want to know why it’s not counting the element ?
I made an key then in each iteration it should increase the value.
def isAnagram( s, t):
if len(s) != len(t):
return False
d_1 = {}
d_2 = {}
for i in range(len(s)):
d_1[s[i]] =+ 1
d_2[t[i]] =+ 1
print(d_1)
print(d_2)
return True if d_1 == d_2 else False
s = 'aabb'
t = 'bbaa'
print(isAnagram(s,t))
OUTPUT :
d_1 = {'a': 1, 'b': 1}
d_2 = {'b': 1, 'a': 1}
Answers:
Don’t reinvent the wheel!
from collections import Counter
def isAnagram(s, t):
return Counter(s) == Counter(t)
s = "aabb"
t = "bbaa"
print(isAnagram(s,t)) # => True
as mentioned in comments. you should use +=
instead of =+
.
+=
means adding the value to the previous value stored. while
=+
here means =
(assigning the value)
def isAnagram(s, t):
if len(s) != len(t):
return False
d_1 = {}
d_2 = {}
for i in range(len(s)):
d_1[s[i]] += 1
d_2[t[i]] += 1
print(d_1)
print(d_2)
return True if d_1 == d_2 else False
s = 'aabb'
t = 'bbaa'
print(isAnagram(s,t))
First, you have an error, to add value to the variable you need to do
d_1[s[i]] += 1
d_2[t[i]] += 1
Second, the function fails because you need to define the key and value before modifying the value.
Working function:
def isAnagram( s, t):
if len(s) != len(t):
return False
d_1 = {}
d_2 = {}
for i in range(len(s)):
try: # checking if the character in the dictionary exists
d_1[s[i]]
except KeyError:
d_1[s[i]] = 0
try: # checking if the character in the dictionary exists
d_2[t[i]]
except KeyError:
d_2[t[i]] = 0
d_1[s[i]] += 1
d_2[t[i]] += 1
print(d_1)
print(d_2)
return True if d_1 == d_2 else False
s = 'aabb'
t = 'bbaa'
print(isAnagram(s,t))
add a .lower() to Domagoj’s answer to be case-insensitive.
so this will also work:
s = ‘Betrug’.lower()
t = ‘Erbgut’.lower()
{‘b’: 1, ‘e’: 1, ‘t’: 1, ‘r’: 1, ‘u’: 1, ‘g’: 1}
{‘e’: 1, ‘r’: 1, ‘b’: 1, ‘g’: 1, ‘u’: 1, ‘t’: 1}
True
I want to know why it’s not counting the element ?
I made an key then in each iteration it should increase the value.
def isAnagram( s, t):
if len(s) != len(t):
return False
d_1 = {}
d_2 = {}
for i in range(len(s)):
d_1[s[i]] =+ 1
d_2[t[i]] =+ 1
print(d_1)
print(d_2)
return True if d_1 == d_2 else False
s = 'aabb'
t = 'bbaa'
print(isAnagram(s,t))
OUTPUT :
d_1 = {'a': 1, 'b': 1}
d_2 = {'b': 1, 'a': 1}
Don’t reinvent the wheel!
from collections import Counter
def isAnagram(s, t):
return Counter(s) == Counter(t)
s = "aabb"
t = "bbaa"
print(isAnagram(s,t)) # => True
as mentioned in comments. you should use +=
instead of =+
.
+=
means adding the value to the previous value stored. while
=+
here means =
(assigning the value)
def isAnagram(s, t):
if len(s) != len(t):
return False
d_1 = {}
d_2 = {}
for i in range(len(s)):
d_1[s[i]] += 1
d_2[t[i]] += 1
print(d_1)
print(d_2)
return True if d_1 == d_2 else False
s = 'aabb'
t = 'bbaa'
print(isAnagram(s,t))
First, you have an error, to add value to the variable you need to do
d_1[s[i]] += 1
d_2[t[i]] += 1
Second, the function fails because you need to define the key and value before modifying the value.
Working function:
def isAnagram( s, t):
if len(s) != len(t):
return False
d_1 = {}
d_2 = {}
for i in range(len(s)):
try: # checking if the character in the dictionary exists
d_1[s[i]]
except KeyError:
d_1[s[i]] = 0
try: # checking if the character in the dictionary exists
d_2[t[i]]
except KeyError:
d_2[t[i]] = 0
d_1[s[i]] += 1
d_2[t[i]] += 1
print(d_1)
print(d_2)
return True if d_1 == d_2 else False
s = 'aabb'
t = 'bbaa'
print(isAnagram(s,t))
add a .lower() to Domagoj’s answer to be case-insensitive.
so this will also work:
s = ‘Betrug’.lower()
t = ‘Erbgut’.lower()
{‘b’: 1, ‘e’: 1, ‘t’: 1, ‘r’: 1, ‘u’: 1, ‘g’: 1}
{‘e’: 1, ‘r’: 1, ‘b’: 1, ‘g’: 1, ‘u’: 1, ‘t’: 1}
True