This question already has answers here:
Mask out specific values from an array
(3 answers)
Closed 5 years ago.
Here is the deal,
idx_arr = [0,3,5,7];
tgt_arr = [
[0,3,3,5,5,6,6],
[1,1,3,1,1,3,3],
[2,4,6,8,1,2,9]]
I want to make new array with bool type that would look like. I tried also with sets but numpy.ndarrays are unhashable types. New matrix would look like
final_arr = [
[t,t,t,t,t,f,f],
[f,f,t,f,f,t,t],
[f,f,f,f,f,f,f]]
Thanks in advance.
Using base Python:
[[True if val in idx_arr else False for val in row] for row in tgt_arr]
Result:
[[True, True, True, True, True, False, False],
[False, False, True, False, False, True, True],
[False, False, False, False, False, False, False]]
Related
I am trying to make a grid to store bool variables kinda like mine sweeper and i would like to find a better way
So far i have a very inefficient way of just declaring like 15 lists with the values set to false like this
A = [False, False, False, False, False, False, False, False, False, False]
Is there a more efficient way to do this
You can efficiently create a list of the same value with:
A = [False]*15
However, more code is required to extend this into a grid. Instead, you could use NumPy to create a grid of False (True) values by using np.zeros (np.ones). For example, a 3x4 grid of False values can be created with:
grid = np.zeros((3, 4), dtype=bool)
>> [[False False False False]
>> [False False False False]
>> [False False False False]]
You might want to use a 2D array for that:
array = [
[False, False, False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False, False, False],
...
]
This can also be created using a list comp:
array = [[False] * 15 for _ in range(15)]
This question already has answers here:
if/else in a list comprehension
(12 answers)
Closed 1 year ago.
I want to use the one lined syntax for the "for_loop/if/elif/else/lists" for the code under:
for i in range(20):
if(i<15):
print("True")
else:
print("False")
this is a part of it
[True for i in range(20) if i<15]
How to add the "else" for it?
Try it.
s = [True if i<15 else False for i in range(20)]
print(s)
output
[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, False, False, False, False, False]
I want to merge certain values that are numerically close.
In this example I'll look to merge anything that can be connected via a neighboring value with a difference less 2.
import numpy as np
a = np.arange(10)
a = np.delete(a, (3, 7))
matrix = np.abs(a.reshape(-1,1) - a)
matrix < 2
array([[ True, True, False, False, False, False, False, False],
[ True, True, True, False, False, False, False, False],
[False, True, True, False, False, False, False, False],
[False, False, False, True, True, False, False, False],
[False, False, False, True, True, True, False, False],
[False, False, False, False, True, True, False, False],
[False, False, False, False, False, False, True, True],
[False, False, False, False, False, False, True, True]])
Starting at the top left corner:
First move horizontal to find the last true value.
Then go downwards to find the last true value.
Repeat both until the square is found.
Cannot connect True values diagonally.
This would give you the first square that goes from [0,2] to [2,2].
The desired output of this example would be:
[[0, 2], [3, 5], [6, 7]]
Where the values indicate the beginning and end of the square. Is there a good way of doing this?
I'd prefer not to loop if possible.
So I've done this using np.diagonal as the basis for the logic.
bm = matrix < 2
endcoords = np.argwhere(bm[1:].diagonal() == False)
zers = np.zeros(endcoords.shape)
zers[1:] += endcoords[:-1] + 1
end = np.hstack((zers, endcoords))
end
array([[0., 2.],
[3., 5.]])
I know this excludes the last coordinate, which for my example doesn't give the 'correct' answer. This however, does give the right answer for my workflow as I'm chunking arrays together - so the last missing coordinate will be the first of my next array. It shouldn't be too hard to finagle the right answer from this anyways.
How do I compute the inverse of what is described here: Getting indices of True values in a boolean list ?
That above link always comes up when I try searching for "how to obtain the true values in a boolean list from integer indices," but it gives me the indices from the true values in a boolean list, which is the inverse of what I want...
For example, from:
t = [4, 5, 7]
count = 16
I want to obtain:
[False, False, False, False, True, True, False, True, False, False, False, False, False, False, False, False]
The values are all 0 indexed, as expected with Python.
I'm guessing that my question is a duplicate, but it's so annoying to not be able to find what I'm looking for every time I try to remember how to do this operation, I decided to ask a new question so my Google search will hopefully bring up this post next time.
You can use a list comprehension. I recommend you turn t into a set for O(1) lookup:
t_set = set(t)
res = [i in t_set for i in range(count)]
Use a list comprehension with conditions:
print([True if i in t else False for i in range(count)])
Shorter:
print([i in t else False for i in range(count)])
How about this:
In [6]: holderplace =[False for i in range(count)]
In [7]: for i in t:
...: holderplace[i-1]=True
...:
In [8]: holderplace
Out[8]:
[False,
False,
False,
True,
True,
False,
True,
False,
False,
False,
False,
False,
False,
False,
False,
False]
In [9]:
You could also try using map():
list(map(lambda x: x in t, range(count)))
# [False, False, False, False, True, True, False, True, False, False, False, False, False, False, False, False]
It might also be worth converting t to a set, since lookup is O(1) instead of O(N).
You could also use __contains__():
list(map(t.__contains__, range(count)))
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 7 years ago.
I have a list in list. All the values are False. I have to change value to True of a very specific one.
s=[[False,False,False,False]
[False,False,False,False]
[False,False,False,False]
[False,False,False,False]]
I want to change it into this:
s=[[False,False,False,False]
[False,False,False,False]
[False,True,False,False]
[False,False,False,False]]
P.s.:
I tried doing this:
s[2][1]=True
But i got this:
[[False,True,False,False]
[False,True,False,False]
[False,True,False,False]
[False,True,False,False]]
Add commas in order to build your matrix.
s=[[False,False,False,False],
[False,False,False,False],
[False,False,False,False],
[False,False,False,False]]
s[2][1]=True
for i in s:
print i
Outputs:
[False, False, False, False]
[False, False, False, False]
[False, True, False, False]
[False, False, False, False]