【问题标题】:how do i go over lines in an open text file on Python (2.72)我如何在 Python (2.72) 上打开一个文本文件中的行
【发布时间】:2012-01-06 08:53:10
【问题描述】:

我有包含几千个单词的文本文件(一行一个单词)。 我写了一个函数,它接受两个单词(字符串),并检查一个 word 是另一个词的 Anagram(这意味着如果两个词包含相同的 字母,即使顺序不同)。

现在我想查看我的巨大文本文件并搜索字谜。我的输出应该是一个列表,其中包含几个单词的元组 是字谜。

问题是我不知道如何使用 for/while 循环遍历单词。我尝试过的一切都失败了。 (我很清楚这样做的方式,但我对python还不够了解)。

编辑#1: 假设我想遍历文本中的第 1 到 100 行而不是整个文本,我该怎么做?

【问题讨论】:

    标签: python list text-files anagram


    【解决方案1】:
    file = 'file.txt'
    with open(file, 'r') as f:
        for line in f:
            pass
    

    【讨论】:

      【解决方案2】:

      readlines 为您提供文件中所有单词的列表:

      text = open("myfile.txt")
      wordlist = text.readlines()
      

      现在你只需要执行 for 循环:

      for item in wordlist:
          anagramfunction()...
      

      【讨论】:

      • 你可以做for line in text
      • 好吧,这已经开始帮助我了。但是我如何摆脱每个单词末尾的 2\n 呢?
      【解决方案3】:
      1. 将所有单词(行)加载到列表中,而单词位于单独的行中,这可以通过readlines() 完成(您必须使用strip() 删除行尾):

        words = [s.strip() for s in f.readlines()]

      2. 为每个单词创建字谜

      3. 对该字谜使用单词列表in 运算符来检查字谜是否存在
      4. 如果存在则打印

      【讨论】:

        【解决方案4】:

        我假设您的单词列表没有那么大,它不适合 RAM。这是一个(未优化的)算法,可以构建字谜列表(使用先前答案的位):

        def buildAnagramsList(word, wordList):
            anagramsList = []
            for word2 in wordList:
                if areAnagrams(word, word2): #you already have a similar method
                    list.remove(word2) # Spare some time here by not looking twice for the same anagrams
                    anagramsList.append(word2)
            return anagramsList
        
        file = open("myfile.txt")
        words = [s.strip() for s in file.readlines()]
        anagramsLists = [buildAnagramsList(word, words) for word in words]
        

        【讨论】:

          【解决方案5】:

          Python Tutorial 已覆盖:

          另一种读取行的方法是遍历文件 目的。这是内存效率高、速度快、代码更简单的方法:

          for line in f:
              print line,
          

          您可以使用itertools.combinations获取所有单词组合:

          with open("file.txt") as word_list:
              for (word1, word2) in itertools.combinations(word_list, 2):
                  if anagram(word1, word2):
                      # do stuff
          

          【讨论】:

          • 我需要给我的函数两个词(字符串)。这意味着我需要给它当前的单词,下一行的单词,然后在第三行等等......之后我需要给我的函数在第二行的单词第三行上的单词,而不是第四行中的单词等等……我没有成功。有什么想法吗?
          【解决方案6】:

          我会选择这样的:

          wordList = []
          anagrams = []
          
          file = StringIO.StringIO(open("file.txt","rb"),dialect=csv.excel) //Using csv.excel as each word is on a different line, so hoping this should work but Im not entirely sure
          wordList.extend(wordList)
          

          Wordlist 现在应该类似于 [Word1, Word2, Word3]

          for i in xrange(wordList):
              if wordList[i] == wordList[i+1]://Code to analyse anagrams here
                  anagrams.append(wordList[i])
          

          我真的不确定这种语法,我让你知道我会做什么。虽然有人可能会否决这个答案,因为它不在我的脑海中,你必须阻止它抛出 OutOfBounds 错误,但我没有太多时间来写它! :P

          【讨论】:

            猜你喜欢
            • 2011-12-20
            • 1970-01-01
            • 1970-01-01
            • 2021-02-03
            • 2021-03-22
            • 1970-01-01
            • 2014-03-18
            • 2022-06-15
            • 2019-01-30
            相关资源
            最近更新 更多