Is there a one liner code for converting the following list/string to map, basically counting all chars and storing in map.
lst = 'leet'
map ={'l':1,'e':2,'t':1}
what i did so far
for i in lst:
if i not in map:
map[i] = 1
else:
map[i] =1
CodePudding user response:
Try
>>> from collections import Counter
>>> Counter("leet")
Counter({'e': 2, 'l': 1, 't': 1})
Source: https://realpython.com/python-counter/
CodePudding user response:
from collections import Counter; c = Counter(lst)
since you said must be one-liner :)
CodePudding user response:
{letter: "leet".count(letter) for letter in "leet"}
might work