Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用词汇表(字谜)时比较两个字符串_Python_Python 3.x_Dictionary - Fatal编程技术网

Python 使用词汇表(字谜)时比较两个字符串

Python 使用词汇表(字谜)时比较两个字符串,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,检查两个字符串是否为字谜。编写一个函数anagramss1,s2,该函数给定两个字符串s1和s2,如果它们为 错误,否则使用字典 如果lens1与lens2不同,则它们不是字谜 我找不到使用字典比较两个字符串的方法。 代码: 可以对每个字符串使用dict来计算每个不同字符的出现次数: def anagrams(s1, s2): d = {} for s in s1, s2: d[s] = {} for c in s: d[s

检查两个字符串是否为字谜。编写一个函数anagramss1,s2,该函数给定两个字符串s1和s2,如果它们为 错误,否则使用字典

如果lens1与lens2不同,则它们不是字谜 我找不到使用字典比较两个字符串的方法。 代码:


可以对每个字符串使用dict来计算每个不同字符的出现次数:

def anagrams(s1, s2):
    d = {}
    for s in s1, s2:
        d[s] = {}
        for c in s:
            d[s][c] = d[s].get(c, 0) + 1
    return d[s1] == d[s2]

如果你想使用字典,请检查下面的代码

def anagrams(s1,s2):
    s = s1+s2 #for example s = asd + dsa = asddsa
    l = list(s) #[a,s,d,d,s,a]
    dic = dict(enumerate(l)) # {0: 'a', 1: 's', 2: 'd', 3: 'd', 4: 's', 5: 'a'}
    if len(dic)%2 == 1: #if the two strings are anagrams, the length of the combined strings should be even number
        return False
    else: # now we just compare the two ends of all keys, in the above example, we compare 0 and 5 / 1 and 4 / 2 and 3
        # Notice: the sum of i and the its corresponding party is the len of dic
        i = 0
        while i < len(dic)/2:
            if dic[i] != dic[len(dic)-1-i]:
                return False
                break

            else:
                i += 1
        return True

您可以将单词加载到词典中,并比较词典的排序值

D1={}
D2={}

def anagrams(s1,s2):
    if len(s1)!=len(s2):
        return False

    else:
        elementNumber = 0
        for char in s1:                    #Load s1 into dictionary
            D1[elementNumber] = char
            elementNumber = elementNumber + 1

        elementNumber = 0
        for char in s2:                    #Load s2 into dictionary
            D2[elementNumber] = char
            elementNumber = elementNumber + 1

        print(sorted(D1.values()))         #Example output
        print(sorted(D2.values()))         #Example output

        if sorted(D1.values())==sorted(D2.values()): #Sort and compare
             return True
        else:
             return False

print("Anagrams: "+str(anagrams("Hello", "oHlel"))) #Returns True
print("Anagrams: "+str(anagrams("Hello", "xyzlo"))) #Returns False

如果您只是在检查字谜,请尝试使用python的计数器对象。你只需要一行


字符串是否包含空格?特殊角色呢?我也不明白你为什么需要词典。请帮助我们和你自己制定。到目前为止,在你的帖子里还没有。
from collections import deque
def anagrams(s1,s2):
    s = s1+s2 # put them into one string and now we can simply compare if the far left and far right one is the same
    dq = deque(s) # apply deque function to it
    while len(dq) > 1: #if it equals to one
        if dq.popleft() != dq.pop():
            return False
    if len(dq) == 1:
        return False
    else:
        return True
D1={}
D2={}

def anagrams(s1,s2):
    if len(s1)!=len(s2):
        return False

    else:
        elementNumber = 0
        for char in s1:                    #Load s1 into dictionary
            D1[elementNumber] = char
            elementNumber = elementNumber + 1

        elementNumber = 0
        for char in s2:                    #Load s2 into dictionary
            D2[elementNumber] = char
            elementNumber = elementNumber + 1

        print(sorted(D1.values()))         #Example output
        print(sorted(D2.values()))         #Example output

        if sorted(D1.values())==sorted(D2.values()): #Sort and compare
             return True
        else:
             return False

print("Anagrams: "+str(anagrams("Hello", "oHlel"))) #Returns True
print("Anagrams: "+str(anagrams("Hello", "xyzlo"))) #Returns False
# Python code to check if two strings are 
# anagram 
from collections import Counter 

def anagram(input1, input2): 

    # Counter() returns a dictionary data 
    # structure which contains characters  
    # of input as key and their frequencies 
    # as it's corresponding value 
    return Counter(input1) == Counter(input2)