Python 巨蟒计数元音
我已经开始了一个计算元音的程序,但似乎一无所获。我需要计算字符串中的元音,然后显示元音。我需要通过在变量中存储出现的次数来实现这一点。像这样:Python 巨蟒计数元音,python,python-3.x,counting,Python,Python 3.x,Counting,我已经开始了一个计算元音的程序,但似乎一无所获。我需要计算字符串中的元音,然后显示元音。我需要通过在变量中存储出现的次数来实现这一点。像这样: a = 0 b = 0 .... then print the lowest. 目前的代码没有那么多: string = str(input("please input a string: ")) edit= ''.join(string) print(edit) 我自己尝试了很多方法,但似乎都没有成功 >
a = 0
b = 0
....
then print the lowest.
目前的代码没有那么多:
string = str(input("please input a string: "))
edit= ''.join(string)
print(edit)
我自己尝试了很多方法,但似乎都没有成功
>>> a="hello how are you"
>>> vowel_count = dict.fromkeys('aeiou',0)
>>> vowel_count
{'a': 0, 'i': 0, 'e': 0, 'u': 0, 'o': 0}
>>> for x in 'aeiou':
... vowel_count[x]=a.count(x)
...
>>> vowel_count
{'a': 1, 'i': 0, 'e': 2, 'u': 1, 'o': 3}
现在,您可以从这里打印低nd最大值
现在,您可以从这里打印low nd max您可以使用字典解决此问题。对每个字符进行迭代,如果该字符是元音,则将其放入字典中,计数为0,并将其计数增加1,对于下一次出现的每个字符,继续增加计数
>>> string = str(input("please input a string: "))
please input a string: 'Hello how are you'
>>> dt={} # initialize dictionary
>>> for i in string: # iterate over each character
... if i in ['a','e','i','o','u']: # if vowel
... dt.setdefault(i,0) # at first occurrence set count to 0
... dt[i]+=1 # increment count by 1
...
>>> dt
{'a': 1, 'u': 1, 'e': 2, 'o': 3}
你可以用字典解决这个问题。对每个字符进行迭代,如果该字符是元音,则将其放入字典中,计数为0,并将其计数增加1,对于下一次出现的每个字符,继续增加计数
>>> string = str(input("please input a string: "))
please input a string: 'Hello how are you'
>>> dt={} # initialize dictionary
>>> for i in string: # iterate over each character
... if i in ['a','e','i','o','u']: # if vowel
... dt.setdefault(i,0) # at first occurrence set count to 0
... dt[i]+=1 # increment count by 1
...
>>> dt
{'a': 1, 'u': 1, 'e': 2, 'o': 3}
您可以使用字典理解:
>>> example = 'this is an example string'
>>> vowel_counts = {c: example.count(c) for c in 'aeoiu'}
>>> vowel_counts
{'i': 2, 'o': 0, 'e': 5, 'u': 0, 'a': 2}
那么,找到最小值、最大值等就很简单了。你可以使用字典理解:
>>> example = 'this is an example string'
>>> vowel_counts = {c: example.count(c) for c in 'aeoiu'}
>>> vowel_counts
{'i': 2, 'o': 0, 'e': 5, 'u': 0, 'a': 2}
word = input('Enter Your word : ')
vowel = 'aeiou'
vowel_counter = {}
for char in word:
if char in vowel:
vowel_counter[char] = vowel_counter.setdefault(char,0)+1
sorted_result = sorted(vowel_counter.items(), reverse=True,key=lambda x : x[1])
for key,val in sorted_result:
print(key,val)
那么,找到最小值、最大值等是很简单的。可能重复打印的最小值是什么?计数或基于计数的单词?打印最低计数可能的副本打印最低的是什么?计数或基于计数的单词?打印最低计数
word = input('Enter Your word : ')
vowel = 'aeiou'
vowel_counter = {}
for char in word:
if char in vowel:
vowel_counter[char] = vowel_counter.setdefault(char,0)+1
sorted_result = sorted(vowel_counter.items(), reverse=True,key=lambda x : x[1])
for key,val in sorted_result:
print(key,val)