'Fill values of a dict in increase order given the initial key
I am looking for the best way to do some task. Hope you can help me to improve what I have so far.
I have two dicts with same keys, in the dict ('other_dict') I need to find the key which match some value and then that key will be the key with value 0 in t_position dict and then fill the rest of the t_position dict keys with values from 1 to 5 in increasing order. Lets say that the value is in key "V3", then its position will be 0, "V4" : 1, "V5" : 2, "V0" : 3 ... "V2" : 5
t_positions = {"V0": 6, "V1": 6, "V2": 6, "V3": 6, "V4": 6, "V5": 6}
other_dict = {"V0": 0, "V1": 0, "V2": 0, "V3": 1, "V4": 0, "V5": 0}
value_to_find = 1
def pos_detector():
dict_keys = ["V0", "V1", "V2", "V3", "V4", "V5"]
value_index = None
for key, value in other_dict.items():
if value == value_to_find:
t_positions[key] = 0
value_index = dict_keys.index(key)
break
up_counter = value_index + 1
down_counter = 0
position_counter = 1
while up_counter <= 5:
t_positions[dict_keys[up_counter]] = position_counter
up_counter += 1
position_counter += 1
while down_counter < value_index:
t_positions[dict_keys[down_counter]] = position_counter
down_counter += 1
position_counter += 1
pos_detector()
print(t_positions)
Thanks
*EDIT: Now there is a minimal reproducible example. I can think in other ways to do it, but the key of the question is about time, I am looking to the fastest solution, because I will use this function a lot of times.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
