Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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 count()类型错误_Python_Python 3.x - Fatal编程技术网

传递字符类型编号时出现Python count()类型错误

传递字符类型编号时出现Python count()类型错误,python,python-3.x,Python,Python 3.x,我正试图以这种方式计算字符串中的字符数: randomString = 'a-sasdqwe3123d234wdd213dsad12da-sd-sa' for number in range(256): print(randomString.encode('utf-8').count(chr(number))) 据我所知,例如,char(55)应该给出一个7和char(85)一个字母U,这两个字母都是count()函数的有效输入 上面的代码段返回 TypeError:参数应该是整数或类

我正试图以这种方式计算字符串中的字符数:

randomString = 'a-sasdqwe3123d234wdd213dsad12da-sd-sa'
for number in range(256):
    print(randomString.encode('utf-8').count(chr(number)))
据我所知,例如,
char(55)
应该给出一个
7
char(85)
一个字母
U
,这两个字母都是
count()
函数的有效输入

上面的代码段返回

TypeError:参数应该是整数或类似对象的字节,而不是'str'


知道会发生什么吗?

编码字符串是一个类似字节的对象,这意味着它只能计算整数,不能计算字符串。去掉chr()

randomString='a-sasdqwe3123d234wdd213dsad12da-sd-sa'
对于范围内的数字(256):
打印(随机字符串。编码('utf-8')。计数(数字))
或者,不要对其进行编码:

randomString='a-sasdqwe3123d234wdd213dsad12da-sd-sa'
对于范围内的数字(256):
打印(randomString.count(chr(number)))
删除
.encode('utf-8')