Anagram 解决 python 错误

发布于 2024-12-27 04:24:31 字数 224 浏览 2 评论 0原文

我写了一个字谜求解算法,但不起作用。

for word in wordlist: #Checking for equal length
    if sorted(word.replace("\n", "")) == sorted(anagram):
        possible.append(word)

我需要使用 len(word) - 1 来去掉 \n。

I have written an anagram solving algorithm, which does not work.

for word in wordlist: #Checking for equal length
    if sorted(word.replace("\n", "")) == sorted(anagram):
        possible.append(word)

I needed to use len(word) - 1 to take away the \n.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2025-01-03 04:24:31

(1) 我不明白第一个循环中“len(word)-1”中的“-1”。

(2) 你的第二个循环有几个问题:

它不检查字母是否相同,而是检查字谜中的每个字母是否在单词中。您没有使用计数信息,因此无法区分 bok 和 book。您还从正在迭代的序列中删除,这会导致意外的行为。

就我而言,我只是使用

sorted_anagram = sorted(anagram)
possibles = [word for word in wordlist if sorted(word) == sorted_anagram]

而不是显式的 for 循环。

请注意,对单词进行排序是一种规范化过程——它确保任意两个互为字谜的单词具有相同的格式。确定两个事物是否是字谜词的另一种方法是确保字母计数相同:

>>> from collections import Counter
>>> Counter('book')
Counter({'o': 2, 'k': 1, 'b': 1})
>>> Counter('obko')
Counter({'o': 2, 'k': 1, 'b': 1})
>>> Counter('bok')
Counter({'k': 1, 'b': 1, 'o': 1})
>>> 
>>> Counter('book') == Counter('boko')
True
>>> Counter('book') == Counter('bok')
False

(1) I don't understand the "-1" in "len(word)-1" in your first loop.

(2) Your second loop has several problems:

It doesn't check to see whether the letters are the same, it checks to see whether each letter in the anagram is in the word. You're not using the count information, so you can't distinguish between bok and book. You're also removing from a sequence you're iterating over, which leads to unexpected behaviours.

For my part I'd simply use

sorted_anagram = sorted(anagram)
possibles = [word for word in wordlist if sorted(word) == sorted_anagram]

rather than explicit for loops.

Note that sorting the words is a kind of canonicalization process -- it makes sure that any two words which are anagrams of each other will be in the same format. Another approach to determine whether two things are anagrams would be to make sure that the letter counts are the same:

>>> from collections import Counter
>>> Counter('book')
Counter({'o': 2, 'k': 1, 'b': 1})
>>> Counter('obko')
Counter({'o': 2, 'k': 1, 'b': 1})
>>> Counter('bok')
Counter({'k': 1, 'b': 1, 'o': 1})
>>> 
>>> Counter('book') == Counter('boko')
True
>>> Counter('book') == Counter('bok')
False
笑脸一如从前 2025-01-03 04:24:31

正如评论中提到的,我遇到的两个问题是:

  1. 为什么这样做: if len(word) - 1 == len(anagram) ?
  2. 在迭代时缩短列表是一个很大的禁忌。该行 possible.remove(word) 应该更改。

像这样的东西怎么样:

anagramLength = len(anagram) # Get the value once to save CPU
possible1 = [word for word in wordlist if len(word)-1 == anagramLength] # List iteration
possible2 = [] # Make a new list that will be more constricted
for word in possible: #Checking for same letters
    for letter in anagram:
        if letter not in word:
            break
    else:
        possible2.append(word) # Only called if you don't break the above for loop

对使用的工具的引用:

  1. listIteration
  2. for..else

As mentioned in the comments the two evils that jump out at me are:

  1. Why do: if len(word) - 1 == len(anagram) ?
  2. Shortening a list while iterating it is a big nono. That line possible.remove(word) should be changed.

What about something like this:

anagramLength = len(anagram) # Get the value once to save CPU
possible1 = [word for word in wordlist if len(word)-1 == anagramLength] # List iteration
possible2 = [] # Make a new list that will be more constricted
for word in possible: #Checking for same letters
    for letter in anagram:
        if letter not in word:
            break
    else:
        possible2.append(word) # Only called if you don't break the above for loop

References to tools used:

  1. listIteration
  2. for..else
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文