简体   繁体   English

如何在python中创建元音计数器

[英]How to create vowel counter in python

Title says all! 标题说明了一切! I am trying to make a " Display Stats" option in my menu i created. 我试图在我创建的菜单中设置“显示统计信息”选项。 It would display the following after inputing a sentence. 输入句子后将显示以下内容。 For example: String Analysis: 6 words 26 Characters 9 Vowels 17 Consonants. 例如:字符串分析:6个单词26个字符9个元音17个辅音。 I made the whole word and character counter already now i need to do vowel and consonants, can someone help me please? 现在我已经做了整个单词和字符计数器,我需要做元音和辅音,有人可以帮我吗? Id highly appreciate the support! 我非常感谢您的支持!

What i got so far: 我到目前为止所得到的:

def displayst():
    print()
    print("You said the following:")
    time.sleep(1)
    length = str(input("Please enter your sentence: "))
    word = dis(length)
    lengths = diss(length)
    vowel = disv(length)
    print(length)
    time.sleep(1)
    print()
    print("String Analysis:",'\n', word, "Words",'\n', lengths, "Characters",'\n',vowel,"Vowels",'\n')
    again()

The vowel = disv(length) is what i need to finish and if you could help with consonants that would be great! 元音= disv(length)是我需要完成的,如果您可以帮助辅音,那就太好了! If not i mainly need the vowels done as consonants i guess i could give it a try haha. 如果不是,我主要需要做为辅音的元音,我想我可以试试看。

Then in my disv(length): 然后在我的DISV(长度):

vowels = 'aeiou'
count = {}.fromkeys(vowels,0)
for char in length:
   if char in count:
        count[char] += 1
print(count)

Its all crazy and i honestly havnt a clue where im going with this haha. 这全是疯了,老实说,我知道这个哈哈去向。 Please help cheers. 请帮助加油。

If you want a count, use a Counter dict: 如果要计数,请使用Counter dict:

from collections import Counter

inp = input("Please enter your sentence: ").lower()
cn = Counter(inp)
vowels = {v: cn[v] for v in "aeiou" if v in cn}
cons = {c: cn[c] for c in "bcdfghjklmnpqrstvwxyz" if c in cn}

if you want the total count just sum the values: 如果您想要总计数,只需对值求和:

 print(sum(vowels.values()))

 print(sum(cons.values()))

If you just want the sums: 如果您只想要总和:

vowels = sum(cn[v] for v in "aeiou")
cons = sum(cn[c] for c in  "bcdfghjklmnpqrstvwxy")

You are not returning anything from disv that's why it is saying vowels None.You can use counter to get the vowel count 您没有从DISV返回任何内容,这就是它说元音无的原因。您可以使用counter来获取元音计数

from collections import Counter

x = Counter(length)
vowel_count = 0
for v in "aeiou":
    vowel_count += x[v]
return vowel_count

This will return the exact number of vowels in your sentence. 这将返回句子中元音的确切数量。

def getInput():
    sentenceInput=input('Enter your sentence: ')
    return sentenceInput

def count(sentenceInput):
    vowelList=['a','e','i','o','u']
    vowelCount = 0
    constCount = 0
    spaceCount = 0
    for char in sentenceInput:
        if char in vowelList:
            vowelCount += 1
        elif char == ' ':
            spaceCount += 1
        else:
            constCount +=1
    print ('Length: ' + str(len(sentenceInput)) + ', Words: ' + str(spaceCount) + ', Vowels: ' + str(vowelCount) + ', Consonants: ' + str(constCount))

count(getInput())
vcount = sum([s.count(v) for v in 'aeiou'])

The list comprehension in side the [] returns a list of the occurrences of each vowel. []旁的列表推导返回每个元音的出现列表。 The outer sum sums up the counts to give a total vowel count. 外部总和将计数加起来得到总元音计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM