Python – Resolve Float Keys in Dictionary
Last Updated :
18 May, 2023
Given a dictionary with variety of floating-point keys, find a way to access them as single value.
Input : test_dict = {"010.78" : "Gfg", "9.0" : "is", "10" : "Best"}, K = "09.0"
Output : "is"
Explanation : 09.0 -> 9.0 whose value is "is".
Input : test_dict = {"010.78" : "Gfg", "9.0" : "is", "10" : "Best"}, K = "10.0"
Output : "Best"
Explanation : 10.0 -> 10 whose value is "Best".
Method #1 : Using float() + loop
- This is one of the ways to solve this problem. In this, the strategy used is to convert key into a float value using float()
It resolves to a single value, and performs check of the input string after conversion to float(), both resolve to a common float value.
Python3
test_dict = { "010.78" : "Gfg" , "9.0" : "is" , "10" : "Best" }
print ( "The original dictionary is : " + str (test_dict))
K = "10.78"
res = dict ()
for key in test_dict:
res[ float (key)] = test_dict[key]
convK = float (K)
res = res[convK]
print ( "Value of resolved float Key : " + str (res))
|
Output
The original dictionary is : {'010.78': 'Gfg', '9.0': 'is', '10': 'Best'}
Value of resolved float Key : Gfg
Method #2 : Using dictionary comprehension + float()
This computes in similar way as above method. The difference being for conversion one liner dictionary comprehension is employed.
Python3
test_dict = { "010.78" : "Gfg" , "9.0" : "is" , "10" : "Best" }
print ( "The original dictionary is : " + str (test_dict))
K = "10.78"
res = { float (key) : test_dict[key] for key in test_dict}
convK = float (K)
res = res[convK]
print ( "Value of resolved float Key : " + str (res))
|
Output
The original dictionary is : {'010.78': 'Gfg', '9.0': 'is', '10': 'Best'}
Value of resolved float Key : Gfg
Convert keys to float:
Approach:
Convert all keys in the dictionary to float using float() function.
Convert the search key to float.
Search for the key in the dictionary and return its value.
Python3
def resolve_float_keys(d, k):
d = { float (key): value for key, value in d.items()}
k = float (k)
return d.get(k)
test_dict = { "010.78" : "Gfg" , "9.0" : "is" , "10" : "Best" }
k = "09.0"
print (resolve_float_keys(test_dict, k))
k = "10.0"
print (resolve_float_keys(test_dict, k))
|
Time Complexity: O(n), where n is the number of keys in the dictionary.
Space Complexity: O(n), where n is the number of keys in the dictionary.
Method #4: Using map() and lambda function to resolve float keys
- Initialize the input dictionary test_dict with the given key-value pairs.
- Initialize the key K to be resolved as a float value to “10.78”.
- Using the map() function to create a new dictionary with the keys as the float values of the original dictionary keys.
- With usage of lambda function to convert each key to a float value.
- Assign the result to a variable res.
- Access the value corresponding to the key K in the res dictionary.
- Lastly, print the value of the resolved float key.
Python3
test_dict = { "010.78" : "Gfg" , "9.0" : "is" , "10" : "Best" }
print ( "The original dictionary is: " + str (test_dict))
K = "10.78"
res = dict ( map ( lambda x: ( float (x[ 0 ]), x[ 1 ]), test_dict.items()))
res = res[ float (K)]
print ( "Value of resolved float key: " + str (res))
|
Output
The original dictionary is: {'010.78': 'Gfg', '9.0': 'is', '10': 'Best'}
Value of resolved float key: Gfg
Time complexity: O(n log n), where n is the number of key-value pairs in the input dictionary. This is due to the sorting of the dictionary keys while converting them to float values.
Auxiliary space: O(n), where n is the number of key-value pairs in the input dictionary. This is the space required to store the new dictionary with float keys
Method 5 : using a list comprehension and the float() function
Python3
test_dict = { "010.78" : "Gfg" , "9.0" : "is" , "10" : "Best" }
print ( "The original dictionary is: " + str (test_dict))
K = "10.78"
res = { float (key): value for key, value in test_dict.items()}
res = res[ float (K)]
print ( "Value of resolved float key: " + str (res))
|
Output
The original dictionary is: {'010.78': 'Gfg', '9.0': 'is', '10': 'Best'}
Value of resolved float key: Gfg
The time complexity of this method is O(n), where n is the number of key-value pairs in the dictionary.
The auxiliary space complexity is O(n) as well because the new dictionary is created, which can store up to n key-value pairs.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...