英文:
tuple false indexing function that takes into consideration old false indices
问题
考虑一个包含全部为True的布尔值列表:
bools = [True] * 100
然后我有一个元组,其中包含我想将其设置为False的布尔值的索引:
false_index = (0, 2, 4)
对于false_index中的每个元素:
bools[element] = False
下一次,我想使用相同的元组设置False元素,需要考虑已经被翻转的列表中的`False`值的索引。
我想要一个函数,该函数接受已经翻转的值的索引和要翻转的新值,并返回一个包含要翻转的True值的元组,例如:
old_false_index = (0, 2, 4)
new_false_index = (0, 2, 4)
check_index(old_false_index, new_false_index) # 输出 (1, 5, 7)
更直观的例子:
对于一个包含8个True值的列表:
[
1
1
1
1
1
1
1
1
]
第一个元组将给定索引处的元素更改为False。所以对于(0, 2, 4),列表现在已更改为:
[
1 # 索引 0
1
1 # 索引 2
1
1 # 索引 4
1
1
1
]
下一次使用相同的元组更改它时,索引已经改变:
[
0
1 # 索引 1,但可以更改的第一个元素
0
1
0
1 # 索引 5,但可以更改的第三个元素
1
1 # 索引 7,但可以更改的第四个元素
]
英文:
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
]
答案1
得分: 1
def check_index(old_false_index, new_false_index):
result = []
for new_index in new_false_index:
for old_index in old_false_index:
# 检查是否存在较小的索引在旧索引中
# 对于每个找到的索引,递增一
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)
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论