【问题标题】:Python- The Word Game "Ghost", File I/O, and List questionPython - 文字游戏“Ghost”、文件 I/O 和列表问题
【发布时间】:2011-06-13 11:13:48
【问题描述】:

我想为文字游戏Ghost 创建计算机。但是,我在思考一个处理访问庞大单词列表的好方法时遇到了问题。这是我当前的实现(不起作用):

import os, random, sys, math, string


def main():

    #Contains a huge wordlist-- opened up for reading
    dictionary = open("wordlist.txt", "r")
    wordlist = []
    win= 0
    turn= 0
    firstrun = 0
    word = ""

    #while nobody has won the game
    while win==0:
        if turn == 0:
            #get first letter from input
            foo = raw_input("Choose a letter: ")[0]
            word+=foo
            print "**Current word**: "+ word
            #Computer's turn
            turn = 1
        if turn == 1:
            #During the first run the program gets all definitively 
            #winning words (words that have odd-number lengths)
            #from the "dictionary" file and puts them in a list
            if firstrun== 0:
                for line in dictionary:
                    #if the line in the dictionary starts with the current 
                    #word and has an odd-number of letters                                                                                                
                    if str(line).startswith(word) and len(line)%2 == 0:
                        wordlist.append(line[0: len(line)-1])
                print "first run complete... size = "+str(len(wordlist))
                firstrun = 1
            else:   #This is run after the second computer move         
                for line in wordlist:
                    #THIS DOES NOT WORK-- THIS IS THE PROBLEM.
                    #I want it to remove from the list every single
                    #word that does not conform to the current limitations
                    #of the "word" variable. 
                    if not line.startswith(word): 
                        wordlist.remove(line)
                print "removal complete... size = "+str(len(wordlist))

            turn = 0




if __name__ == "__main__":
    main()

我已经在代码中划定了问题区域。我不知道为什么它不起作用。应该发生什么:想象一下列表中填充了所有以“a”开头的单词。然后用户选择字母“b”。然后目标词必须具有开头字母“ab”。应该发生的是,列表中所有不直接跟在“b”后面的“a”单词都应该被删除。

如果有人可以让我知道一种更有效的方法,然后制作一个庞大的初始列表,我也将不胜感激。

【问题讨论】:

  • 应该发生什么:想象一下列表中填充了所有以“a”开头的单词。然后用户选择字母“b”。目标词则具有开头字母“ab”。应该发生的是,列表中所有不直接跟在“b”后面的“a”单词都应该被删除。
  • 您不能在迭代列表时从列表中删除内容。 “巨大”有多大?如果它只是很大,请在每个字母后制作一个新列表。如果真的很大,请使用数据库,例如 sqlite。

标签: python string file list


【解决方案1】:

我建议不要从您的列表中删除单词。这会非常慢,因为在列表中间删除是 O(N)。

最好只创建一个新列表。一种可能的方法是替换行

for line in wordlist:
    if not line.startswith(word):
        wordlist.remove(line)

wordlist = [w for w in wordlist if w.startswith(word)]

【讨论】:

  • 有没有办法以类似的方式压缩我的任何其他语句? (我是 Python 新手,所以我想学习如何尽可能高效和 Python)
  • 另外,删除所有包含较小单词的单词的简单方法是什么?例如,单词“crappy”应该无法播放,因为它包含单词“crap”
【解决方案2】:

Peter Norvig 在此处对自动更正进行了精彩的讨论:

http://norvig.com/spell-correct.html

大列表理解和上下文匹配似乎相关 - 21 行是快速阅读(后面有很好的解释)。还可以查看关于使用 python 进行函数式编程的“迷人的 python”3 部分(IBM)。您可以通过一些列表推导来完成所有设置。

【讨论】:

    【解决方案3】:

    如果您想为 ITA 工作,最好用 clisp... 或 clojure 编写此内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多