Imagine a list of booleans, which are all True:
bools = [True] * 100
I then have a tuple that correspond to the index of the bool that i want to set to False:
false_index = (0,2,4)
for element in false_index:
bools[element] = False
The next time i would like to set the False elements with the same tuple, the index of the false_index, needs to take into consideration the False values of the list that has already been flipped.
I would like a function that take the index of the values that are already flipped, and the new values to be flipped, and return a tuple of the true values to be flipped, like:
old_false_index = (0,2,4)
new_false_index = (0,2,4)
check_index(old_false_index, new_false_index) # output (1,5,7)
A more visual example:
for a list of 8 True values:
[
1
1
1
1
1
1
1
1
]
The first tuple changes the elements to False at the given indices. So for (0,2,4), the list has now been changed to:
[
1 # index 0
1
1 # index 2
1
1 # index 4
1
1
1
]
The next time it is changed with the same tuple, the index has changed:
[
0
1 # index 1 but the first element that can be changed
0
1
0
1 # index 5 but the third element that can be changed
1
1 # index 7 but the fourth element that can be changed
]
>Solution :
You could do something like this:
def check_index(old_false_index, new_false_index):
result = []
for new_index in new_false_index:
for old_index in old_false_index:
# check if a smaller index already exists in the old
# for each one you find, increment by one
if old_index <= new_index:
new_index += 1
result.append(new_index)
return tuple(result)
old_false_index = (0,2,4)
new_false_index = (0,2,4)
print(check_index(old_false_index, new_false_index)) # (1, 5, 7)
Keep in mind, if you’re doing this repeatedly, you’ll have to add the result to old_false_index in order to use old_false_index again in the same fashion.
old_false_index = (0,2,4)
new_false_index = (0,2,4)
actual_new_false_indexes = check_index(old_false_index, new_false_index)
old_false_index += actual_new_false_indexes