krm76
krm76

Reputation: 356

How to compare values with same index position between sublists within a list?

For visual simplicity I'm going to paste the code that I consider relevant, if something else is needed, please leave a comment.

Let's say we have the following dictionary called 'states':

{0: ([[0, 0], [0, 0]], [1, 0]), 1: ([[0, 0], [0, 1]], [1, 0]), 2: ([[0, 0], [1, 0]], [1, 0]), 3: ([[0, 0], [1, 1]], [1, 0]), 4: ([[0, 1], [0, 0]], [1, 0]), 5: ([[0, 1], [0, 1]], [1, 0]), 6: ([[0, 1], [1, 0]], [1, 0]), 7: ([[0, 1], [1, 1]], [1, 0]), 8: ([[1, 0], [0, 0]], [1, 0]), 9: ([[1, 0], [0, 1]], [1, 0]), 10: ([[1, 0], [1, 0]], [1, 0]), 11: ([[1, 0], [1, 1]], [1, 0]), 12: ([[1, 1], [0, 0]], [1, 0]), 13: ([[1, 1], [0, 1]], [1, 0]), 14: ([[1, 1], [1, 0]], [1, 0]), 15: ([[1, 1], [1, 1]], [1, 0]), 16: ([[0, 0], [0, 0]], [0, 1]), 17: ([[0, 0], [0, 1]], [0, 1]), 18: ([[0, 0], [1, 0]], [0, 1]), 19: ([[0, 0], [1, 1]], [0, 1]), 20: ([[0, 1], [0, 0]], [0, 1]), 21: ([[0, 1], [0, 1]], [0, 1]), 22: ([[0, 1], [1, 0]], [0, 1]), 23: ([[0, 1], [1, 1]], [0, 1]), 24: ([[1, 0], [0, 0]], [0, 1]), 25: ([[1, 0], [0, 1]], [0, 1]), 26: ([[1, 0], [1, 0]], [0, 1]), 27: ([[1, 0], [1, 1]], [0, 1]), 28: ([[1, 1], [0, 0]], [0, 1]), 29: ([[1, 1], [0, 1]], [0, 1]), 30: ([[1, 1], [1, 0]], [0, 1]), 31: ([[1, 1], [1, 1]], [0, 1]), 32: ([[0, 0], [0, 1]], [-1, 0]), 33: ([[0, 0], [1, 0]], [-1, 0]), 34: ([[0, 0], [1, 1]], [-1, 0]), 35: ([[0, 1], [0, 0]], [-1, 0]), 36: ([[0, 1], [0, 1]], [-1, 0]), 37: ([[0, 1], [1, 0]], [-1, 0]), 38: ([[0, 1], [1, 1]], [-1, 0]), 39: ([[1, 0], [0, 0]], [-1, 0]), 40: ([[1, 0], [0, 1]], [-1, 0]), 41: ([[1, 0], [1, 0]], [-1, 0]), 42: ([[1, 0], [1, 1]], [-1, 0]), 43: ([[1, 1], [0, 0]], [-1, 0]), 44: ([[1, 1], [0, 1]], [-1, 0]), 45: ([[1, 1], [1, 0]], [-1, 0]), 46: ([[1, 1], [1, 1]], [-1, 0]), 47: ([[0, 0], [0, 1]], [0, -1]), 48: ([[0, 0], [1, 0]], [0, -1]), 49: ([[0, 0], [1, 1]], [0, -1]), 50: ([[0, 1], [0, 0]], [0, -1]), 51: ([[0, 1], [0, 1]], [0, -1]), 52: ([[0, 1], [1, 0]], [0, -1]), 53: ([[0, 1], [1, 1]], [0, -1]), 54: ([[1, 0], [0, 0]], [0, -1]), 55: ([[1, 0], [0, 1]], [0, -1]), 56: ([[1, 0], [1, 0]], [0, -1]), 57: ([[1, 0], [1, 1]], [0, -1]), 58: ([[1, 1], [0, 0]], [0, -1]), 59: ([[1, 1], [0, 1]], [0, -1]), 60: ([[1, 1], [1, 0]], [0, -1]), 61: ([[1, 1], [1, 1]], [0, -1])}

'states' depends on three variables: (Note: the values of the following variables generate the above dictionary)

NUM_MECS = 2
MAX_MEC_CAPACITY = 2
VNF_TYPES = 2

Let's choose value from key 1:

([[0, 0], [0, 1]], [1, 0])

'NUM_MECS' determines the number of sublists that the first item of the tupple (value from key 1) will have. Since its value is 2, we have two sublists: [[0, 0], [0, 1]].

'MAX_MEC_CAPACITY' determines the range for every value within the sublists for the first item in the tupple. That is to say, any of the values from [[0, 0], [0, 1]] can range from 0 to 1. If we change the value to 3 then would mean that every value ranges from 0 to 2 (e.g. [[2, 0], [1, 2]]).

'VNF_TYPES' will determine the size for all sublists and also for the second item in the tupple. Since its value is 2 we only have size 2 on all sublists and list from second item in the tupple ([[size 2], [size 2]], [size 2]).

The second item in the tupple, the list, can only hold two values (1 or -1) and only one of its items can hold it while the rest will remain with a value of 0. No matter the size of the list that only one of its items will hold either a 1 or a -1. where ever the 1 or -1 is, will determine in which position from the elements of the sublists from the first item in the tupple will be affected. Hence if we have:

 ([[0, 0], [0, 1]], [1, 0])

Means that a unit can be incremented in either of the first items from the sublists from the first item in the tupple.

Here:

 ([[1, 0], [0, 1]], [1, 0])

or here:

 ([[0, 0], [1, 1]], [1, 0])

Similarly if we have:

([[0, 1], [1, 1]], [-1, 0])

Means that we can substract a unit from any of the first elements from the sublists, in this particularly case only will me sibstracted from the second sublist:

([[0, 1], [0, 1]], [-1, 0])

How do we assaign to which sublist add or substract is not relevant.

Let's focus only on those values where the second item of the tupple will hold a -1. Following the guidelines previously explained, we can agree that it does not make any sense to have a value where we can not substract anything, like:

([[0, 0], [0, 0]], [-1, 0])

Therefor we could implement the following in order to get rid of all possible variations from the latter example:

for key, state in states.values():
    if ([[0]*VNF_TYPES]*NUM_MECS == state[0]) and (-1 in s[1]):
        del states[key]

This only allows to delete:

([[0, 0], [0, 0]], [-1, 0])

or

([[0, 0], [0, 0]], [0, -1])

PROBLEM STATEMENT

Although the above values have been removed from the dictionary, there still some values that still don't make sense, hence I want to delete them, like:

([[0, 0], [0, 1]], [-1, 0]),
([[0, 0], [1, 0]], [0, -1]),
([[0, 1], [0, 0]], [-1, 0]),
([[1, 0], [0, 0]], [0, -1]),
([[0, 1], [0, 1]], [-1, 0]),
([[1, 0], [1, 0]], [0, -1]),
([[0, 1], [0, 1]], [-1, 0]),

My question question is how can run through all values and then through all sublists within the first list of each value to compare values with same index position as the -1 in the second item of the tupple in order to remove them? Any suggestions?

Obviously the idea is to do it in a general way no matter the structure of the dictionary, therefore please do not suggest any hard-coding methods.

Upvotes: 1

Views: 97

Answers (1)

Kraigolas
Kraigolas

Reputation: 5590

This function will determine if a tuple is valid or not.

def is_valid_tuple(aTuple):
    try: 
        ind = aTuple[1].index(-1) 
    except ValueError: # There is no -1 
        return True 
    for ls in aTuple[0]: # for all sublists 
        if ls[ind] != 0: # if any are not zero, we are fine to exit 
            return True 
    return False 

For all of your test cases, it will return False.

Upvotes: 1

Related Questions