Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Resolve Float Keys in Dictionary

Python – Resolve Float Keys in Dictionary

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




# Python3 code to demonstrate working of
# Resolve Float Keys in Dictionary
# Using float() + loop()
 
# initializing dictionary
test_dict = {"010.78" : "Gfg", "9.0" : "is", "10" : "Best"}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = "10.78"
 
# performing resolution
res = dict()
for key in test_dict:
    res[float(key)] = test_dict[key]
 
# converting compare value to float
convK = float(K)
 
# performing value access
res = res[convK]
 
# printing result
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




# Python3 code to demonstrate working of
# Resolve Float Keys in Dictionary
# Using dictionary comprehension + float()
 
# initializing dictionary
test_dict = {"010.78" : "Gfg", "9.0" : "is", "10" : "Best"}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = "10.78"
 
# performing resolution using dictionary comprehension
res = {float(key) : test_dict[key] for key in test_dict}
 
# converting compare value to float
convK = float(K)
 
# performing value access
res = res[convK]
 
# printing result
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)
 
# Example usage
test_dict = {"010.78": "Gfg", "9.0": "is", "10": "Best"}
k = "09.0"
print(resolve_float_keys(test_dict, k)) # Output: is
k = "10.0"
print(resolve_float_keys(test_dict, k)) # Output: Best


Output

is
Best

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

  1. Initialize the input dictionary test_dict with the given key-value pairs.
  2. Initialize the key K to be resolved as a float value to “10.78”.
  3. Using the map() function to create a new dictionary with the keys as the float values of the original dictionary keys.
  4. With usage of lambda function to convert each key to a float value.
  5. Assign the result to a variable res.
  6. Access the value corresponding to the key K in the res dictionary.
  7. Lastly, print the value of the resolved float key.

Python3




# initializing dictionary
test_dict = {"010.78" : "Gfg", "9.0" : "is", "10" : "Best"}
 
# printing original dictionary
print("The original dictionary is: " + str(test_dict))
 
# initializing key to be resolved
K = "10.78"
 
# creating a new dictionary with float keys using map() and lambda function
res = dict(map(lambda x: (float(x[0]), x[1]), test_dict.items()))
 
# accessing value corresponding to the key K
res = res[float(K)]
 
# printing result
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




# Initializing dictionary
test_dict = {"010.78": "Gfg", "9.0": "is", "10": "Best"}
 
# Printing original dictionary
print("The original dictionary is: " + str(test_dict))
 
# Initializing key to be resolved
K = "10.78"
 
# Creating a new dictionary with float keys using list comprehension and float() function
res = {float(key): value for key, value in test_dict.items()}
 
# Accessing value corresponding to the key K
res = res[float(K)]
 
# Printing result
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.

RELATED ARTICLES

Most Popular

Recent Comments