简体   繁体   English

在Python中从.txt文件向字典添加字谜?

[英]Adding anagrams to a dictionary from a .txt file in Python?

I have a text file which is a long list of words with no spaces eg"neuropsychologicalneuropsychologistneuropsychologyneuropsychopathic" etc I am trying to build a program that will take an inputted word and check the .txt file for any anagrams of the word and then add them to a dictionary. 我有一个文本文件,该文件是一长串单词,没有空格,例如“ neuropsychologicalneuropsychologistneuropsychologyneuropsychopathic”等。我正在尝试构建一个程序,该程序将使用输入的单词并检查.txt文件中该单词的任何字谜,然后将其添加到一本字典。

The code I have come up with so far that prints 1 dictionary with 1 permutation contained in words.txt is; 到目前为止,我想出的代码是打印出words.txt中包含1个排列的1个字典。

anagram_dict  = {}

def anagram(word):

    b = open('words.txt', 'r').readlines()
    text = ''.join(line.strip() for line in b)

    #print(len(text))

    for i in range(len(text)-len(word)):
        prop = text[i:i+len(word)]
        if all(char in word for char in prop): #and all(prop.count(char) == prop.count(word) for char in prop):
            anagram_dict[''.join(sorted(word))] = [prop]

    print(anagram_dict)

anagram("demand")

The issue is that the permutation it prints isn't an anagram (I input "demand" expecting "madden"to come out but it prints "amamad" which is in the file but not an anagram) How can I make it so that it only prints the dictionary if the permutation is a true anagram (same letters just rearranged)? 问题是它打印的排列不是一字谜(我输入“需求”,期望“ madden”出来,但它打印文件中的“ amamad”,但不是字谜)我该如何制作它仅在排列是真正的字谜(只是重新排列相同的字母)时才打印字典?

I think the problem is possibly with the "if all(char in word for char in prop): # and all(prop.count(char) == prop.count(word) for char in prop):" 我认为问题可能在于“如果all(prop中的char为单词char):#和all(prop中的char.prop.count(char)== prop.count(word)):”

Especially the part that's commented out as when that runs it just prints an empty string. 特别是在运行时被注释掉的部分只会打印一个空字符串。

Apologies for this being so long I just wanted to make sure I explained myself. 很长的歉意,我只是想确保我自己解释了一下。 Thanks for any help. 谢谢你的帮助。

all(char in word for char in prop) simply checks if all the letters of word are in prop . all(char in word for char in prop)只是检查word所有字母是否都在prop All the letters of demand are "present" in "madden" , so it would show up in the results. 需求的所有字母是"present""madden" ,所以它会在搜索结果中。

There is problem with all(prop.count(char) == prop.count(word) for char in prop) (probably a typo). all(prop.count(char) == prop.count(word) for char in prop)有问题(可能是拼写错误)。

It should be all(prop.count(char) == word.count(char) for char in prop) 它应该all(prop.count(char) == word.count(char) for char in prop)


There is also a better alternative, check if sorted versions of prop and word are same. 还有一个更好的选择,检查propword排序版本是否相同。

sword = ''.join(sorted(word))
anagram_dict[sword] = []
for i in range(len(text)-len(word)):
    prop = text[i:i+len(word)]
    if sword == ''.join(sorted(prop)):
        anagram_dict[sword].append(prop)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM