python - 识别列表中的单词是否以元音开头
问题描述
我需要确定这些单词是否以元音开头。
year= ["january","february","march","april","may","june","july","august","september","october","november","december"]
vowels = "aeiou"
for month in range(len(year)):
if month.startswith(vowels):
if_vowel = "Yes"
else:
if_vowel = "No"
print("Does the month of {0} start with a vowel? {1}".format(year[i],if_vowel)
理想情况下,它会打印 Yes 或 No 取决于月份是否以元音开头
解决方案
for month in year:
print(f"Does the month of {month} start with a vowel? {'Yes' if month[0] == 'e' else 'No'}")
您可以使用 Python3 的f-String进行更轻松的格式化。然后,在 f-String 中,您可以使用条件表达式,例如如果月份的第一个字母是“e”则返回“Yes”,否则返回“No”。
推荐阅读
- flutter - Flutter 断言失败:第 513 行 pos 14:'file != null':不正确
- python - 是否可以操作烧瓶日志字符串?
- ios - __attribute__((constructor)) 为 iOS 编译成什么?如何在手写asm中做到这一点?
- r - 删除R中数据框中的重复行
- mysql - SQL 最大记录数
- python - 无法使用 scipy.arff.loadarff 加载 arff 数据集
- scala - Flink DataSet.map 得到'错误:缺少参数类型'
- python - 用于包装不同框架的抽象 ETL 流程的设计架构
- javascript - 如何使用 vue/Quasar 进行日期范围验证
- swift - Xcode 编译器错误 - 更改“优化速度”设置会导致我的应用程序崩溃