I am noobie. I am trying to assign values from A to Z. This is very long.
A= 97 B= 98 C= 99 D= 100 ... Z= 122What is the best way to do it?
asked Aug 31, 2017 at 15:31
9
You can do it like that:
import string alpha_dict = {k: ord(k) for k in string.ascii_lowercase} print(alpha_dict) # {'r': 114, 'l': 108, 'z': 122, ...}And access your variables like alpha_dict['a'].
Note that the values you have in your example correspond to the lowercase letters but you are naming your variables with uppercase. If you actually want the uppercase, instead of looping through ascii_lowercase loop over ascii_uppercase
answered Aug 31, 2017 at 15:36
Ma0Ma0
14.8k3 gold badges33 silver badges62 bronze badges
Instead of hard-coding every letter you can use ord('letter'). For example ord('a') will return the number 97 which stands for the letter a in ASCII.
EDIT:
I used the code of Ev. Kounis for producing the dictionary because mine was wrong. If there's any problem I can delete it.
import string alpha_dict = {k: ord(k) for k in string.ascii_uppercase} '''Iterates in keys of alpha_dict, turning the key to lowercase, removes the old key and replaces it with the new lowercase one''' for key in alpha_dict: lower = key.lower() alpha_dict[lower] = alpha_dict.pop(key) print(alpha_dict)answered Aug 31, 2017 at 15:36
vmankvmank
6251 gold badge6 silver badges18 bronze badges
2
Given a list of elements, assign similar alphabet to same element.
Input : test_list = [4, 5, 2, 4, 2, 6]
Output : [‘a’, ‘b’, ‘c’, ‘a’, ‘c’, ‘d’]
Explanation : Alphabets assigned to elements as occurring.Input : test_list = [4, 5, 2, 4, 2, 6]
Output : [‘a’, ‘b’, ‘c’, ‘a’, ‘c’]
Explanation : Alphabets assigned to elements as occurring.
Method #1 : Using ascii_lowercase() + loop + list comprehension
In this, we extract all lowercase alphabets using lowercase(), and create dictionary mapping same element to similar character, post that we flatten that to appropriate index using list comprehension.
Python3
import string
test_list = [4, 5, 2, 4, 2, 6, 5, 2, 5]
print("The original list : " + str(test_list))
temp = {}
cntr = 0
for ele in test_list:
if ele in temp:
continue
temp[ele] = string.ascii_lowercase[cntr]
cntr += 1
res = [temp.get(ele) for ele in test_list]
print("The mapped List : " + str(res))
Output
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']Method #2 : Using defaultdict() + ascii_lowercase() + iter()
In this we use defaultdict() to assign values to similar elements, ascii_lowercase() is used to get all lowercase all lowercased alphabets.
Python3
from collections import defaultdict
import string
test_list = [4, 5, 2, 4, 2, 6, 5, 2, 5]
print("The original list : " + str(test_list))
temp = iter(string.ascii_lowercase)
res = defaultdict(lambda: next(temp))
res = [res[key] for key in test_list]
print("The mapped List : " + str(list(res)))
Output
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']The Time and Space Complexity for all the methods are the same:
Time Complexity: O(N)
Auxiliary Space: O(N)