How to create a nested list with sublist elements assigned from a dictionary on certain custom requirements?

Viewed 55

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).

  1. Sublist number 4 (the last sublist in this case) would contain those keys from dict that have values '0'. Thus, sublist 4 = ['+5', '+3', '+2'].

  2. Once that is done, we delete these key-value pairs from dict. Thus, dict now 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.]

  1. Once '*5', '*6', '+1', '+4' keys are in sublist 3, we delete these key-value pairs from dict. So dict now has:
dict = {'*1': '*3', '*2': '*3', '*3': '+1', '*4': '*5'}
  1. Again we need to select those keys whose values are in sublist 3. For sublist 3 elements '*5, +1' in dict, 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

1 Answers

This code works:

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 = [[k for k, v in d2.items() if v == '0']]

var_latency = 4
for k in nested_list[0]:
    del d2[k]

while not all(x==None for x in d2.values()):
    print(d2, nested_list)
    nested_list.insert(0, [])
    for key, value in d2.items():
        if d2[key] in nested_list[1]:
            nested_list[0].append(key)
            d2[key] = None
    var_latency -= 1

print(nested_list)
Related