Find Count Of Characters Within The String In Python
Solution 1:
As others have said, this is best done with a Counter.
You can also do:
>>> {e:str1.count(e) for e inset(str1)}
{'a': 4, 'b': 3}
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.
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.
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!
Solution 2:
Ideal way to do this is via using collections.Counter
:
>>>from collections import Counter>>>str1 = "aabbaba">>>Counter(str1)
Counter({'a': 4, 'b': 3})
You can not achieve this via simple dict comprehension expression as you will require reference to your previous value of count of element. As mentioned in Dawg's answer, as a work around you may use list.count(e)
in order to find count of each element from the set
of string within you dict comprehension expression. But time complexity will be n*m
as it will traverse the complete string for each unique element (where m are uniques elements), where as with counter it will be n
.
Solution 3:
This is a nice case for collections.Counter
:
>>> from collections import Counter
>>> Counter(str1)
Counter({'a': 4, 'b': 3})
It's dict subclass so you can work with the object similarly to standard dictionary:
>>>c = Counter(str1)>>>c['a']
4
You can do this without use of Counter class as well. The simple and efficient python code for this would be:
>>>d = {}>>>for x in str1:... d[x] = d.get(x, 0) + 1...>>>d
{'a': 4, 'b': 3}
Solution 4:
Note that this is not the correct way to do it since it won't count repeated characters more than once (apart from losing other characters from the original dict) but this answers the original question of whether if-else is possible in comprehensions and demonstrates how it can be done.
To answer your question, yes it's possible but the approach is like this:
dic = {x: (dic[x] + 1 if x in dic else 1) for x in str1}
The condition is applied on the value only not on the key:value mapping.
The above can be made clearer using dict.get
:
dic = {x: dic.get(x, 0) + 1 for x in str1}
0 is returned if x
is not in dic
.
Demo:
In [78]: s = "abcde"
In [79]: dic = {}
In [80]: dic = {x: (dic[x] + 1if x in dic else1) forxin s}
In [81]: dic
Out[81]: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1}
In [82]: s = "abfg"
In [83]: dic = {x: dic.get(x, 0) + 1forxin s}
In [84]: dic
Out[84]: {'a': 2, 'b': 2, 'f': 1, 'g': 1}
Post a Comment for "Find Count Of Characters Within The String In Python"