I want to find 2D array's index and make it array.
for example:
data_pre=[[1,1,1,0,0,0],[1,0,1,0,0,0],[1,0,0,0,1,0],[1,0,0,0,0,0]]
i wanna find index that have one and wanna make it like this
b=[[0,1,2],[0,2],[0,4],[0]]
Code:
result = []
for i in range(len(data_pre)):
arr=data_pre[i]
currentArrResult=[]
for j in range(len(arr)):
if arr[j]==1:
currentArrResult.append(j)
result.append(currentArrResult)
I tried like that but output is wrong.
[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 2], [0, 2], [0, 4], [0, 4], [0]]
I don't know which part is wrong...
you should not collect output inside the inner loop. that may get a result like this :
[[0],[0, 1],[0, 1, 2],
[0],[0, 2],
[0],[0, 4],
[0]]
you can check that by printing currentArrResult after append finish.
but you got different outcome because of data reference.
you should collect result after inner loop finish its work.
like:
result = []
for i in range(len(data_pre)):
arr=data_pre[i]
currentArrResult=[]
for j in range(len(arr)):
if arr[j]==1:
currentArrResult.append(j)
#print(currentArrResult)
result.append(currentArrResult)
Related
hope doing well.
Is it possible to merge two big numpy array to make a several ones. These two arrays have the same number of rows. One array contains some names:
name_arr = [[sub_1],
[sub_2],
[sub_3],
...
[sub_n]]
The other one has some values:
value_arr = [[1, 2, 3],
[5, 2, -1],
[0, 0, 4],
...
[6, 18,200]]
Now, I want to extract numpy arrays using both the name_arr and value_arr. To be clear, I want to extract arrays with the names coming from name_arr and values coming from value_arr:
sub_1= [[1, 2, 3]]
sub_2= [[1, 2, 3]]
sub_3= [[0, 0, 4]]
...
sub_4= [[6, 18,200]]
I tried to use a for loop, but it was not successful:
for i in name_arr:
for j in value_arr:
if i == j:
name_arr [0, i] = value_arr [0, j]
but it was not successful at all ...
FYI, I made the arrays by splitting a dictionary,
Dict_data = {'sub_1' : [1, 2, 3],
'sub_2' : [5, 2, -1],
'sub_3' : [0, 0, 4],
... ,
'sub_n' : [6, 18,200]}
in case of having a solution to do my extraction directly from the dictionary, I deeply appreciate that. definitely I prefer to find a was to extract numpy arrays with the name of my keys and related data.
In advance, I appreciate any feedback.
Regards
I assume your name array contains strings as follows:
name_arr = ['sub_1',
'sub_2',
'sub_3',
...
'sub_n']
in this case you can simply do it with a for loop:
my_dict = {}
for i in range(len(name_arr)):
my_dict[name_arr[i]] = value_arr[i, :]
Okay, this is super basic, which unfortunately means that searching for it gives a bajillion hits that all do something different and / or more complex.
Consider this code:
shape = (10, 20)
indices = []
for i in range(shape[0]):
for j in range(shape[1]):
indices.append([i, j])
or alternatively indices = itertools.product(range(10), range(20)).
Now, I feel like there must be a simple numpy function that does the same? Something like
indices = np.indices_into_shape((10, 20))
Most of the index-generating functions I can find via search generate multiple arrays, like in meshgrid or ix_.
You can stack meshgrids:
np.dstack(np.meshgrid(np.arange(10), np.arange(20), indexing='ij')).reshape(-1, 2)
One way would be
np.argwhere(np.broadcast_to(True,(3,4)))
# array([[0, 0],
# [0, 1],
# [0, 2],
# [0, 3],
# [1, 0],
# [1, 1],
# [1, 2],
# [1, 3],
# [2, 0],
# [2, 1],
# [2, 2],
# [2, 3]])
another (similar to #MadPhysicist's)
np.c_[np.unravel_index(np.arange(3*4),(3,4))]
This is probably a very basic question but I dont know what I have to search for to find the answer for it:
I have this code:
list = [[0,1],[0,2],[1,3],[1,4],[1,5]]
list.append(list[0])
for i in list:
i.append(0)
print(list)
This List will later be used as coordinates for a curve. I need to duplicate the first coordinate at the end to get a closed curve.
If I then want to add a third value to each coordinate in the list the first and last item in list will be iterated over twice:
[[0, 1, 0, 0], [0, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0], [0, 1, 0, 0]]
I am guessing they have the same memory address and thereby the append-function is applied to the same object at this address once for the first index and once for the last.
What is this phenomenon called ? what is the easiest way to get the list like this:
[[0, 1, 0], [0, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0], [0, 1, 0]]
Thank you for your help
You can do a list comprehension:
list = [[0,1],[0,2],[1,3],[1,4],[1,5]]
list.append(list[0])
list = [x + [0] for x in list]
print(list)
# [[0, 1, 0], [0, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0], [0, 1, 0]]
EDIT: The trick here is, using x + [0] within the list comprehension. This way new lists are created, thus you do not append 0 to the same list twice (Hattip to #dx_over_dt)
The problem you have with your approach is, that the first and last element of your list refers to the very same object. You can see this, when you print i and list for every iteration:
for i in list:
i.append(0)
print(i)
print(list)
So for the first and last i in your loop, you will append a 0 to the very same list.
You could stick to your approach appending a copy of the first element:
list.append(list[0].copy())
The simplest answer is to add the 0's before appending the closing point.
list = [[0,1],[0,2],[1,3],[1,4],[1,5]]
for i in list:
i.append(0)
list.append(list[0])
print(list)
It's the tiniest bit more efficient than a list comprehension because it's not making copies of the elements.
Lets say have an array of the form
r=([[[3,2,1],[5,4,1]],[[10,6,3],[5,3,1]],[[9,5,2],[8,4,1]]])
And I want to do a subtraction between the elements of each array but getting the following array
r=([[[3-3,3-2,3-1],[5-5,5-4,5-1]],[[10-10,10-6,10-3],[5-5,5-3,5-1]],[[9-9,9-5,9-2],[8-8,8-4,8-1]]])
r=([[[0,1,2],[0,1,4]],[[0,4,7],[0,2,4]],[[0,4,7],[0,4,7]]])
I have tried loops inside loops but I don't get what I want because I don't know how to restart the value I'm subtracting in each array.
You can use a nested list comprehension as following:
In [45]: [[[i[0]-j for j in i] for i in sub] for sub in r]
Out[45]: [[[0, 1, 2], [0, 1, 4]], [[0, 4, 7], [0, 2, 4]], [[0, 4, 7], [0, 4, 7]]]
I have a list[5][5] to populate... it looks like a table with 5 columns and 5 rows.
Each cell can be either one or zero.
I want to find different 2^25 possibility that can exist. Each possiblity is a combination of either 0 or 1 in a 5*5 table/list
How can I do that? With nested loop or something?
I suggest you start small... with a 1x1 list first and check that you can display both of the available combinations:
[[0]]
[[1]]
Next up, try a 2x2 list. There are 16 different lists to display:
[[0, 0], [0, 0]]
[[0, 0], [0, 1]]
[[0, 0], [1, 0]]
[[0, 0], [1, 1]]
[[0, 1], [0, 0]]
[[0, 1], [0, 1]]
[[0, 1], [1, 0]]
[[0, 1], [1, 1]]
[[1, 0], [0, 0]]
[[1, 0], [0, 1]]
[[1, 0], [1, 0]]
[[1, 0], [1, 1]]
[[1, 1], [0, 0]]
[[1, 1], [0, 1]]
[[1, 1], [1, 0]]
[[1, 1], [1, 1]]
If you've got the algorithm right for 1x1 and 2x2, then you should be able to generalise it to print your 5x5.
Good luck!
Update
Since you appear to be still struggling, here's a little extra help.
Break this problem into smaller problems. I'd start with generating the values. If you ignore the list notation in my examples above, you'll see that the sequence of values is one that is recognisable to every computer scientist on the planet. It's also pretty easy to generate in Python using bin() and str.zfill().
The second problem is putting them into lists. This isn't too hard either. Supposing the first value in your sequence is '0000'. You know that your lists are two rows by two columns. You can put the first two characters into a list and put that list into a list. Then put the next two characters into a list and append that list to the previous one. Done. Repeat for each value in the sequence.
Hope this helps.
You could try:
import itertools
gen = itertools.product((0,1),repeat=25)
To create a generator to get all of the combinations in 1d and then reshape the data as needed.