Trying to assign a number to each letter within the string abc. Instead of getting a dictionary of {a : 0, b : 1, c : 2 etc} i get many new lines with only one key pair.
message = "54 396 131 198 225 258 87 258 128 211 57 235 114 258 144 220 39 175 330 338 297 288"
m2 = message.replace(" ", ",")
print(m2)
li = list(m2.split(","))
print(li)
res = []
#res = [eval(i) for i in li]
for i in li:
res.append(eval(i))
print(res)
giz = []
for i in res:
giz.append(i % 37)
print(giz)
#for i in f:
# print(int(math.fmod(i, 37)),end=" ")
abc = "abcdefghijklmnopqrstuvwxyz0123456789_"
d = {}
o = -1
for i in abc:
o = 1
d = {o : i}
print(d)
output
54,396,131,198,225,258,87,258,128,211,57,235,114,258,144,220,39,175,330,338,297,288
['54', '396', '131', '198', '225', '258', '87', '258', '128', '211', '57', '235', '114', '258', '144', '220', '39', '175', '330', '338', '297', '288']
[54, 396, 131, 198, 225, 258, 87, 258, 128, 211, 57, 235, 114, 258, 144, 220, 39, 175, 330, 338, 297, 288]
[17, 26, 20, 13, 3, 36, 13, 36, 17, 26, 20, 13, 3, 36, 33, 35, 2, 27, 34, 5, 1, 29]
{0: 'a'}
{1: 'b'}
{2: 'c'}
{3: 'd'}
{4: 'e'}
{5: 'f'}
{6: 'g'}
{7: 'h'}
{8: 'i'}
{9: 'j'}
{10: 'k'}
{11: 'l'}
{12: 'm'}
{13: 'n'}
{14: 'o'}
{15: 'p'}
{16: 'q'}
{17: 'r'}
{18: 's'}
{19: 't'}
{20: 'u'}
{21: 'v'}
{22: 'w'}
{23: 'x'}
{24: 'y'}
{25: 'z'}
{26: '0'}
{27: '1'}
{28: '2'}
{29: '3'}
{30: '4'}
{31: '5'}
{32: '6'}
{33: '7'}
{34: '8'}
{35: '9'}
{36: '_'}
I tried looking on google and everywhere and couldn't find the answer. Any advice on finding answers to obscure questions like this for the future?
CodePudding user response:
First, you are printing the dictionary in each iteration of the loop Second, you should use the update method because you are redefining the dictionary every iteration instead of adding to it. it should look like
d = {}
o = 0
for i in abc:
d.update({o : i})
o =1
print(d)
CodePudding user response:
If you want complete letters and digits as you have shown in your code:
string.digits
will be '0123456789'
string.ascii_lowercase
will be 'abcdefghijklmnopqrstuvwxyz
'
import string
d=dict(zip(range(37),string.ascii_lowercase string.digits '_'))
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z', 26: '0', 27: '1', 28: '2', 29: '3', 30: '4', 31: '5', 32: '6', 33: '7', 34: '8', 35: '9', 36: '_'}
CodePudding user response:
abc = "abcdefghijklmnopqrstuvwxyz0123456789_"
d = {}
o = 1
for i in abc:
d[i] = o
o =1
print(d)
CodePudding user response:
What you want is use dict.update({...}) function --> to answer your basic question. it writes to an existing dict without creating a new one.
To simplify the whole thing I would suggest using
message = "0 1 2 3 4 5 6 7 8 9 10"
resultingDict = dict([(int(c), chr(int(c) 97)) for c in message.split(' ')])
print (resultingDict)
to generate the output of
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j', 10: 'k'}
Good luck