I am new to Python, I had asked a similar question before but this is still different from that. Although long and complex (my apologies that it sounds complicated), I would appreciate your help once again, since I've been having difficulty getting it to work right.
Considering the following example dictionary:
dict = {'*1': '*3', '*2': '*3', '*3': '+1', '*4': '*5', '*5': '+2', '*6': '+3', '+1': '+2', '+4': '+5', '+5': '0', '+3': '0', '+2': '0'}
I also have an integer variable say: var which is equal to 4. Based on this number, we create a nested list of strings, where the number of sublists = var value (in this case, 4 sublists in the nested list).
Sublist number 4 (the last sublist in this case) would contain those keys from
dictthat have values'0'. Thus, sublist 4 =['+5', '+3', '+2'].Once that is done, we delete these key-value pairs from
dict. Thus,dictnow has the following pairs:
dict = {'*1': '*3', '*2': '*3', '*3': '+1', '*4': '*5', '*5': '+2', '*6': '+3', '+1': '+2', '+4': '+5'}
For these remaining pairs, we iterate over dict (as long as it is non-empty) to find those keys in it, whose corresponding values are in sublist 4. In this case, this would be: '*5', '*6', '+1', '+4'. As long as all of the values for these keys were in sublist 4, we can assign these keys to the previous sublist, that is, (4-1)= sublist 3.
[However, if any of the values were not in sublist4 and say they were in sublist 3 for example, then, the corresponding key would go to (3-1)= sublist 2. So before assigning to any list, this check needs to be performed every time. This may happen in cases when there are more than one value to a key and not every key is assigned the same sublist.]
- Once
'*5', '*6', '+1', '+4'keys are in sublist 3, we delete these key-value pairs fromdict. Sodictnow has:
dict = {'*1': '*3', '*2': '*3', '*3': '+1', '*4': '*5'}
- Again we need to select those keys whose values are in sublist 3. For sublist 3 elements
'*5, +1'indict, the corresponding keys are'*4, *3'respectively. Ideally'*4, *3'would then go to sublist (3-1) = sublist 2. However, a check is to be performed such that if any of the existing values were not in sublist 3 and instead were in sublist 2 for example, then, the corresponding key would go to sublist (2-1)= sublist 1. This may happen in cases when there are more than one value to a key and when not every key is assigned the same sublist. In that case, the current key would get assigned to the(minimum of the 2 sublist numbers - 1), for example, sublist (2-1)= sublist 1. This check needs to be performed each time from beginning to end before assigning any component to a list. We would repeat this until dict is empty.
Desired Output for this example would be:
[['*1', '*2'], ['*3', '*4'], ['+1', '*5', '*6', '+4'], ['+2', '+3', '+5']]
What I tried:
d2 = {'*1': '*3', '*2': '*3', '*3': '+1', '*4': '*5', '*5': '+2', '*6': '+3', '+1': '+2', '+4': '+5', '+5': '0', '+3': '0', '+2': '0'}
nested_list = [['']]
var_latency = 4
last_sublist = [k for k, v in d2.items() if v == '0']
print(last_sublist) #this adds the last sublist elements from dict
for k in last_sublist:
del d2[k]
while d2 != 0:
for key, value in d2.items():
if d2[value] in last_sublist:
sublist[var_latency-1] = d2[key]
del d2[key]
var_latency -= 1