Flipping the boolean values in a list Python - python

I have a boolean list in Python
mylist = [True , True, False,...]
which I want to change to the logical opposite [False, False, True , ...]
Is there an inbuilt way to do this in Python (something like a call not(mylist) ) without a hand-written loop to reverse the elements?

It's easy with list comprehension:
mylist = [True , True, False]
[not elem for elem in mylist]
yields
[False, False, True]

The unary tilde operator (~) will do this for a numpy.ndarray. So:
>>> import numpy
>>> mylist = [True, True, False]
>>> ~numpy.array(mylist)
array([False, False, True], dtype=bool)
>>> list(~numpy.array(mylist))
[False, False, True]
Note that the elements of the flipped list will be of type numpy.bool_ not bool.

>>> import operator
>>> mylist = [True , True, False]
>>> map(operator.not_, mylist)
[False, False, True]

Numpy includes this functionality explicitly. The function
"numpy.logical_not(x[, out])" computes the truth value of NOT x element-wise.
import numpy
numpy.logical_not(mylist)
http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.logical_not.html (with same examples)
Example:
import numpy
mylist = [True , True, False]
print (mylist)
returns [True, True, False]
mylist=numpy.logical_not(mylist)
print (mylist)
returns [False False True]

numpy.invert is another nice option:
x = [False, True, True]
not_x = np.invert(x) # [True, False, False]

Why not just use a simple list comprehension?
mylist[:] = [not x for x in mylist]

I would do it the way everybody else is saying, but for sake of documenting alternatives, you could also do
import operator
myList = map(operator.not_, myList)

what about the following
>>> import numpy
>>> list(numpy.asarray(mylist)==False)

Related

extending list of booleans by complements of another list of booleans

Given two lists list1 and list2 of booleans , I want to extend list1 by the complements of the elements in list2. For example if
list1 = [True, True, False]
list2 = [False, False, True, False]
then after the operation
list1 = [True, True, False, True, True, False, True]
while list2 shall remain unchanged.
What is the most pythonic way to achieve that?
How about this:
list1.extend(not value for value in list2)
If there is a risk that list2 is an alias to list1, you are better off with
list1 += [not value for value in list2]
temp_list = [not elem for elem in list2]
list1.extend(temp_list)
If you want to generalize this to longer or more lists/arrays, you could have a look at numpy:
a1 = np.array([True, True, False])
a2 = np.array([False, False, True, False])
out = np.r_[a1, ~a2]
output: array([ True, True, False, True, True, False, True])
Using the not keyword in the list comprehension, will just convert it to it's compliment.
list1.extend([not value for value in list2])
The operator module provides the negation function, __not__ or its alias not_
from operator import __not__
list1.extend(map(__not__, list2))
print(list1)

Return the indices of "false" values in a boolean array

I feel like this is a really simple question but I can't find the solution.
Given a boolean array of true/false values, I need the output of all the indices with the value "false". I have a way to do this for true:
test = [ True False True True]
test1 = np.where(test)[0]
This returns [0,2,3], in other words the corresponding index for each true value. Now I need to just get the same thing for the false, where the output would be [1]. Anyone know how?
Use np.where(~test) instead of np.where(test).
With enumerate:
>>> test = [True, False, True, True]
>>> [i for i, b in enumerate(test) if b]
[0, 2, 3]
>>> [i for i, b in enumerate(test) if not b]
[1]
Using explicite np.array().
import numpy as np
test = [True, False, True, True]
a = np.array(test)
test1 = np.where(a==False)[0] #np.where(test)[0]
print(test1)

How to return True if two-dimensional array in python has at least one True

I'm trying to write a code which checks if 2D-array (consists of only boolean) has at least one True and return True if there is at least one True.
I tried using all() function but couldn't come up with a solution. I assume what I need is opposite of what all() function does.
>>> array1 = [[True, False], [False, False]]
>>> all([all(row) for row in array1)
False # This should be True
>>> array2 = [[False, False], [False, False]]
>>> all([all(row) for row in array2)
False # This is correct but this and array with all True is only working case.
For array1 = [[True, False], [False, False]], I expect the output to be True since there is one True at array1[0][0].
def has_true(arr):
return any(any(row) for row in arr)
In [7]: array1 = [[True, False], [False, False]]
In [8]: array2 = [[False, False], [False, False]]
In [9]: has_true(array1)
Out[9]: True
In [10]: has_true(array2)
Out[10]: False
this answer is using generators so it will return upon finding the first True value without running over the whole matrix. in addition it will use O(1) space
edit: removed unnecessary code
use any() instead of all(). all() return true if all items in an iterable object are true.
any() Returns True if any item in an iterable object is true.
A much shorter approach is to chain the lists together using itertools.chain and apply any on them
from itertools import chain
def has_true(arr):
return any(chain(*arr))
print(has_true([[True, False], [False, False]]))
print(has_true([[False, False], [False, False]]))
The output will be
True
False

Logical operation between two Boolean lists

I get a weird result and I try to apply the and or the or operator to 2 Boolean lists in python. I actually get the exact opposite of what I was expecting.
[True, False, False] and [True, True, False]
> [True, True, False]
[True, False, False] or [True, True, False]
> [True, False, False]
Is that normal, and if yes, why?
If what you actually wanted was element-wise boolean operations between your two lists, consider using the numpy module:
>>> import numpy as np
>>> a = np.array([True, False, False])
>>> b = np.array([True, True, False])
>>> a & b
array([ True, False, False], dtype=bool)
>>> a | b
array([ True, True, False], dtype=bool)
This is normal, because and and or actually evaluate to one of their operands. x and y is like
def and(x, y):
if x:
return y
return x
while x or y is like
def or(x, y):
if x:
return x
return y
Since both of your lists contain values, they are both "truthy" so and evaluates to the second operand, and or evaluates to the first.
I think you need something like this:
[x and y for x, y in zip([True, False, False], [True, True, False])]
Both lists are truthy because they are non-empty.
Both and and or return the operand that decided the operation's value.
If the left side of and is truthy, then it must evaluate the right side, because it could be falsy, which would make the entire operation false (false and anything is false). Therefore, it returns the right side.
If the left side of or is truthy, it does not need to evaluate the right side, because it already knows that the expression is true (true or anything is true). So it returns the left side.
If you wish to perform pairwise comparisons of items in the list, use a list comprehension, e.g.:
[x or y for (x, y) in zip(a, b)] # a and b are your lists
Your lists aren't comparing each individual value, they're comparing the existence of values in the list.
For any truthy variables a and b:
a and b
> b #The program evaluates a, a is truthy, it evaluates b, b is truthy, so it returns the last evaluated value, b.
a or b
> a #The program evaluates a, a is truthy, so the or statement is true, so it returns the last evaluated value, a.
Now, truthy depends on the type. For example, integers are truthy for my_int != 0, and are falsy for my_int == 0. So if you have:
a = 0
b = 1
a or b
> b #The program evaluates a, a is falsy, so the or statement goes on to evaluate b, b is truthy, so the or statement is true and it returns the last evaluated value b.
Very convenient way:
>>> import numpy as np
>>> np.logical_and([True, False, False], [True, True, False])
array([ True, False, False], dtype=bool)
>>> np.logical_or([True, False, False], [True, True, False])
array([ True, True, False], dtype=bool)
Мore functional:
from operator import or_, and_
from itertools import starmap
a = [True, False, False]
b = [True, True, False]
starmap(or_, zip(a,b)) # [True, True, False]
starmap(and_, zip(a,b)) # [True, False, False]

Python: numpy array larger and smaller than a value

How to look for numbers that is between a range?
c = array[2,3,4,5,6]
>>> c>3
>>> array([False, False, True, True, True]
However, when I give c in between two numbers, it return error
>>> 2<c<5
>>> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
The desire output is
array([False, True, True, False, False]
Try this,
(c > 2) & (c < 5)
Result
array([False, True, True, False, False], dtype=bool)
Python evaluates 2<c<5 as (2<c) and (c<5) which would be valid, except the and keyword doesn't work as we would want with numpy arrays. (It attempts to cast each array to a single boolean, and that behavior can't be overridden, as discussed here.) So for a vectorized and operation with numpy arrays you need to do this:
(2<c) & (c<5)
You can do something like this :
import numpy as np
c = np.array([2,3,4,5,6])
output = [(i and j) for i, j in zip(c>2, c<5)]
Output :
[False, True, True, False, False]

Categories