I've got a list with characters
chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-']
Then a Counter object
from collections import Counter
s = 'testing'
counter = Counter(s)
print(counter)
>>> Counter({'t': 2, 'e': 1, 's': 1, 'i': 1, 'n': 1, 'g': 1})
How do I get a list from this Counter
object that would have the same order and length as the chars
list? The output list would contain integers so if the string did not contain the character there would be a 0
.
You can use the following list comprehension:
>>> [counter.get(c, 0) for c in chars]
[0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0]