Python – Assign Alphabet to each element
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
# Python3 code to demonstrate working of # Assign Alphabet to each element # Using ascii_lowercase() + loop + list comprehension import string # initializing list test_list = [ 4 , 5 , 2 , 4 , 2 , 6 , 5 , 2 , 5 ] # printing list print ( "The original list : " + str (test_list)) temp = {} cntr = 0 for ele in test_list: if ele in temp: continue # assigning same Alphabet to same element temp[ele] = string.ascii_lowercase[cntr] cntr + = 1 # flattening res = [temp.get(ele) for ele in test_list] # printing results print ( "The mapped List : " + str (res)) |
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
# Python3 code to demonstrate working of # Assign Alphabet to each element # Using defaultdict() + ascii_lowercase() + iter() from collections import defaultdict import string # initializing list test_list = [ 4 , 5 , 2 , 4 , 2 , 6 , 5 , 2 , 5 ] # printing list print ( "The original list : " + str (test_list)) # assigning lowercases as iterator temp = iter (string.ascii_lowercase) # lambda functions fits to similar elements res = defaultdict( lambda : next (temp)) # flatten in list res = [res[key] for key in test_list] # printing results print ( "The mapped List : " + str ( list (res))) |
The original list : [4, 5, 2, 4, 2, 6, 5, 2, 5] The mapped List : ['a', 'b', 'c', 'a', 'c', 'd', 'b', 'c', 'b']