Say I have the following two arrays, a and b:
import numpy as np
a = np.array([[[1, 0],
[1, 1]],
[[1, 0],
[0, 0]],
[[0, 0],
[1, 0]]])
b = np.array([[[0, 2],
[0, 0]],
[[0, 0],
[0, 2]],
[[0, 2],
[0, 2]]])
and I wish to 'overlap' them so that I get the following result:
[[[1, 2],
[1, 1]],
[[1, 0],
[0, 2]],
[[0, 2],
[1, 2]]]
In the case there is an overlapping co-ordinate, I would just take 1. How could I achieve this?
Maybe you can start with a matrix with zeros and then assign the flags one by one:
import numpy as np
a = np.array([[[1, 0],
[1, 1]],
[[1, 0],
[0, 0]],
[[0, 0],
[1, 0]]])
b = np.array([[[0, 2],
[0, 0]],
[[0, 0],
[0, 2]],
[[0, 2],
[0, 2]]])
# Create a matrix with zeros
c = np.zeros(a.shape, dtype='int')
# Assign flags
c[b==2] = 2
c[a==1] = 1 # Put in second because priority to 1 in case of overlapping
# Output
print(c)
Output:
array([[[1, 2],
[1, 1]],
[[1, 0],
[0, 2]],
[[0, 2],
[1, 2]]])
Using a simple sum I manage to get the desired result:
import numpy as np
a = np.array([[[1, 0],
[1, 1]],
[[1, 0],
[0, 0]],
[[0, 0],
[1, 0]]])
b = np.array([[[0, 2],
[0, 0]],
[[0, 0],
[0, 2]],
[[0, 2],
[0, 2]]])
print(a+b)
Output:
[[[1 2]
[1 1]]
[[1 0]
[0 2]]
[[0 2]
[1 2]]]
Related
for example I have this list:
lst = [[0, 1], [0, 1], [0, 1], [1, 0], [1, 0], [1, 0]]
and I shuffle it for example with seed = 42:
random.seed(42)
random.shuffle(lst)
I took this list after shuffle:
[[1, 0], [0, 1], [0, 1], [1, 0], [0, 1], [1, 0]]
I want to take oposite of this shuffle too
How can I take this list:
[[0, 1], [1, 0], [1, 0], [0, 1], [1, 0], [0, 1]]
I want oposite seed
I am trying to manipulate array2 so that the row and column is dependent on the len of the strings as for array1
str1 = "Hi"
str2 = "Bye"
array1 = [[[0, 0] for y in range(len(str2)+1)] for x in range(len(str1)+1)]
print(array1)
#output: [[[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]]]
array2 = [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
#want array2 to implement same format as array1 where the row and column is determined by the len of str1 and str2
temp = [[[array2[i], array2[j]] for y in range(len(str2)+1)] for x in range(len(str1)+1)] #does not work
I tried to remove some brackets from temp however, did not work.
I tried to manipulate the method I used for array1, but did not work. I was expecting the rows and columns to be dependent on the len of the strings as for array2.
The current code has no idea what to do with array2[i], array2[j] cause neither i nor j have been defined.
This code is working as expected (I've organized the output for better readability):
str1 = "Hi"
str2 = "Bye"
array1 = [[[0, 0] for y in range(len(str2)+1)] for x in range(len(str1)+1)]
print(array1)
#output is 4x3: [
#[[0, 0], [0, 0], [0, 0], [0, 0]],
#[[0, 0], [0, 0], [0, 0], [0, 0]],
#[[0, 0], [0, 0], [0, 0], [0, 0]]]
array2 = [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]]
temp = [[[0, 0] for y in range(len(str2)+1)] for x in range(len(str1)+1)]
#output is 4x3: [
#[[0, 0], [0, 0], [0, 0], [0, 0]],
#[[0, 0], [0, 0], [0, 0], [0, 0]],
#[[0, 0], [0, 0], [0, 0], [0, 0]]]
If you want to use a certain set of numbers from the array, you need to change the code to:
i, o = 0, 1
temp = [[[array2[i], array2[j]] for y in range(len(str2)+1)] for x in range(len(str1)+1)]
If this doesn't solve your problem, please provide further explanation
Say, I have a 2-dimensional list/array that contains 50 subarrays that look like [0, 0]. I want to change only some parts of the array, say the first elements of each subarray from index 15 to 29. I used a for loop to do this.
array = [[0,0]] * 50
for i in range(15, 30):
array[i][0] = 1
But when I print(array), it seems like the program changes all the first elements of each subarray. Output:
[[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0]]
I do not know what I am doing wrong, as the logic seems sound. Can anyone identify my mistake?
array = [[0,0]] * 50 By this, your array will have [0,0] 50 times referencing to same [0,0]. So modifying one will modify all of them.
Instead try this: array = [[0, 0] for _ in range(50)].
I have a list having sublists of numbers and want to extract specific ones. In my simplified example I have two main sublists and each one has its own pairs of numbers:
data=[[[1, 0], [2, 0], [2, 1], [2, 2],\
[1, 0], [1, 1], [1, 2],\
[0, 1], [0, 2], [0, 3]],\
[[1, 0], [2, 0],\
[1, 0],\
[0, 1], [0, 2], [1, 2],\
[1, 0], [1, 1], [1, 1]]]
Pairs stored in data can be divided based on some rules and I want the last pair of each division. For simplicity I have shown each division as a row in data. Each division starts with [1, 0] or [0, 1] and these two pairs are break points. Then, simply I want the last pair before each break points. In cases I may have no point between two break points and I only export the previous break point. Finally I want it as the following list:
data=[[[2, 2],\
[1, 2],\
[0, 3]],\
[[2, 0],\
[1, 0],\
[1, 2],\
[1, 1]]]
You can do the following, using enumerate:
def fun(lst):
return [p for i, p in enumerate(lst) if i==len(lst)-1 or set(lst[i+1])=={0,1}]
[*map(fun, data)]
# [[[2, 2], [1, 2], [0, 3]], [[2, 0], [1, 0], [1, 2], [1, 1]]]
fun filters a nested list for all elements that are either last or succeeded by [0, 1] or [1, 0].
data=[[[1, 0], [2, 0], [2, 1], [2, 2],
[1, 0], [1, 1], [1, 2],
[0, 1], [0, 2], [0, 3]],
[[1, 0], [2, 0],
[1, 0],
[0, 1], [0, 2], [1, 2],
[1, 0], [1, 1], [1, 1]]]
newData = []
for subarray in data:
new_subarray = []
for i,item in enumerate(subarray):
if item == [0,1] or item == [1,0]:
if i> 0:
new_subarray.append(subarray[i-1])
if i == len(subarray)-1:
new_subarray.append(item)
newData.append(new_subarray)
print(newData)
Here is a fun little unreadable numpy oneliner:
import numpy as np
[np.array(a)[np.roll(np.flatnonzero(np.logical_or(np.all(np.array(a)==(1, 0), axis=1), np.all(np.array(a)==(0, 1), axis=1)))-1, -1)].tolist() for a in data]
# [[[2, 2], [1, 2], [0, 3]], [[2, 0], [1, 0], [1, 2], [1, 1]]]
It works but in reality you'd better use schwobaseggl's solution.
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 6 years ago.
c_k_list = [[0, 0]]*(sorted_degrees[len(sorted_degrees)-1]+1)
c_k_list[entry[1]][0] = c_k_list[entry[1]][0]+1
where entry[1]=1
In the above statement, instead of adding 1 to a particular element in c_k_list, it adds 1 to all the rows.
Eg:
c_k_list is
[[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0]]
instead of
[[0,0], [1,0], [0,0]......[0,0]]
Lists are objects, and so are stored by reference. Using * will just create copies of that reference. To correct this try:
c_k_list = [[0, 0] for i in range(5)]
c_k_list[1][0] = c_k_list[1][0]+1