【发布时间】:2021-03-19 03:30:00
【问题描述】:
count = 0
vowels = "aeiou"
open("TEXT FILE PATH", "r") as text:
text = text.read()
for character in range(len(text) - 1):
if text[(character + 1) and (character - 1)] not in vowels and text[character] in vowels:
count += 1
在上面的“if 语句”中,我试图检查“字符”前面和后面的字符串是否不是元音,同时最小化我的条件的长度,但在 Python 中,这在下面的行中不起作用。
if text[character] in vowels and text[(character + 1)] not in vowels and text[character - 1] not in vowels:
基本上,这个想法不是像上面那行那样有 3 英里长的代码。是否有另一种方法可以使这段代码更短和/或更高效?
【问题讨论】:
-
通过
text[(character + 1) and (character - 1)]对字符串进行索引不是它的工作原理。
标签: python arrays performance if-statement conditional-statements