在Python中查找字符串中的字符数 [英] Find count of characters within the string in Python

查看:88
本文介绍了在Python中查找字符串中的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个单词字典以及它在字符串中重复的次数.假设字符串是否如下所示

I am trying to create a dictionary of word and number of times it is repeating in string. Say suppose if string is like below

str1 = "aabbaba"

我想创建一个这样的字典

I want to create a dictionary like this

word_count = {'a':4,'b':3}

我正在尝试使用字典理解来做到这一点.我做了

I am trying to use dictionary comprehension to do this. I did

dic = {x:dic[x]+1 if x in dic.keys() else x:1 for x in str}

这最终给出了一个错误提示

This ends up giving an error saying

  File "<stdin>", line 1
    dic = {x:dic[x]+1 if x in dic.keys() else x:1 for x in str}
                                               ^
SyntaxError: invalid syntax

有人可以告诉我语法有什么问题吗?另外,如何使用字典理解功能创建这样的字典?

Can anybody tell me what's wrong with the syntax? Also,How can I create such a dictionary using dictionary comprehension?

推荐答案

正如其他人所说,最好使用Counter来完成.

As others have said, this is best done with a Counter.

您也可以这样做:

>>> {e:str1.count(e) for e in set(str1)}
{'a': 4, 'b': 3}

但是,对于每个唯一字符,该字符串将遍历字符串1 + n次(一次创建该集合,然后对每个唯一字母一次遍历该字符串,以计算其出现的次数.即,这具有二次运行时复杂性.).如果长字符串中包含许多唯一字符,那么结果很糟糕...计数器只会遍历字符串一次.

But that traverses the string 1+n times for each unique character (once to create the set, and once for each unique letter to count the number of times it appears. i.e., This has quadratic runtime complexity.). Bad result if you have a lot of unique characters in a long string... A Counter only traverses the string once.

如果您不希望使用比使用 .count 更有效的导入版本,则可以使用 .setdefault 进行计数:

If you want no import version that is more efficient than using .count, you can use .setdefault to make a counter:

>>> count={}
>>> for c in str1:
...    count[c]=count.setdefault(c, 0)+1
... 
>>> count
{'a': 4, 'b': 3}

无论字符串有多长或多少个唯一字符,该字符串仅遍历一次.

That only traverses the string once no matter how long or how many unique characters.

如果愿意,还可以使用 defaultdict :

You can also use defaultdict if you prefer:

>>> from collections import defaultdict
>>> count=defaultdict(int)
>>> for c in str1:
...    count[c]+=1
... 
>>> count
defaultdict(<type 'int'>, {'a': 4, 'b': 3})
>>> dict(count)
{'a': 4, 'b': 3}

但是,如果您要导入集合,请使用计数器!

But if you are going to import collections -- Use a Counter!

这篇关于在Python中查找字符串中的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆