Skip to main content
deleted 22 characters in body
Source Link
dawg
  • 104.7k
  • 24
  • 142
  • 217

Suppose you have and arbitrary group of lists:

A=[True, False, False] B=[True, True, False] C=[3,0,0] 

Now write something that looks a bit like itertools.izip but allows a function to be added:

def elements(*iterables, **kwds): func=kwds.get('func', None) iterables=map(iter, iterables) while iterables: t=tuple(map(next, iterables)) if func is not None: yield func(t) else: yield t 

Now add functions that will return the logical result of F(A[0],B[0],C[0]...). For example, these each do the function described:

def ands(elements): ''' logical 'and' for all the elements''' return all(e for e in elements) def ors(elements): ''' logical 'or' for all the elements''' return any(e for e in elements) def bitand(elements): ''' bitwise 'and' for all the elements''' return reduce(operator.and_,elements) 

Then just call the generatorfunction:

print list(elements(A,B,C,func=ands)) # [True, False, False] 

Or for you specific example:

print list(elements([True,False],[True,True],func=ands)) # [True, False] 

Or just use all directly:

print list(elements([True,False],[True,True],func=all)) # [True, False] 

Suppose you have and arbitrary group of lists:

A=[True, False, False] B=[True, True, False] C=[3,0,0] 

Now write something that looks a bit like itertools.izip but allows a function to be added:

def elements(*iterables, **kwds): func=kwds.get('func', None) iterables=map(iter, iterables) while iterables: t=tuple(map(next, iterables)) if func is not None: yield func(t) else: yield t 

Now add functions that will return the logical result of F(A[0],B[0],C[0]...). For example, these each do the function described:

def ands(elements): ''' logical 'and' for all the elements''' return all(e for e in elements) def ors(elements): ''' logical 'or' for all the elements''' return any(e for e in elements) def bitand(elements): ''' bitwise 'and' for all the elements''' return reduce(operator.and_,elements) 

Then just call the generator:

print list(elements(A,B,C,func=ands)) # [True, False, False] 

Or for you specific example:

print list(elements([True,False],[True,True],func=ands)) # [True, False] 

Or just use all directly:

print list(elements([True,False],[True,True],func=all)) # [True, False] 

Suppose you have and arbitrary group of lists:

A=[True, False, False] B=[True, True, False] C=[3,0,0] 

Now write something that looks a bit like itertools.izip but allows a function to be added:

def elements(*iterables, **kwds): func=kwds.get('func', None) iterables=map(iter, iterables) while iterables: t=tuple(map(next, iterables)) if func is not None: yield func(t) else: yield t 

Now add functions that will return the logical result of F(A[0],B[0],C[0]...). For example, these each do the function described:

def ands(elements): ''' logical 'and' for all the elements''' return all(elements) def ors(elements): ''' logical 'or' for all the elements''' return any(elements) def bitand(elements): ''' bitwise 'and' for all the elements''' return reduce(operator.and_,elements) 

Then just call the function:

print list(elements(A,B,C,func=ands)) # [True, False, False] 

Or for you specific example:

print list(elements([True,False],[True,True],func=ands)) # [True, False] 

Or just use all directly:

print list(elements([True,False],[True,True],func=all)) # [True, False] 
deleted 22 characters in body
Source Link
dawg
  • 104.7k
  • 24
  • 142
  • 217

Suppose you have and arbitrary group of lists:

A=[True, False, False] B=[True, True, False] C=[3,0,0] 

Now write something that looks a bit like itertools.izip but allows a function to be added:

def elements(*iterables, **kwds): func=kwds.get('func', None) iterables=map(iter, iterables) while iterables: t=tuple(map(next, iterables)) if func is not None: yield func(t) else: yield t 

Now add functions that will return the logical result of F(A[0],B[0],C[0]...). For example, these each do the function described:

def ands(elements): ''' logical 'and' for all the elements''' return all(e for e in elements) def ors(elements): ''' logical 'or' for all the elements''' return any(e for e in elements) def bitand(elements): ''' bitwise 'and' for all the elements''' return reduce(operator.and_,elements) 

Then just call the generator:

print list(elements(A,B,C,func=ands)) # [True, False, False] 

Or for you specific example:

print list(elements([True,False],[True,True],func=ands)) # [True, False] 

Or as a lambda if you wishjust use all directly:

print list(elements([True,False],[True,True],func=lambda t: all(e for e in t)func=all))  # [True, False] 

Suppose you have and arbitrary group of lists:

A=[True, False, False] B=[True, True, False] C=[3,0,0] 

Now write something that looks a bit like itertools.izip but allows a function to be added:

def elements(*iterables, **kwds): func=kwds.get('func', None) iterables=map(iter, iterables) while iterables: t=tuple(map(next, iterables)) if func is not None: yield func(t) else: yield t 

Now add functions that will return the logical result of F(A[0],B[0],C[0]...). For example, these each do the function described:

def ands(elements): ''' logical 'and' for all the elements''' return all(e for e in elements) def ors(elements): ''' logical 'or' for all the elements''' return any(e for e in elements) def bitand(elements): ''' bitwise 'and' for all the elements''' return reduce(operator.and_,elements) 

Then just call the generator:

print list(elements(A,B,C,func=ands)) # [True, False, False] 

Or for you specific example:

print list(elements([True,False],[True,True],func=ands)) # [True, False] 

Or as a lambda if you wish:

print list(elements([True,False],[True,True],func=lambda t: all(e for e in t))) # [True, False] 

Suppose you have and arbitrary group of lists:

A=[True, False, False] B=[True, True, False] C=[3,0,0] 

Now write something that looks a bit like itertools.izip but allows a function to be added:

def elements(*iterables, **kwds): func=kwds.get('func', None) iterables=map(iter, iterables) while iterables: t=tuple(map(next, iterables)) if func is not None: yield func(t) else: yield t 

Now add functions that will return the logical result of F(A[0],B[0],C[0]...). For example, these each do the function described:

def ands(elements): ''' logical 'and' for all the elements''' return all(e for e in elements) def ors(elements): ''' logical 'or' for all the elements''' return any(e for e in elements) def bitand(elements): ''' bitwise 'and' for all the elements''' return reduce(operator.and_,elements) 

Then just call the generator:

print list(elements(A,B,C,func=ands)) # [True, False, False] 

Or for you specific example:

print list(elements([True,False],[True,True],func=ands)) # [True, False] 

Or just use all directly:

print list(elements([True,False],[True,True],func=all))  # [True, False] 
added 140 characters in body
Source Link
dawg
  • 104.7k
  • 24
  • 142
  • 217

Suppose you have and arbitrary group of lists:

A=[True, False, False] B=[True, True, False] C=[3,0,0] 

Now write something that looks a bit like itertools.izip but allows a function to be added:

def elements(*iterables, **kwds): func=kwds.get('func', None) iterables=map(iter, iterables) while iterables: t=tuple(map(next, iterables)) if func is not None: yield func(t) else: yield t 

Now add functions that will return the logical result of F(A[0],B[0],C[0]...). For example, these each do the function described:

def ands(elements): ''' logical 'and' for all the elements''' return all(e for e in elements) def ors(elements): ''' logical 'or' for all the elements''' return any(e for e in elements) def bitand(elements): ''' bitwise 'and' for all the elements''' return reduce(operator.and_,elements) 

Then just call the generator:

print list(elements(A,B,C,func=ands)) # [True, False, False] 

Or for you specific example:

print list(elements([True,False],[True,True],func=ands)) # [True, False] 

Or as a lambda if you wish:

print list(elements([True,False],[True,True],func=lambda t: all(e for e in t))) # [True, False] 

Suppose you have and arbitrary group of lists:

A=[True, False, False] B=[True, True, False] C=[3,0,0] 

Now write something that looks a bit like itertools.izip but allows a function to be added:

def elements(*iterables, **kwds): func=kwds.get('func', None) iterables=map(iter, iterables) while iterables: t=tuple(map(next, iterables)) if func is not None: yield func(t) else: yield t 

Now add functions that will return the logical result of F(A[0],B[0],C[0]...). For example, these each do the function described:

def ands(elements): ''' logical 'and' for all the elements''' return all(e for e in elements) def ors(elements): ''' logical 'or' for all the elements''' return any(e for e in elements) def bitand(elements): ''' bitwise 'and' for all the elements''' return reduce(operator.and_,elements) 

Then just call the generator:

print list(elements(A,B,C,func=ands)) # [True, False, False] 

Or for you specific example:

print list(elements([True,False],[True,True],func=ands)) # [True, False] 

Suppose you have and arbitrary group of lists:

A=[True, False, False] B=[True, True, False] C=[3,0,0] 

Now write something that looks a bit like itertools.izip but allows a function to be added:

def elements(*iterables, **kwds): func=kwds.get('func', None) iterables=map(iter, iterables) while iterables: t=tuple(map(next, iterables)) if func is not None: yield func(t) else: yield t 

Now add functions that will return the logical result of F(A[0],B[0],C[0]...). For example, these each do the function described:

def ands(elements): ''' logical 'and' for all the elements''' return all(e for e in elements) def ors(elements): ''' logical 'or' for all the elements''' return any(e for e in elements) def bitand(elements): ''' bitwise 'and' for all the elements''' return reduce(operator.and_,elements) 

Then just call the generator:

print list(elements(A,B,C,func=ands)) # [True, False, False] 

Or for you specific example:

print list(elements([True,False],[True,True],func=ands)) # [True, False] 

Or as a lambda if you wish:

print list(elements([True,False],[True,True],func=lambda t: all(e for e in t))) # [True, False] 
Source Link
dawg
  • 104.7k
  • 24
  • 142
  • 217
Loading