Read characters in a string from a character list

I am trying to write a function count(s, chars)that takes a string sand a list of characters chars. The function should count the number of occurrences of the letters given in chars. It should return a dictionary in which keys are the characters specified in the character list chars.

So for example:

In [1]: s = "Another test string with x and y but no capital h."
In [2]: count(s, ['A', 'a', 'z'])
Out[2]: 'A': 1, 'a': 3, 'z': 0

I made some code that can read all the characters of a string and return a dictionary:

return {i: s.count(i) for i in set(s)}

but I'm not sure how you would use a list of specific characters and return a dictionary ...

+4
source share
6 answers

What about:

def count_chars(s,chars):
    return {c : s.count(c) for c in chars}

Forms:

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "Another test string with x and y but no capital h."
>>> def count_chars(s,chars):
...     return {c : s.count(c) for c in chars}
... 
>>> count_chars(s, ['A', 'a', 'z'])
{'z': 0, 'A': 1, 'a': 3}

. , . Counter , :

from collections import Counter

def count_chars(s,chars):
    counter = Counter(s)
    return {c : counter.get(c,0) for c in chars}
+4

" ", dict fromkeys, :

li=['A', 'a', 'z']
s = "Another test string with x and y but no capital h."

def count(s, li):
    cnt={}.fromkeys(li, 0)
    for c in s:
        if c in cnt:
            cnt[c]=cnt[c]+1
    return cnt

>>> count(s, li)
{'A': 1, 'a': 3, 'z': 0}

, , , :

def count(s, li):
    cnt={}.fromkeys(li, 0)
    for c in (e for e in s if e in cnt):
        cnt[c]+=1
    return cnt

, Pythonic - :

>>> from collections import Counter
>>> c=Counter(s)
>>> c
Counter({' ': 10, 't': 7, 'n': 4, 'h': 3, 'i': 3, 'a': 3, 'o': 2, 'e': 2, 'r': 2, 's': 2, 'A': 1, 'g': 1, 'w': 1, 'x': 1, 'd': 1, 'y': 1, 'b': 1, 'u': 1, 'c': 1, 'p': 1, 'l': 1, '.': 1})

dict :

>>> {k:c[k] for k in li}
{'A': 1, 'a': 3, 'z': 0}
+2
str.count(sub[, start[, end]])

sub [, ]. start end .

.

>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4

.

li=['A', 'a', 'z']
s = "Another test string with x and y but no capital h."

def count(s, li):
    cnt={}.fromkeys(li, 0)
    for c in li:
      cnt[c] = s.count(c);
    return cnt

   count(s, li)
=> {'a': 3, 'A': 1, 'z': 0}
+2
 def count(s, chars):
    ret = dict(zip(chars, [0 for c in chars]))
    for c in s:
        if ret.has_key(c):
            ret[c] += 1
    return ret

Something like this is possible.

+1
source

You can also create a dictionary using the built-in method zip:

>>> s
'Another test string with x and y but no capital h.'
>>> c
['A', 'a', 'z']
>>> def count_char(s, c):
       counts = map(s.count, c)
       return dict(zip(c, counts))

>>> 
>>> count_char(s, c)
{'z': 0, 'A': 1, 'a': 3}
+1
source

Well, there are so many answers, I will also invest in mine based on inline constructs:

from collections import Counter

s = "Another test string with x and y but no capital h."
chars = ['A', 'a', 'z']

count = Counter(s)  # creates a dictionary
count = {k:v for k, v in count.items() if k in chars}  # take only charatcters from chars
count.update({k:0 for k in set(chars) - set(s)})  # add zero instances
print(count)

===
{'a': 3, 'A': 1, 'z': 0}
+1
source

Source: https://habr.com/ru/post/1666847/


All Articles