第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

返回具有給定元音數(shù)量的列表中的單詞數(shù)

返回具有給定元音數(shù)量的列表中的單詞數(shù)

慕無忌1623718 2021-11-09 16:21:48
有沒有辦法編輯這個程序,以便它返回列表中具有給定元音數(shù)量的單詞數(shù)?我試過了,但似乎無法返回正確的數(shù)字,而且我不知道我的代碼輸出的是什么。(我是初學者)def getNumWordsWithNVowels(wordList, num):totwrd=0x=0ndx=0while ndx<len(wordList):    for i in wordList[ndx]:        if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U'):            x+=1        if x==num:            totwrd+=1        ndx+=1return totwrd打印(getNumWordsWithNVowels(aList,2))這輸出“2”,但它應(yīng)該輸出“5”。
查看完整描述

1 回答

?
郎朗坤

TA貢獻1921條經(jīng)驗 獲得超9個贊

您可以將該sum函數(shù)與生成器表達式一起使用:


def getNumWordsWithNVowels(wordList, num):

    return sum(1 for w in wordList if sum(c in 'aeiou' for c in w.lower()) == num)

以便:


aList = ['hello', 'aloha', 'world', 'foo', 'bar']

print(getNumWordsWithNVowels(aList, 1))

print(getNumWordsWithNVowels(aList, 2))

print(getNumWordsWithNVowels(aList, 3))

輸出:


2 # world, bar

2 # hello, foo

1 # aloha


查看完整回答
反對 回復(fù) 2021-11-09
  • 1 回答
  • 0 關(guān)注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號