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

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

“ord() 需要一個字符,但是長度為 15 的字符串......”

“ord() 需要一個字符,但是長度為 15 的字符串......”

BIG陽 2022-01-05 10:42:37
我需要知道每個字母在一個句子中出現(xiàn)多少次。但是,它給出了一個錯誤“ord () 需要一個字符,但字符串長度為 15”(15 和我當前文件中包含的字母數(shù)量)texto = open('teste.txt','r')ocorrencias = [0] * 25 ord_a = ord("a")for caracter in texto:    if caracter >= 'a' and caracter <= 'z':        ocorrencias[ord(caracter) - ord_a] += 1
查看完整描述

3 回答

?
阿晨1998

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

您可以使用itertools一個小的更改來調(diào)整您的代碼。文件迭代器生成文本行,并且您希望一行中的每個字符。


的chain類用于通過鏈接在一起的多個iterables創(chuàng)建一個新的迭代; 它從一個迭代中產(chǎn)生項目,然后從下一個迭代中產(chǎn)生項目,直到所有原始迭代都用完為止。


由于texto它本身是可迭代的(產(chǎn)生str對象),您可以將這些值鏈接到一個長的“虛擬”中str,從而產(chǎn)生您想要的單個字符。


from itertools import chain


texto = open('teste.txt','r')

ocorrencias = [0] * 26  # You still need 26 slots, even if they are indexed 0 to 25


ord_a = ord("a")


for caracter in chain.from_iterable(texto):

    if caracter >= 'a' and caracter <= 'z':

        ocorrencias[ord(caracter) - ord_a] += 1


查看完整回答
反對 回復(fù) 2022-01-05
?
浮云間

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

texto = open('teste.txt','r')

ocorrencias = [0] * 26


ord_a = ord("a")


for caracter in texto:

    for c in caracter:

        if c >= 'a' and c <= 'z':

            ocorrencias[ord(c) - ord_a] += 1

print(ocorrencias)

texto.close()


查看完整回答
反對 回復(fù) 2022-01-05
?
慕田峪4524236

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

您可以通過在單詞上使用附加循環(huán)來實現(xiàn)它


texto = open('teste.txt','r')

ocorrencias = [0] * 26 


ord_a = ord("a")


for words in texto:

    for character in words:

        if character >= 'a' and character <= 'z':

            ocorrencias[ord(character) - ord_a] += 1


查看完整回答
反對 回復(fù) 2022-01-05
  • 3 回答
  • 0 關(guān)注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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