1

我正在尝试做的事情:通过过滤较低的列表,创建并打印满足以下所有条件的单词列表:

该词至少有 6 个字符长;该单词至少包含 3 次字母“e”;字母“e”的第一次出现在单词的后半部分。

到目前为止我有这个: 之前使用过:

words = [ line.strip() for line in file(DATA+'english_wordlist.txt') ]

(lowers 在我之前的作品中被定义为部分单词)

[word for word in lowers if len(word)>=6 and word.count('e')>=3 and 'e' is after word(len(word)/2::)]

我知道 'e' 在 word(len(word)/2::) 之后是错误的,但这只是我的粗略逻辑。我将如何做到这一点?

4

3 回答 3

1
and word.index('e') >= len(word)/2
于 2012-09-25T18:25:02.397 回答
0
[word for word in lowers if len(word)>=6 and word.count('e')>=3 and 
'e' not in word[int(len(word)/2) + 1:]]

len(word)/2true divisionPython 中.. 所以它可能会给出浮点值.. 所以,将它的类型转换为int并添加 1 以在中间之后移动它..

*编辑:-或者您可以简单地使用len(word)//2(which is floor division) 并且不需要进行类型转换。所以,这是一个替代方案:-

[word for word in lowers if len(word)>=6 and word.count('e')>=3 and 
'e' not in word[(len(word)//2) + 1:]]
于 2012-09-25T18:26:26.337 回答
0

检查'e'首先出现在单词的后半部分,就相当于知道它没有出现在单词的前半部分。那应该对你有帮助。

于 2012-09-25T18:26:50.600 回答