In a dictionary like {'a': 0,'b': 0,'c': 0 ...'y': 0,'z': 0},
I want to count each character in the list as a dictionary value.
(For example, in the list of ["red", "blue", "yellow"], there is one e for red and one for yellow, so use'y': 2)
Is it possible to do something like this?

Corresponding source code
  • Answer # 1

    It means creating a dictionary that counts the number of alphabetic characters that appear in each word.

    import string
    dic = {ch: 0 for ch in string.ascii_lowercase}
    # print (dic)
    target = ["red", "blue", "yellow"]
    for word in target:
        for ch in word:
            if ch in string.ascii_lowercase:
                dic [ch] + = 1
    print (dic)
    {'a': 0,'b': 1,'c': 0,'d': 1,'e': 3,'f': 0,'g': 0,'h': 0,' i': 0,'j': 0,'k': 0,'l': 3,'m': 0,'n': 0,'o': 1,'p': 0,'q' : 0,'r': 1,'s': 0,'t': 0,'u': 1,'v': 0,'w': 1,'x': 0,'y': 1 ,'z': 0}