【问题标题】:Python List Comprehension Personal ChallengePython 列表理解个人挑战
【发布时间】:2014-06-30 01:58:11
【问题描述】:

给定一个文本文件“words.txt”,使用列表推导读入文件中的所有单词,找出所有至少包含 2 个元音的单词。

所以,我有一个文本文件:

The quick brown fox jumps over the lazy dog

而且,获取所有单词以及所有具有两个或多个元音的单词的最佳尝试是:

#This could be hardcoded in, but for the sake of simplicity (as simple as simplicity gets)
vowels = ["a","e","i","o","u"]
filename = "words.txt"
words = [word for word in open(filename, "r").read().split()]
multivowels = [each for each in open(filename, "r").read().split() if sum(letter in vowels for letter in each) >= 2]

输出应该模仿:

All words in the file:  ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
The words in the file that contain 2 or more vowels: ['quick', 'over']

我试图把它放在一行中只是打印“单词”和“多元音”的列表理解侧以及“文件中的所有单词;”......等等。

有没有人挑战将这两个列表推导合二为一?我和我的队友很困惑,但很想向我们的教授炫耀!

我最后的单行代码是:

print "All words in the file: " + str([word for word in open(filename, "r").read().split()]) + "\nAll words with more than 2 vowels: " + str([each for each in open(filename, "r").read().split() if sum(letter in vowels for letter in each) >= 2])

编辑: 我尝试获取文件中的所有单词,以及包含两个或多个元音的所有单词。

vowels = ["a", "e", "i", "o", "u"]
filename = "words.txt"
print [(word, each) for word in open(filename, "r").read().split() if sum([1 for each in word if each in vowels]) >= 2]

【问题讨论】:

  • 为什么要写成一行?它使 FAR 的调试和可读性变得更加困难
  • 我不会读两次文件。
  • 您的教授是否对您让其他人解决问题表示赞赏?
  • 写成一行的原因只是为了向我的教授证明我可以战胜他的挑战。毫无疑问,这绝对是一种可怕的编码方式。但是,他挑战我这样做,所以我至少要尝试一下!这只是为了好玩。我只是想看看是否有人有任何见解。
  • 可以使用.seek(0)重置已经打开的文件

标签: python file list-comprehension word


【解决方案1】:

这里有一些特殊情况需要处理,但如果你假设一个简单的文本文件:

import re
vowels = "a","e","i","o","u"

answer = [[word for word in re.sub("[^\w]", " ",  sentence).split() if (sum(1 for letter in word if letter in vowels)>=2)] for sentence in open(filename,"r").readlines()]

【讨论】:

    【解决方案2】:

    所以,谢谢大家的意见。有很多非常有趣的方法可以找到带有两个或更多元音的单词。今天早上我和我的教授讨论了我在这个问题上遇到的困难,他澄清了我的一个误解。

    我的印象是他希望单个列表推导返回一个列表,其中包含文件中所有单词的列表,以及仅包含两个或多个元音的单词的列表。但事实上,他只是想要我已经完成的;每个场景的列表理解:文件中的所有单词;文件中包含两个或多个元音的所有单词。

    感谢大家的意见!

    【讨论】:

      【解决方案3】:

      我在更大的数据集上运行了一个稍微不同的版本,发现对str 的重复调用开始增加。

      vowels = ['a', 'e', 'i', 'o', 'u']
      #filename = 'vowelcount.txt'
      filename = 'largetextfile.txt'
      print "All words in the file: ", [w for w in open(filename).read().split()], "\n", "All words with more than 2 vowels: ", [w for w in open(filename).read().split() if sum(1 for l in w if l in vowels) > 1]
      

      cProfile 调用这个版本会显示一个小的改进:

      python -m cProfile vowelcount1.py

         7839 function calls in 0.023 seconds
      
         Ordered by: standard name
      
         ncalls  tottime  percall  cumtime  percall filename:lineno(function)
              1    0.005    0.005    0.023    0.023 vowelcount1.py:1(<module>)
           6045    0.009    0.000    0.009    0.000 vowelcount1.py:4(<genexpr>)
              1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
              2    0.000    0.000    0.000    0.000 {method 'read' of 'file' objects}
              2    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
              2    0.000    0.000    0.000    0.000 {open}
           1786    0.009    0.000    0.018    0.000 {sum}
      

      您输入的代码的函数调用次数大约是原来的两倍:

      #filename = 'vowelcount.txt'
      filename = 'largetextfile.txt'
      vowels = ['a', 'e', 'i', 'o', 'u']
      print "All words in the file: " + str([word for word in open(filename, "r").read().split()]) + "\nAll words with more than 2 vowels: " + str([each for each in open(filename, "r").read().split() if sum(letter in vowels for letter in each) >= 2])
      

      python -m cProfile vowelcount2.py

         14568 function calls in 0.036 seconds
      
         Ordered by: standard name
      
         ncalls  tottime  percall  cumtime  percall filename:lineno(function)
              1    0.004    0.004    0.036    0.036 vowelcount2.py:2(<module>)
          12774    0.016    0.000    0.016    0.000 vowelcount2.py:4(<genexpr>)
              1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
              2    0.000    0.000    0.000    0.000 {method 'read' of 'file' objects}
              2    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
              2    0.000    0.000    0.000    0.000 {open}
           1786    0.016    0.000    0.032    0.000 {sum}
      

      正如每个人都已经明确提到的,这不是您想要编写其他人必须阅读的代码的方式。虽然我承认看到我可以适应一个 python list 也很有趣:D

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多