Learn Python 012: for loop

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Learn Python 012: for loop相关的知识,希望对你有一定的参考价值。

# 1

vowels = 0
consonants = 0

for letter in ‘supercalifragilisticexpialidocious‘:
    if letter.lower() in ‘aeiou‘:
        vowels = vowels + 1
    elif letter == ‘ ‘:
        pass
    else:
        consonants = consonants + 1
print(‘There are {} vowels in the text.‘.format(vowels))
print(‘And there are {} consonants in the text.‘.format(consonants))

# 2

students = {
    ‘male‘: [‘Bob‘, ‘Clark‘, ‘Frank‘, ‘Graig‘],
    ‘female‘: [‘Alice‘, ‘Emma‘, ‘Helen‘]
}
for key in students.keys():
    for name in students[key]:
        if ‘a‘ in name:
            print(name)

 

以上是关于Learn Python 012: for loop的主要内容,如果未能解决你的问题,请参考以下文章

012python路--迭代器

如何在 scikit-learn 中执行随机森林模型的交叉验证?

如何使用 scikit-learn for python 分析和预测(机器学习)时间序列数据集

012 循环

Learn Python—函数(迭代器生成器)

如何减少运行(for循环),Python