给定一个文本文件"words.txt",使用列表理解来读取文件中的所有单词,并找到至少包含两个元音的所有单词。
所以,我有一个文本文件:
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]
发布于 2014-06-29 19:22:47
这里有一些需要处理的角落案例,但是如果您假设一个简单的文本文件:
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()]
发布于 2014-06-30 07:04:20
我在一个更大的数据集中运行了一个稍微不同的版本,并发现对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}
正如每个人都已经很明显地提到的,这不是您想要编写的代码,其他人将不得不阅读。虽然我承认我也能理解一条蟒蛇的list
理解是多么的有趣:
发布于 2014-06-30 13:30:21
所以,谢谢大家的意见。有很多非常有趣的方法可以找到有两个或更多元音的单词。今天早上我和我的教授谈到了我在这个问题上遇到的困难,他澄清了我的一个误解。
我的印象是,他希望单表理解返回一个列表,其中包含了所有的单词在档案中,和一个名单上只有两个或两个以上元音的单词。但是,实际上,他只是想要我已经完成的工作;每个场景的列表理解:文件中的所有单词;文件中带有两个或多个元音的所有单词。
谢谢大家的投入!
https://stackoverflow.com/questions/24482050
复制相似问题