python - 多条件python
问题描述
我想做一个计算字符串中辅音的函数,所以我尝试这样做:
def vowel_count(foo):
count = 0
for i in foo:
if not i == 'a' and i == 'e' and i ... and i == 'O' and i == 'U':
count += 1
return count
但这非常丑陋和乏味,在更多条件下更是如此。有没有办法把它们放在一起?
解决方案
您正在寻找not in
运营商。
def vowel_count(foo):
count = 0
for i in foo.lower():
if i not in 'aeiou':
count += 1
return count
或更简单地说:
def vowel_count(foo):
return sum(i not in 'aeiou' for i in foo.lower()) # True == 1, False == 0
推荐阅读
- python - 当我移动到另一个用户的个人资料页面时,base.html 上项目(不是应用程序)的 {{ user }} 标记发生了变化。我该如何阻止它这样做?
- php - 如何在 laravel 中创建构建更新?
- php - 获取下一个收集日期,但要考虑公共假期
- mysql - 带有 MySQL 的 Hive-2.3.2 元存储无法启动
- python - 未能创建我的第一个动态 GUI 计算器
- kotlin - 当 N 个事件中的任何一个发生时执行,但使用所有其他事件的最后一个结果
- c# - 为什么 LINQ 查询不返回任何元素?
- javascript - 底部的空白白块,在页脚页面中
- angular - ng 类未响应类属性更改
- favicon - Safari 图标中的书签页面是橙色星星?