Python – Subscript Dictionary
Last Updated :
11 Apr, 2023
Sometimes, while working with data in Python, we can have a problem in which we need to use subscripted version of numbers rather than normal ones. For this, having a dictionary which maps the number with its subscript version has good utility. Let’s discuss certain ways in which this task can be performed.
Input : test_str = “012345” Output : {‘0’: ‘?’, ‘1’: ‘?’, ‘2’: ‘?’, ‘3’: ‘?’, ‘4’: ‘?’, ‘5’: ‘?’} Input : test_str = “0” Output : {‘0’: ‘?’}
Method #1 : Using loop + ord() This is brute force way in which we perform this task. In this, we iterate through the numbers that we require to subscript and construct subscript value using ord() and its binary value. Works in Python 3.7 +.
Python3
test_str = " 0123456789 "
print ("The original string is : " + test_str)
K = u '\u2080'
res = {}
for ele in test_str:
res[ele] = K
K = chr ( ord (K) + 1 )
print ("The split string is : " + str (res))
|
Output :
The original string is : 0123456789 The split string is : {‘7’: ‘?’, ‘4’: ‘?’, ‘2’: ‘?’, ‘3’: ‘?’, ‘5’: ‘?’, ‘8’: ‘?’, ‘1’: ‘?’, ‘6’: ‘?’, ‘0’: ‘?’, ‘9’: ‘?’}
Method #2 : Using dictionary comprehension This is yet another way in which this task can be performed. In this, we perform a similar task as above, just employ in one-liner using comprehension. Works in Python 3.7 +.
Python3
test_str = " 0123456789 "
print ("The original string is : " + test_str)
K = u '\u2080'
res = {ele : chr ( ord (K) + 1 ) for ele in test_str}
print ("The split string is : " + str (res))
|
Output :
The original string is : 0123456789 The split string is : {‘7’: ‘?’, ‘4’: ‘?’, ‘2’: ‘?’, ‘3’: ‘?’, ‘5’: ‘?’, ‘8’: ‘?’, ‘1’: ‘?’, ‘6’: ‘?’, ‘0’: ‘?’, ‘9’: ‘?’}
Method #3 : Using dict.fromkeys()
Approach
we uses the dict.fromkeys() method to create a new dictionary with all the keys in the input string set to the same default value. The dict.fromkeys() method takes two arguments. The first argument is an iterable, which in this case is the input string test_str. The second argument is the default value to be set for all the keys in the resulting dictionary, which is ‘?’ in this case. The method creates a new dictionary with all the keys from the iterable set to the default value. Since the test_str is the iterable in this case, the resulting dictionary will have all the characters in test_str as keys with ‘?’ as their value. resulting dictionary is returned.
Algorithm
1. Initialize an empty dictionary, say “result”.
2. Call the “dict.fromkeys()” method with the input string, “test_str” and “?” as arguments.
3. Return the resulting dictionary.
Python3
def subscript_dict_fromkeys(test_str):
result = dict .fromkeys(test_str, '?' )
return result
test_str = '012345'
print (subscript_dict_fromkeys(test_str))
|
Output
{'0': '?', '1': '?', '2': '?', '3': '?', '4': '?', '5': '?'}
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(n), where n is the length of the input string.
METHOD 4:Using def
APPROACH:
The approach used in this code is to iterate over each character in the input string and create a dictionary with the character as the key and a default value of ‘?’.
ALGORITHM:
1.Define a function subscript_dict that takes a string as input.
2.Create an empty dictionary result.
3.Iterate over each character char in the string.
4.Add an entry to result with the key char and the value ‘?’.
5.Return the result dictionary.
Python3
def subscript_dict(string):
result = {}
for char in string:
result[char] = '?'
return result
string = '0123456789'
result = subscript_dict(string)
print (result)
|
Output
{'0': '?', '1': '?', '2': '?', '3': '?', '4': '?', '5': '?', '6': '?', '7': '?', '8': '?', '9': '?'}
Time complexity: O(n) where n is the length of the input string.
Auxiliary Space: O(n) where n is the length of the input string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...