从原始输入计算元音 [英] Count vowels from raw input

查看:24
本文介绍了从原始输入计算元音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作业问题,要求通过原始输入读取字符串并计算字符串中有多少个元音.这是我到目前为止所拥有的,但我遇到了一个问题:

I have a homework question which asks to read a string through raw input and count how many vowels are in the string. This is what I have so far but I have encountered a problem:

def vowels():
    vowels = ["a","e","i","o","u"]
    count = 0
    string = raw_input ("Enter a string: ")
    for i in range(0, len(string)):
        if string[i] == vowels[i]:
            count = count+1
    print count

vowels()

它计算元音很好,但是由于if string[i] == metadata[i]:,它只会计算一个元音一次,因为i不断增加范围中.如何更改此代码以检查输入的字符串是否有元音而不会遇到此问题?

It counts the vowels fine, but due to if string[i] == vowels[i]:, it will only count one vowel once as i keeps increasing in the range. How can I change this code to check the inputted string for vowels without encountering this problem?

推荐答案

in operator

您可能想要使用 in 运算符而不是 == 运算符 - in 运算符可让您检查特定的项目在一个序列/集合中.

in operator

You probably want to use the in operator instead of the == operator - the in operator lets you check to see if a particular item is in a sequence/set.

1 in [1,2,3] # True
1 in [2,3,4] # False
'a' in ['a','e','i','o','u'] # True
'a' in 'aeiou' # Also True

<小时>

其他一些评论:


Some other comments:

in 运算符与 set 一起使用时效率最高,set 是一种数据类型,专门设计用于快速处理是这组项目的 X 部分"" 种操作.*

The in operator is most efficient when used with a set, which is a data type specifically designed to be quick for "is item X part of this set of items" kind of operations.*

vowels = set(['a','e','i','o','u'])

*dict 使用 in 也很有效,它检查字典中是否存在一个键.

*dicts are also efficient with in, which checks to see if a key exists in the dict.

字符串是 Python 中的序列类型,这意味着您无需费力获取长度然后使用索引——您只需遍历字符串即可获得每个字符反过来:

A string is a sequence type in Python, which means that you don't need to go to all of the effort of getting the length and then using indices - you can just iterate over the string and you'll get each character in turn:

例如:

for character in my_string:
    if character in vowels:
        # ...

用字符串初始化一个集合

在上文中,您可能已经注意到,创建具有预设值的集合(至少在 Python 2.x 中)涉及使用列表.这是因为 set() 类型构造函数接受一个项目序列.您可能还注意到,在上一节中,我提到字符串是 Python 中的序列——字符序列.

Initializing a set with a string

Above, you may have noticed that creating a set with pre-set values (at least in Python 2.x) involves using a list. This is because the set() type constructor takes a sequence of items. You may also notice that in the previous section, I mentioned that strings are sequences in Python - sequences of characters.

这意味着如果你想要一个 set 字符,你实际上可以将这些字符的字符串传递给 set() 构造函数 - 你不需要't 需要有一个单字符串列表.换句话说,下面两行是等价的:

What this means is that if you want a set of characters, you can actually just pass a string of those characters to the set() constructor - you don't need to have a list one single-character strings. In other words, the following two lines are equivalent:

set_from_string = set('aeiou')
set_from_list = set(['a','e','i','o','u'])

整洁吧?:) 但是请注意,如果您尝试制作一组​​字符串,而不是一组字符,这也可能会困扰您.例如,以下两行相同:

Neat, huh? :) Do note, however, that this can also bite you if you're trying to make a set of strings, rather than a set of characters. For instance, the following two lines are not the same:

set_with_one_string = set(['cat'])
set_with_three_characters = set('cat')

前者是一个元素的集合:

The former is a set with one element:

'cat' in set_with_one_string # True
'c' in set_with_one_string # False

而后者是包含三个元素的集合(每个元素一个字符):

Whereas the latter is a set with three elements (each one a character):

'c' in set_with_three_characters` # True
'cat' in set_with_three_characters # False

区分大小写

比较字符区分大小写.'a' == 'A' 是 False,就像 'A' in 'aeiou' 一样.为了解决这个问题,您可以转换您的输入以匹配您正在比较的情况:

Case sensitivity

Comparing characters is case sensitive. 'a' == 'A' is False, as is 'A' in 'aeiou'. To get around this, you can transform your input to match the case of what you're comparing against:

lowercase_string = input_string.lower()

这篇关于从原始输入计算元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆