Python:检查字符中的所有字符是否以任何顺序存在于字符串中

时间:2018-01-31 16:23:26

标签: python

我想检查两个字符串是否是字谜。例如,如果我的单词是“halo”,我想检查这些字母是否出现在“loha”中。它应该匹配,因为它是一个字谜。

我的尝试失败了,我不知道为什么。我的代码和输出如下。我有一个单词列表,我想查看列表中哪些元素是字谜。

def anagram(myList):
    for elem in myList:
        chars = set(elem)
        if all((c in chars) for c in myList):
            print  "Yes, anagram ", elem, chars
        else:
            print "NOT anagram ", elem, chars


wordsList = ["halo", "loha", "ahlo", "sully"]
anagram(wordsList)

这是我的输出

NOT anagram  halo set(['a', 'h', 'l', 'o'])
NOT anagram  loha set(['a', 'h', 'l', 'o'])
NOT anagram  ahlo set(['a', 'h', 'l', 'o'])
NOT anagram  sully set(['y', 's', 'u', 'l'])

7 个答案:

答案 0 :(得分:1)

试试这个

def isanagram(ele1,ele2):
    ele1=list(ele1)
    ele2=list(ele2)
    return(sorted(ele1)==sorted(ele2))

print(isanagram("ahlo", "halo"))

Out put

  

对于多元素检查

print(map(isanagram,["ahlo", "halo"],[ "alho", "sully"])

Out put

  

[True,False]

答案 1 :(得分:0)

这应该证明相对较快,因为它保留了一个排序列表,因此每次迭代时它只需要创建一个新列表。

from itertools import islice

def anagram(myList):
    s1 = sorted(myList[0])
    for i,elem in enumerate(islice(myList,1,len(myList))):
        s2 = sorted(elem)
        if s1 == s2:
            print ("Yes anagram: ",myList[i],elem)
        else:
            print ("Not anagram: ",myList[i],elem)
        s1 = s2

wordsList = ["halo", "loha", "ahlo", "sully"]
anagram(wordsList)

结果:

Yes anagram:  halo loha
Yes anagram:  loha ahlo
Not anagram:  ahlo sully

不同的结构选项:

from itertools import islice

def is_anagram(sorted_1,sorted_2):
    if sorted_1 == sorted_2:
        return True
    return False

wordsList = ["halo", "loha", "ahlo", "sully"]

s1 = sorted(wordsList[0])
for i,elem in enumerate(islice(wordsList,1,len(wordsList))):
    s2 = sorted(elem)
    if is_anagram(s1,s2):
        print ("Yes anagram: ",wordsList[i],elem)
    else:
        print ("Not anagram: ",wordsList[i],elem)
    s1 = s2

答案 2 :(得分:0)

您可以使用counter输出单词中每个字母的字母计数字典

from Collections import Counter

word1='halo'
word2='hola'

if Counter(word1) == Counter(word2):
   print 'Yes Anagram {} {}'.format(word1,word2)
else :
   print 'Not Anagram {} {}'.format(word1,word2)

答案 3 :(得分:0)

只需在两个字符串上使用sorted()并进行比较即可。

def is_anagram(str1, str2):
   return sorted(str1) == sorted(str2)

k = is_anagram('angel', 'glean')
if k == True:
   print('Strings are anagrams')
else:
   print('Strings are not anagrams')

此处,sorted('angel')输出['a', 'e', 'g', 'l', 'n']sorted('glean')也输出['a', 'e', 'g', 'l', 'n']。两者都相等,因此是字谜

说清楚:

>>> sorted('angel') 
['a', 'e', 'g', 'l', 'n'] 
>>> sorted('glean') 
['a', 'e', 'g', 'l', 'n'] 
>>> sorted('angel') == sorted('glean') 
True

解决您的问题:

def is_anagram(str1, str2):
   return sorted(str1) == sorted(str2)

wordsList = ["halo", "loha", "ahlo", "sully"]
each = wordsList[0]
for another in wordsList:
   if each != another:
      k = is_anagram(each, another)
      if k == True:
         print('{} and {} are anagrams'.format(each, another))
      else:
         print('{} and {} are not anagrams'.format(each, another))

<强>输出:

halo and loha are anagrams
halo and ahlo are anagrams
halo and sully are not anagrams

答案 4 :(得分:0)

如果要查找列表中的所有angrams,可能需要改为:

# Checks if two strings are anagrams
def isAnagram(str1, str2):
    return sorted(str1) == sorted(str2)

# Iterates over all items in your list and compares it with all other items to check if they are anagrams.
def anagrams(myList):
    for i in range(0,len(myList)):
        for j in range(i+1, len(myList)):
            if isAnagram(myList[i], myList[j]):
                print "Yes anagram: ", myList[i], myList[j]
            else:
                print "Not anagram: ", myList[i], myList[j]

wordsList = ["halo", "loha", "ahlo", "sully"]
anagram(wordsList)

这将返回以下内容:

Yes anagram: halo loha
Yes anagram: halo ahlo
Not anagram: halo sully
Yes anagram: loha ahlo
Not anagram: loha sully
Not anagram: ahlo sully

这可能不是最有效的解决方案,但它可以完成工作。

答案 5 :(得分:0)

使用套装:

`def anagram(string1,string2):
if string1=='' or string2== '':
    if string1!='' or string2!='':
        return False
    else:
        return True
if len(string1)!=len(string2) or set(string1)!=set(string2):
    return False
stemp1 = list(string1)
stemp2 = list(string2)
stemp2.remove(stemp1[0])
return anagram(''.join(stemp1[1:]),''.join(stemp2))`

输出True

如评论中所述,即使附加了len参数,这也不起作用。 这是一个递归的anagram测试来赎回自己

SELECT  t1.ID 
FROM Table1 t1 
    JOIN Table2 t2 ON
        t2.ID = t1.ID
WHERE t2.Usernames IS NULL

答案 6 :(得分:0)

下面的代码将一个单词列表作为输入并将它们分组为anagrams

def is_grouped(word, anagram_groups):
    is_grouped = False
    for group in anagram_groups:
        if word in group:
            print "{0} is already part of anagram group={1}".format(word, group)
            is_grouped = True
            break
    return is_grouped

def get_anagram_group(word_to_check, word_to_check_index, wordlist):
    word_to_check_as_list = list(word_to_check)
    word_to_check_as_list.sort()
    group = [word_to_check]
    for i in range(0, len(wordlist)):
        if not i == word_to_check_index:
            word = wordlist[i]
            word_as_list = list(word)
            word_as_list.sort()
            if word_as_list == word_to_check_as_list:
                group.append(word)   
    return group

def group_anagrams(wordlist):
    anagram_groups = []
    for i in range(0, len(wordlist)):

        word_under_test = wordlist[i]
        # check if the word's anagrams already identified as part of anagram group
        anagramed = is_grouped(word_under_test, anagram_groups)
        if not anagramed:
            # if word not already a part of anagram group then find find all anagrams for the word
            anagram_group = get_anagram_group(word_under_test, i, wordlist)
            if len(anagram_group) == 1:
                print "no anagrams found for word {0}".format(word_under_test)
            else:
                print "anagrams found for word {0}={1}".format(word_under_test, anagram_group)
                anagram_groups.append(anagram_group)
    return anagram_groups



wlist = ['aloha' , 'hoala', 'loaha', '123', '321', 'xya']
print group_anagrams(wlist)

输出:

anagrams found for word aloha=['aloha', 'hoala', 'loaha']
hoala is already part of anagram group=['aloha', 'hoala', 'loaha']
loaha is already part of anagram group=['aloha', 'hoala', 'loaha']
anagrams found for word 123=['123', '321']
321 is already part of anagram group=['123', '321']
no anagrams found for word xya
[['aloha', 'hoala', 'loaha'], ['123', '321']]