更新时间:2023-09-18 22:19:28
运算符$ c>运算符$ c>运算符 $ c> ==
运算符 - 在
运算符允许您检查特定项目是否在序列/集合中。1 in [1,2,3]#True
$ p>
1 in [2,3,4]#False
'a'in ['a','e','i','o','u']#True
'a'in'aeiou'#也是True其他一些注释:
$ c>,这是一种专门为是该组项目的项目X部分类型的操作而设计的数据类型。
code> vowels = set(['a','e','i','o','u'])*
dict
s也有效,在
在字符串上迭代
字符串是Python中的序列类型,这意味着你不需要去获取长度然后使用索引的所有努力 - 你可以只是遍历字符串,你会得到每个字符轮流:
例如:
用于my_string中的字符:
元音:
#...
使用字符串初始化集合
上面,您可能已经注意到,创建一个具有预设值的集合(至少在Python 2.x中)涉及使用列表。这是因为
set()
类型构造函数接受一系列项。你还可能注意到,在上一节中,我提到字符串是Python中的序列 - 字符序列。这意味着如果你想要一个设置字符,你实际上可以将这些字符的字符串传递给
set()
构造函数 - 您不需要有一个列表,字符串。换句话说,以下两行是等效的:set_from_string = set('aeiou')
set_from_list = set (['a','e','i','o','u'])整洁,是吗? :)不过请注意,如果您想要设置一组字符串,而不是一组字符,这也可能会使您感到困惑。例如,以下两行不是相同:
set_with_one_string = set(['cat '])
set_with_three_characters = set('cat')一个元素:
'cat'in set_with_one_string#True
'c'in set_with_one_string#False后者是一个包含三个元素(每个元素都是一个字符)的集合:
'c'in set_with_three_characters`#True
'cat'in set_with_three_characters#False
区分大小写
比较字符区分大小写。
'a'=='A'
为False, $'A'in'aeiou'
。为了解决这个问题,你可以转换你的输入,以匹配你正在比较的情况:lowercase_string = input_string。 lower()
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()
It counts the vowels fine, but due to
if string[i] == vowels[i]:
, it will only count one vowel once asi
keeps increasing in the range. How can I change this code to check the inputted string for vowels without encountering this problem?
in
operatorYou probably want to use the
in
operator instead of the==
operator - thein
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:
Sets
The
in
operator is most efficient when used with aset
, 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
s are also efficient within
, which checks to see if a key exists in the dict.Iterating on strings
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:
E.g.:
for character in my_string: if character in vowels: # ...
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.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
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()