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
String 在Python 3中计算字符串中唯一字母的出现次数_String_Python 3.x - Fatal编程技术网

String 在Python 3中计算字符串中唯一字母的出现次数

String 在Python 3中计算字符串中唯一字母的出现次数,string,python-3.x,String,Python 3.x,在Python3中,有许多方法可以计算字符串中唯一字母的出现次数。我只是想知道做这份工作的最佳方式是什么。 谢谢。试试: ''.join(set('aaabbbcccdddd')) 或: 另请参阅帖子:如果需要计算字符串中的字符数,请尝试以下操作 a = "aaabbcccd" b = dict.fromkeys(a, 0) for i in a: b[i] += 1 b现在保存您需要的计数: {'a': 3, 'c': 3, 'b': 2, 'd': 1} 欢迎来到堆栈溢出。如果

在Python3中,有许多方法可以计算字符串中唯一字母的出现次数。我只是想知道做这份工作的最佳方式是什么。 谢谢。

试试:

''.join(set('aaabbbcccdddd'))
或:


另请参阅帖子:

如果需要计算字符串中的字符数,请尝试以下操作

a = "aaabbcccd"
b = dict.fromkeys(a, 0)
for i in a:
    b[i] += 1
b现在保存您需要的计数:

{'a': 3, 'c': 3, 'b': 2, 'd': 1}

欢迎来到堆栈溢出。如果您知道计算唯一字母出现次数的多种方法,那么您应该能够编写一个比较它们的基准,以确定哪种方法最好。这不是家庭作业完成服务。请花点时间阅读这些-,我不是那个意思。imeant:aabbcda a 3 b 2 c 1 d 1
{'a': 3, 'c': 3, 'b': 2, 'd': 1}