Home > database >  compare two dictionaries' keys and values
compare two dictionaries' keys and values

Time:10-29

def are_anagrams(sent_one,sent_two):
    sent_one=sent_one.replace(" ","")
    sent_one=sent_one.lower()
    sent_two=sent_two.replace(" ","")
    sent_two=sent_two.lower()
    dict_of_one={}
    dict_of_two={}
    for one in sent_one:
        if one not in dict_of_one:
            dict_of_one.setdefault(one,1)
        else:
            dict_of_one[one] =1
    for second in sent_two:
        if second not in dict_of_two:
            dict_of_two.setdefault(second,1)
        else:
            dict_of_two[second] =1
    print(dict_of_one)
    print(dict_of_two)
    for k,v in dict_of_one.items():
        if k in dict_of_two and dict_of_two[k]==v:
            return True
        else:
            return False

print(are_anagrams("Elvis", "Lives"))
print(are_anagrams("Elvis", "Live Viles"))
print(are_anagrams("Eleven plus two", "Twelve plus one"))
print(are_anagrams("Hot Water","Worth Coffee"))

Hi, I want to check two dictionaries are the same or not. I tried to do the end of the code, but I couldn't do it. Could you show me a way to do it?

def are_anagrams(first_word, second_word):
    first_word = first_word.lower()
    second_word = second_word.lower()
    first_word = first_word.replace(' ', '')
    second_word = second_word.replace(' ', '')
    letters = []
    for char in first_word:
        letters.append(char)
    for char in second_word:
        if char not in letters:
            return False
        letters.remove(char)
    return len(letters) == 0

this is the second approach...

def are_anagrams(first_word, second_word):
    first_word = first_word.lower()
    second_word = second_word.lower()
    first_word = first_word.replace(' ', '')
    second_word = second_word.replace(' ', '')

    first_word_list=list(first_word)
    first_word_list.sort()
    first_word="".join(first_word_list)

    second_word_list=list(second_word)
    second_word_list.sort()
    second_word="".join(second_word_list)

    if hash(second_word)==hash(first_word):
        return True
    return False

print(are_anagrams("Elvis", "Lives"))
print(are_anagrams("Elvis", "Live Viles"))
print(are_anagrams("Eleven plus two", "Twelve plus one"))
print(are_anagrams("Hot Water","Worth Coffee"))

the third approach to this code uses hash codes. I try to add more ways to solve it...

CodePudding user response:

You can simplify this, the main point being the equality check of the two dictionaries:

def are_anagrams(sent_one,sent_two):
    sent_one = sent_one.replace(" ","").lower()
    sent_two = sent_two.replace(" ","").lower()
    dict_of_one = {}
    dict_of_two = {}
    for one in sent_one:
        dict_of_one[one] = dict_of_one.get(one, 0)   1
    for two in sent_two:
        dict_of_two[two] = dict_of_two.get(two, 0)   1
    return dict_of_one == dict_of_two 

are_anagrams("Elvis", "Lives")
# True
are_anagrams("Elvis", "Live Viles")
# False
are_anagrams("Eleven plus two", "Twelve plus one")
# True
are_anagrams("Hot Water","Worth Coffee")
# False

But then again, all of that can be shortened:

from collections import Counter

def are_anagrams(sent_one,sent_two):
    c1 = Counter(sent_one.lower().replace(" ", ""))
    c2 = Counter(sent_two.lower().replace(" ", ""))
    return c1 == c2

The usage of Counter (a dict subclass) makes you life so much easier.

CodePudding user response:

If you don't have to use a dictionary, it would be easier to use lists:

def split(word):
    return [char for char in word]

def are_anagrams(one, two):
    newOne = split(one.replace(" ","").lower())
    newTwo = split(two.replace(" ","").lower())
    for item in newTwo[:]:
        if item in newOne:
            newOne.remove(item)
            newTwo.remove(item)
    if newOne == [] and newTwo == []:
        return True
    return False

print(are_anagrams("Elvis", "Lives")) # True
print(are_anagrams("Elvis", "Live Viles")) # False
print(are_anagrams("Eleven plus two", "Twelve plus one")) # True
print(are_anagrams("Hot Water","Worth Coffee")) # False

Note the line for item in newTwo[:] has the [:] to stop the for loop breaking because we are deleting items in newTwo. Adding [:] clones the list, meaning it doesn't change when we change the actual newTwo variable.

CodePudding user response:

You don't have to overcomplicate things. To check for anagrams, all you need to do is to check whether the two sorted strings are equal.

def anagrams(a, b):
    a, b = [sorted(i.lower().replace(" ", ""))
            for i in (a, b)]
    
    return a == b

Output

Let's do a few test cases on our anagrams function.

>>> anagrams("Elvis", "Lives")
True
>>> anagrams("Elvis", "Live Viles")
False
>>> anagrams("Eleven plus two", "Twelve plus one")
True
>>> anagrams("Hot Water", "Worth Coffee")
False

It worked as expected. Also, this function is fast in terms of time complexity because it depends on the complexity of the sorting algorithm.

  • Related