python - 读取txt文件寻找匹配的混拼单词

 多米音乐_35780104 发布于 2022-10-27 10:51

有一个类似字典的txt文件,读取后输入一段混乱的字母寻找匹配的单词(Word Jumble)。
并且print在这么多数量的单词中有几个是匹配的
输出结果应该类似

  $ python3 jumbler.py trsesi dict.txt
  resist
  sister
  2 matches in 41238 words
  $ python3 jumbler.py rayin dict.txt
  rainy
  1 match in 41238 words
  $ python3 jumbler.py tororo dict.txt
  No matches
  $ python3 jumbler.py taroro dict.txt
  orator
  1 match in 41238 words

字典的文件类似

a
aardvark
abaci
aback
abacus
abaft
abalone
abandon
abandoned
abandonment
abase
abasement
abash
abashed
abashedly
abashment
abate
abatement
abattoir
abbe
abbess
abbey
abbot
abbreviate
abbreviated
abbreviation
abdicate
abdication
abdomen
abdominal

我写的代码如下

import argparse

def jumbler(jumble, dict_file_name):
    """
    supply an excellent docstring here
    """

    # first you must open the file

    # second you must read each word from the file and perform an
    # appropriate comparison of each with 'jumble'; you need to count the
    # number of lines read from the file

    # if a word matches 'jumble', you are to print the word on a line by itself

    # after you have read each word from the file and compared, you need to
    # close the file

    # assume that there were MATCHES words that matched, and NLINES in the file
    # if there was a single match, you need to print
    # "1 match in NLINES words", where NLINES is replaced by the value of NLINES
    # if there were two or more matches, you need to print
    # "MATCHES matches in NLINES words"
    # if there were no matches, you need to print
    # "No matches"
    wordlist = []
    dictionary = open(dict_file_name,"r")
    for line in dictionary.readlines():
        wordlist.extend(line.split(','))#修改后
    for word in wordlist:
        if sorted(str(jumble))==sorted(str(word)):
            print(word+"\n")
        else:
            print("No matches")
    dictionary.close()



def main():
    """
    collect command arguments and invoke jumbler()
    inputs:
        none, fetches arguments using argparse
    effects:
        calls jumbler()
    """
    parser = argparse.ArgumentParser(description="Solve a jumble (anagram)")
    parser.add_argument("jumble", type=str, help="Jumbled word (anagram)")
    parser.add_argument('wordlist', type=str,
                        help="A text file containing dictionary words, one word per line.")
    args = parser.parse_args()  # gets arguments from command line
    jumble = args.jumble
    wordlist = args.wordlist
    jumbler(jumble, wordlist)

if __name__ == "__main__":
    main() 

但是打出来是一行行no matches....求大神赐教

我根据两位评论修改了list.extend,但是结果还是满满的no matches..不是很理解

ps.我们作业都是要求在终端run的所以会有额外的代码

我的output如下

No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
No matches
2 个回答
  • 原因很简单,问题出在这里:

    for line in dictornary.readlines():
        wordlist.append(line.split(','))    # line.split()得到的是一个list,所以wrodlist最终会是一个二维列表

    你稍微修改一下代码应该就可以:

    wordlist.extend(line.split(','))

    不过你把所有的单词都存储在list里是很耗费内存的,如果词库文件特别大的话……
    个人建议你只用把匹配的词存在list里,不匹配的完全没有必要理会。

    2022-10-27 20:57 回答
  • 首先,string.split() 会将字符串分割成列表, list.append() 则将整个参数作为一个元素添加到列表中,因此,你代码中的

    wordlist.append(line.split(','))

    会使得wordlist成为列表的列表,即wordlist中每个元素都是一个列表,而不是你期望的单个单词,你应该用:

    wordlist.extend(line.split(',')) 

    或者

    wordlist += line.split(',')

    其次,readlines() 返回的字符串包含行尾的换行符,如下代码所示:

    >>> 'abbe\n'.split()
    ['abbe']
    >>> 'abbe\n'.split(',')
    ['abbe\n']

    应该把line.split(',')改为 line.split()

    参考代码如下:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import argparse
    
    def jumbler(jumble, dict_file_name):
        """
        supply an excellent docstring here
        """
    
        # first you must open the file
    
        # second you must read each word from the file and perform an
        # appropriate comparison of each with 'jumble'; you need to count the
        # number of lines read from the file
    
        # if a word matches 'jumble', you are to print the word on a line by itself
    
        # after you have read each word from the file and compared, you need to
        # close the file
    
        # assume that there were MATCHES words that matched, and NLINES in the file
        # if there was a single match, you need to print
        # "1 match in NLINES words", where NLINES is replaced by the value of NLINES
        # if there were two or more matches, you need to print
        # "MATCHES matches in NLINES words"
        # if there were no matches, you need to print
        # "No matches"
        line_count = 0
        match_count = 0
        dictionary = open(dict_file_name,"r")
        for line in dictionary.readlines():
            line_count += 1
            for word in line.split():
                if sorted(str(jumble)) == sorted(str(word)):
                    match_count += 1
                    print(word)
        if match_count == 0:
            print("No matches")
        else:
            print('%d matches in %d words' %(match_count, line_count))
        dictionary.close()
    
    
    
    def main():
        """
        collect command arguments and invoke jumbler()
        inputs:
            none, fetches arguments using argparse
        effects:
            calls jumbler()
        """
        parser = argparse.ArgumentParser(description="Solve a jumble (anagram)")
        parser.add_argument("jumble", type=str, help="Jumbled word (anagram)")
        parser.add_argument('wordlist', type=str,
                            help="A text file containing dictionary words, one word per line.")
        args = parser.parse_args()  # gets arguments from command line
        jumble = args.jumble
        wordlist = args.wordlist
        jumbler(jumble, wordlist)
    
    if __name__ == "__main__":
        main()
    
    2022-10-27 20:58 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有