python - 比较两个字典的键和值

标签 python dictionary

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"))

嗨,我想检查两个词典是否相同。我试图完成代码的结尾,但我做不到。你能告诉我一种方法吗?

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

这是第二种方法...

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"))

此代码的第三种方法使用哈希码。我尝试添加更多方法来解决它......

最佳答案

你可以简化这个,要点是两个字典的相等性检查:

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

但话又说回来,所有这些都可以缩短:

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

Counter的用法(dict 子类)让您的生活变得更加轻松。

关于python - 比较两个字典的键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69751016/

相关文章:

python - Pandas 、应用函数和 lambda

c# - Dictionary.Last() 的返回值是否未定义?

python - 将数据帧转换为字典,忽略某些值

c - 动态字符串数组/动态字典

python - 报告实验室错误 : 'Table' object has no attribute '_colpositions'

python - 如何为多人游戏的赢/输报告组织 MySQL 表

python - OpenCV 3.1.0 : Finding the 3D position of a flat plane with known dimensions

python - Python 中的 % 操作问题

python - 高效的字典搜索?

python - 将嵌套元组转换为嵌套字典