Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;ord()需要一个字符,但字符串长度为15…”;_Python_Python 3.x - Fatal编程技术网

Python &引用;ord()需要一个字符,但字符串长度为15…”;

Python &引用;ord()需要一个字符,但字符串长度为15…”;,python,python-3.x,Python,Python 3.x,我需要知道每个字母在一个句子中出现了多少次。但是,它给出了一个错误“ord()需要一个字符,但字符串长度为15”(15和我当前文件中包含的字母数量) texto=open('teste.txt','r') Ocorrecias=[0]*25 ord_a=ord(“a”) 对于texto中的字符: 如果字符>='a'和字符texto=open('teste.txt','r') Ocorrecias=[0]*26 ord_a=ord(“a”) 对于texto中的字符: 对于字符中的c: 如果c>='

我需要知道每个字母在一个句子中出现了多少次。但是,它给出了一个错误“ord()需要一个字符,但字符串长度为15”(15和我当前文件中包含的字母数量)

texto=open('teste.txt','r')
Ocorrecias=[0]*25
ord_a=ord(“a”)
对于texto中的字符:
如果字符>='a'和字符
texto=open('teste.txt','r')
Ocorrecias=[0]*26
ord_a=ord(“a”)
对于texto中的字符:
对于字符中的c:

如果c>='a'和c,您可以通过在单词上使用额外的循环来实现它

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
texto=open('teste.txt','r')
Ocorrecias=[0]*26
ord_a=ord(“a”)
对于texto中的单词:
对于文字中的字符:

如果字符>='a'和字符,您可以使用
itertools
通过一个小的更改来调整代码。文件迭代器生成文本行,您希望每个字符都在一行中

chain
类用于通过将多个iterable链接在一起来创建新的iterable;它从一个iterable生成项目,然后从下一个iterable生成项目,直到所有原始iterable都用完

由于
texto
本身是可编辑的(生成
str
对象),因此可以链接这些值 组合成一个长的“虚拟”
str
,生成所需的单个字符

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
来自itertools导入链的

texto=open('teste.txt','r')
ocorrecias=[0]*26#即使索引为0到25,您仍然需要26个插槽
ord_a=ord(“a”)
对于链中的字符。从\u iterable(texto):

如果字符>='a'且您试图对一个单词进行排序,请为循环添加另一个字符。(打印要调试的字符)文件迭代器生成行,而不是单个字符/字节。现在检查,我已经更新了列表的大小。
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
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