Python - the lowest cost of checking multiple equalities at once

I start with a list full of False elements.
Then these elements switch to True independently during iterations.
I need to know when the list is completely right.

Say I have 3 elements, they start as

 [False, False, False] 

then I check them by iterations like:

 elements == [True, True, True] 

The list of items is fixed and should not grow (or shrink). You can think of these elements as switches, the input determines how many there are, and they are all disabled. The only thing that can happen over time is that individual switches are turned on (True) by events occurring in the iteration.

How does python validate and what is the cost?
What is the best way in terms of cost to test this?
Is there a way with bitwise operations or something that checks all the elements at once?

+7
python list
source share
3 answers

You can use bit operations to use a number as an array of flag bits. To make this work, we must encode your True as a cleared bit, but False as the set bit. Thus, the number becomes zero if all bits are cleared.

This works well because the number of flags is fixed. Starting with an array of set bits, you need to clear them until the number becomes zero.

This provides faster verification of conditions for minor bits of complexity and cost when cleaning bits. Testing if the number is zero is much cheaper than applying all to any list of booleans.

Comments on this issue suggested keeping the score and list. When one of the values ​​becomes true, the counter either reaches the final value of the list length or decreases from the list length to zero. This will work, but it is redundant since the same fact is encoded twice as a counter and once as a number of Trues.

This combines an account and a list. It does not contain redundancy.

Start with 5 preset bits:

 >>> bin((1<<5)-1) '0b11111' 

Then clean them. This clears the 4th bit:

 >>> bin(((1<<5)-1) & ~(1 << 3)) '0b10111' 

This will allow your loop to have a state like the following loop:

 flags = (1<<5)-1 n = 0 while flags: flags &= ~(1<<n) print bin(flags) n += 1 

This cycle starts with 5 preset bits and clears them one at a time.

 >>> flags = (1<<5)-1 >>> n = 0 >>> while flags: ... flags &= ~(1<<n) ... print bin(flags) ... n += 1 ... 0b11110 0b11100 0b11000 0b10000 0b0 
+2
source share

Use all

 >>> l = [True, True, True] >>> all(l) True 

Note that if iterability is empty, it will also return True .

 >>> all([]) True 
+4
source share

You can create your own flag class that implements the @StefanPochmann idea and keeps track of how many flags have been set.

Proof of concept:

 class flags: def __init__(self,n): self.__flags = [False]*n self.__ntrue = 0 def flags(self): return self.__flags[:] #so read only def __len__(self): return len(self.__flags) def check(self,i): return self.__flags[i] def on(self,i): if not self.check(i): self.__ntrue +=1 self.__flags[i] = True def off(self,i): if self.check(i): self.__ntrue -=1 self.__flags[i] = False def toggle(self,i): if self.check(i): self.off(i) else: self.on(i) def ntrue(self): return self.__ntrue 

Tested as follows:

 import random i = 0 f = flags(5) while f.ntrue() != len(f): i +=1 f.toggle(random.randint(0,4)) print(i,f.flags()) 

Typical Output:

 19 [True, True, True, True, True] 
+3
source share

All Articles