假设s
是一串小写字符。
编写一个程序,计算字符串s
中包含的元音数量。有效的元音包括:'a'
,'e'
,'i'
,'o'
和'u'
。例如,如果s = 'azcbobobegghakl'
,您的程序应打印:
元音数量:5
到目前为止我有这个
count = 0
vowels = 'a' or 'e' or 'i' or 'o' or 'u'
for vowels in s:
count +=1
print ('Number of vowels: ' + count)
谁能告诉我它有什么问题?
答案 0 :(得分:7)
有几个问题。首先,您对vowels
的分配并不符合您的想法:
>>> vowels = 'a' or 'e' or 'i' or 'o' or 'u'
>>> vowels
'a'
Python懒惰地评估or
;只要任何谓词评估True
,就会返回它。非空序列(包括""
以外的字符串评估True
,因此会立即返回'a'
。
其次,当您迭代s
时,无论如何都会忽略该分配:
>>> for vowels in "foo":
print(vowels)
f
o
o
for x in y:
依次将可迭代y
中的每个项目分配给名称x
,因此以前分配给x
的所有内容都无法通过该名称访问。
我认为你想要的是:
count = 0
vowels = set("aeiou")
for letter in s:
if letter in vowels:
count += 1
答案 1 :(得分:7)
首先,试试这个:
In [9]: V = ['a','e','i','o','u']
In [10]: s = 'azcbobobegghakl'
In [11]: sum([1 for i in s if i in V])
Out[11]: 5
答案 2 :(得分:5)
使用自己的循环。
count = 0
vowels = ['a' , 'e' , 'i' ,'o' , 'u']
for char in s:
if char in vowels: # check if each char in your string is in your list of vowels
count += 1
print ('Number of vowels: ' + str(count)) # count is an integer so you need to cast it as a str
您也可以使用字符串格式:
print ('Number of vowels: {} '.format(count))
答案 3 :(得分:2)
x = len(s)
a = 0
c = 0
while (a < x):
if s[a] == 'a' or s[a] == 'e' or s[a] == 'i' or s[a] == 'o' or s[a] == 'u':
c += 1
a = a+1
print "Number of vowels: " + str(c)
以上代码适用于初学者
答案 4 :(得分:1)
这是一个简单的:
count = 0 #initialize the count variable
def count_vowel(word): #define a function for counting the vowels
vowels = 'aeiouAEIOU' #A string containing all the vowels
for i in range(word): #traverse the string
if i in vowels: #check if the the character is contained in the vowel string
count = count + 1 #update the count
return count
答案 5 :(得分:0)
您可以使用此代码查找计数
{{1}}
答案 6 :(得分:0)
for i in s:
if i in vowels:
num_vowels += 1
print (num_vowels)
答案 7 :(得分:0)
str = 'aeioubbaeiouggaeiouss'
vow = set("aeiouAEIOU")
count = 0
for alpha in str:
if alpha in vow:
count += 1
print(count)
计数将给出元音进入字符串的总次数。
答案 8 :(得分:0)
这是一个利用Procfile
运算符和web
循环的简单解决方案:
in
为什么for
?要使其与任何包含大写字母的元音的字符串一起使用。
答案 9 :(得分:0)
你需要一个数组。这是一个元素序列,在你的情况下是字符。这是一种在Python中定义数组的方法:
vowels = ['a','e','i','o','u']
答案 10 :(得分:0)
//Using while loop:-
%%time
s = 'services'
m = list(s)
count = 0
while m:
d = m.pop()
if (d is 'a') or (d is 'e') or (d is 'i') or (d is 'o') or (d is 'u'):
count += 1
结果是: - CPU时间:用户0 ns,sys:0 ns,总计:0 ns 壁挂时间:18.8μs //使用for循环一行: -
%%time
vowels = ['a', 'e', 'i', 'o', 'u']
sum([s.count(elem) for elem in vowels])
结果是: - CPU时间:用户0 ns,sys:0 ns,总计:0 ns 壁时间:18.6μs
答案 11 :(得分:0)
strings = raw_input('Enter a string: ')
vowels = 0
for char in strings:
if(char ==('a') or char ==('e') or char ==('i') or char ==('o') or char ==('u')):
vowels += 1
print('Number of vowels: ' + str(vowels))
答案 12 :(得分:0)
count=0
for letter in s:
if(letter=='a' or letter == 'e' or letter =='i' or letter =='o' or letter=='u'):
count=count+1
print("Number of vowels:",count)
答案 13 :(得分:0)
total = 0
for c in s:
if c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u':
total += 1
print "Number of vowels: " + str(total)
替代解决方案
num = 0
for letter in s:
if letter in "aeiou":
num+=1
print "Number of vowels:" + str(num)
答案 14 :(得分:0)
用于计算字符串中的元音
s = "Some string here"
或
s = intput(raw_input("Enter ur string"))
s1 = s.lower()
count = 0
vowels = set("aeiou")
for letter in s1:
if letter in vowels:
count += 1
print 'Number of vowels:' ,count
这将给出给定字符串中元音总数的输出
答案 15 :(得分:0)
我的解决方案:
s = 'aassfgia'
vowels = 0
for x in s:
if x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u':
vowels += 1
print x
print vowels
答案 16 :(得分:0)
这是一个使用Counter的样本,对于较大的字符串来说,它比Sundar更紧凑,甚至更快一点:
from collections import Counter
cnt = Counter('this and that')
sum([cnt[x] for x in 'aeiou'])
以下是比较3种方法的时间测试:
import time
from collections import Counter
s = 'That that is is is not that that is not is not. This is the understanding of all who begin to think.'
c = Counter(s)
dt1 = dt2 = dt3 = dt4 = 0;
vowels = ['a' , 'e' , 'i' ,'o' , 'u']
for i in range(100000):
ms0 = time.time()*1000.0
s1 = sum([c[x] for x in 'aeiou'])
ms1 = time.time()*1000.0
dt1 += ms1 - ms0
for i in range(100000):
ms1 = time.time()*1000.0
s2 = sum([c[x] for x in set(vowels).intersection(c.keys())])
ms2 = time.time()*1000.0
dt2 += ms2 - ms1
for i in range(100000):
ms2 = time.time()*1000.0
s3 = 0
for char in s:
if char in vowels: # check if each char in your string is in your list of vowels
s3 += 1
ms3 = time.time()*1000.0
dt3 += ms3 - ms2
print('sums:', s1, s2, s3)
print('times:', dt1, dt2, dt3)
print('relative: {:.0%}{:.0%}{:.0%}'.format(dt1/dt2, dt2/dt2, dt3/dt2))
结果(平均六次运行),版本:这个,Sundar,简单
总和:26 26 26
时间:392 494 2626
相对:80%100%532%
答案 17 :(得分:0)
这也是另一种解决方案,
In [12]: vowels = ['a', 'e', 'i', 'o', 'u']
In [13]: str = "azcbobobegghakl"
In [14]: sum([str.count(elem) for elem in vowels])
Out[14]: 5
使用string.count()
答案 18 :(得分:-1)
print("\nThe count of vowels are:",sum([1 for i in input("\nEnter the string\n\n").lower() if i in ['a','e','i','o','u']]),"\n")
输入字符串
azcbobobegghakl
元音的数量是:5
print("\nThe count of vowels are:",sum([1 for i in input("\nEnter the string\n\n").lower() if i in ['a','e','i','o','u']]),"\n")
输入字符串
azCBOBOBegghakl
元音的数量是:5
答案 19 :(得分:-1)
使用计数器的不同实现
from collections import Counter
s='azcbobobegghakl'
vowels="aeiou"
c=Counter(s)
print sum([c[i] for i in set(vowels).intersection(c.keys())])
语句set(vowels).intersection(c.keys())
这将返回句子中出现的dictint元音