Reputation: 101
I have a dictionary 'g' and I want to convert all letters to numbers.
g = { "a" : ["c","e","h","m","k","i"],
"b" : ["d","f","k","m"]
}
I found this on stack overflow:
def alphabet_position_Headcrab(text):
nums = [str(ord(x) - 96) for x in text.lower() if x >= 'a' and x <= 'z']
return " ".join(nums)
and this one with a prefer:
def alphabet_position_wvxvw(text):
result, i = [32, 32, 32] * len(text), 0
for c in bytes(text.lower(), 'ascii'):
if 97 <= c < 106:
result[i] = c - 48
i += 2
elif 106 <= c < 116:
result[i] = 49
result[i + 1] = c - 58
i += 3
elif 116 <= c <= 122:
result[i] = 50
result[i + 1] = c - 68
i += 3
return bytes(result[:i-1])
But it works not for my dictionary but only for 1Dimensional dictionary like:
dic = { "a" : "g", "b" : "f"}
Thank you for your help ( perhaps is the answer obvious but I'm not an expert in coding)
Upvotes: 1
Views: 1458
Reputation: 158
This might help you
for key, value in g.items():
nums = [str(ord(x) - 96) for x in value if x.lower() >= 'a' and x.lower() <= 'z']
g[key] = nums
Upvotes: 1
Reputation: 191844
If the functions work for single values, then it should be possible for you to map the function over your list values
new_g = {k: [alphabet_position(x) for x in v] for k, v in g.items()}
Upvotes: 1