Here is the letters:
letters=\'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\'
I made a list of it with this:
<
I think you mean this:
letters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
# dict comprehension
dd = { x:0 for x in list(letters)}; dd
{'A': 0,
'B': 0,
'C': 0,
'D': 0,
'E': 0,
'F': 0,
'G': 0,
'H': 0,
'I': 0,
'J': 0,
'K': 0,
'L': 0,
'M': 0,
'N': 0,
'O': 0,
'P': 0,
'Q': 0,
'R': 0,
'S': 0,
'T': 0,
'U': 0,
'V': 0,
'W': 0,
'X': 0,
'Y': 0,
'Z': 0,
'a': 0,
'b': 0,
'c': 0,
'd': 0,
'e': 0,
'f': 0,
'g': 0,
'h': 0,
'i': 0,
'j': 0,
'k': 0,
'l': 0,
'm': 0,
'n': 0,
'o': 0,
'p': 0,
'q': 0,
'r': 0,
's': 0,
't': 0,
'u': 0,
'v': 0,
'w': 0,
'x': 0,
'y': 0,
'z': 0,
'0': 0,
'1': 0,
'2': 0,
'3': 0,
'4': 0,
'5': 0,
'6': 0,
'7': 0,
'8': 0,
'9': 0}
update:
dd['A'] = 13
dd
dd{'A': 13,
'B': 0,
'C': 0,
'D': 0,
'E': 0,
'F': 0,
'G': 0,
'H': 0,
'I': 0,
Or,
list(letters)
['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',
'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',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9']
You can use either a list of tuples or a dict. A simple solution to do it:
>>> import string
>>> letters = string.ascii_uppercase + string.ascii_lowercase + string.digits
>>> chars = dict.fromkeys(letters , 0)
>>> chars
>>> {...'a': 0, 'b': 0 ....}
To use list of tuples:
>>> list(chars.items())
>>> [...('a',0), ('b', 0)...]
If you want to create 0
-list with the length of string letters
.
letters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
chars = [0 for _ in range(len(letters))]
An alternative to list
comprehensions is to use map
In [841]: letters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
In [842]: chars = list(map(lambda l: 0, letters))
Or if you want a dict
like the other answers are suggesting
In [844]: dict(map(lambda l: (l, 0), letters))
I generally find list
/dict
comprehensions to both be faster and more readable (to me at least). This is just a different approach
So, in short, what you want is a dictionary (mapping) of character -> 0
for each character in the input.
This is the way to do it:
letters='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
chars = {char: 0 for char in letters}
The problem with the original code was that there, chars
was a list
(because it was created as a list here: chars=[]
), and characters were used as its indices.
So, the first time chars[i]=0;
was executed (BTW, ;
is not needed here), i
was 'A'
and chars['A']=0
produces the error.