元组假索引函数,考虑旧的假索引。

huangapple go评论91阅读模式
英文:

tuple false indexing function that takes into consideration old false indices

问题

  1. 考虑一个包含全部为True的布尔值列表:
  2. bools = [True] * 100
  3. 然后我有一个元组,其中包含我想将其设置为False的布尔值的索引:
  4. false_index = (0, 2, 4)
  5. 对于false_index中的每个元素:
  6. bools[element] = False
  7. 下一次,我想使用相同的元组设置False元素,需要考虑已经被翻转的列表中的`False`值的索引。
  8. 我想要一个函数,该函数接受已经翻转的值的索引和要翻转的新值,并返回一个包含要翻转的True值的元组,例如:
  9. old_false_index = (0, 2, 4)
  10. new_false_index = (0, 2, 4)
  11. check_index(old_false_index, new_false_index) # 输出 (1, 5, 7)
  12. 更直观的例子:
  13. 对于一个包含8True值的列表:
  14. [
  15. 1
  16. 1
  17. 1
  18. 1
  19. 1
  20. 1
  21. 1
  22. 1
  23. ]
  24. 第一个元组将给定索引处的元素更改为False。所以对于(0, 2, 4),列表现在已更改为:
  25. [
  26. 1 # 索引 0
  27. 1
  28. 1 # 索引 2
  29. 1
  30. 1 # 索引 4
  31. 1
  32. 1
  33. 1
  34. ]
  35. 下一次使用相同的元组更改它时,索引已经改变:
  36. [
  37. 0
  38. 1 # 索引 1,但可以更改的第一个元素
  39. 0
  40. 1
  41. 0
  42. 1 # 索引 5,但可以更改的第三个元素
  43. 1
  44. 1 # 索引 7,但可以更改的第四个元素
  45. ]
英文:

Imagine a list of booleans, which are all True:

  1. bools = [True] * 100

I then have a tuple that correspond to the index of the bool that i want to set to False:

  1. false_index = (0,2,4)
  2. for element in false_index:
  3. 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:

  1. old_false_index = (0,2,4)
  2. new_false_index = (0,2,4)
  3. check_index(old_false_index, new_false_index) # output (1,5,7)

A more visual example:
for a list of 8 True values:

  1. [
  2. 1
  3. 1
  4. 1
  5. 1
  6. 1
  7. 1
  8. 1
  9. 1
  10. ]

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. [
  2. 1 # index 0
  3. 1
  4. 1 # index 2
  5. 1
  6. 1 # index 4
  7. 1
  8. 1
  9. 1
  10. ]

The next time it is changed with the same tuple, the index has changed:

  1. [
  2. 0
  3. 1 # index 1 but the first element that can be changed
  4. 0
  5. 1
  6. 0
  7. 1 # index 5 but the third element that can be changed
  8. 1
  9. 1 # index 7 but the fourth element that can be changed
  10. ]

答案1

得分: 1

  1. def check_index(old_false_index, new_false_index):
  2. result = []
  3. for new_index in new_false_index:
  4. for old_index in old_false_index:
  5. # 检查是否存在较小的索引在旧索引中
  6. # 对于每个找到的索引,递增一
  7. if old_index <= new_index:
  8. new_index += 1
  9. result.append(new_index)
  10. return tuple(result)
  11. old_false_index = (0, 2, 4)
  12. new_false_index = (0, 2, 4)
  13. print(check_index(old_false_index, new_false_index)) # (1, 5, 7)
英文:

You could do something like this:

  1. def check_index(old_false_index, new_false_index):
  2. result = []
  3. for new_index in new_false_index:
  4. for old_index in old_false_index:
  5. # check if a smaller index already exists in the old
  6. # for each one you find, increment by one
  7. if old_index &lt;= new_index:
  8. new_index += 1
  9. result.append(new_index)
  10. return tuple(result)
  11. old_false_index = (0,2,4)
  12. new_false_index = (0,2,4)
  13. 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.

  1. old_false_index = (0,2,4)
  2. new_false_index = (0,2,4)
  3. actual_new_false_indexes = check_index(old_false_index, new_false_index)
  4. old_false_index += actual_new_false_indexes

huangapple
  • 本文由 发表于 2023年4月19日 23:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定