Generate a cross dimensional 2-d array from given arrays of ranges - python

Suppose I have a list of lists like this:
list = [[1, 2, 3],
[4, 5, 6, 7],
[3, 4]]
I wish to create a 2-d array like this using the above input in Python:
result = [[1,4,3],[1,4,4],[1,5,3],[1,5,4],[1,6,3],[1,6,4],[1,7,3],[1,7,4],
[2,4,3],[2,4,4],[2,5,3],[2,5,4],[2,6,3],[2,6,4],[2,7,3],[2,7,4],
[3,4,3],[3,4,4],[3,5,3],[3,5,4],[3,6,3],[3,6,4],[3,7,3],[3,7,4]]
This is kind of a truth table.
Note - The length of list can vary (it can contain more lists) and also the length of inner lists can vary.

You can use itertools.product(). l is your original list (avoid using "list" as name, due to the existing built-in data structure in python)
res=[list(i) for i in itertools.product(*l)]
>>> print(res)
[[1, 4, 3], [1, 4, 4], [1, 5, 3], [1, 5, 4], [1, 6, 3], [1, 6, 4], [1, 7, 3], [1, 7, 4], [2, 4, 3], [2, 4, 4], [2, 5, 3], [2, 5, 4], [2, 6, 3], [2, 6, 4], [2, 7, 3], [2, 7, 4], [3, 4, 3], [3, 4, 4], [3, 5, 3], [3, 5, 4], [3, 6, 3], [3, 6, 4], [3, 7, 3], [3, 7, 4]]

Related

How to sum second list from list of list in Python

I would like to sum from list of list as below
array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]],
[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]],
[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]]]
What i want is to sum like below
[1,1,1]+[1,1,1]+[1,1,1] = 9
[2,2,2]+[2,2,2]+[2,2,2] = 18
.... = 27
= 36
= 45
And return a list like below as the final list:
[9,18,27,36,45]
You can use np.sum
a = np.array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]],
[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]],
[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]]])
res = np.sum(a, axis=(0,2))
# Does reduction along axis 0 and 2 by doing summation.
# numpy takes tuple of axis indices to do reduction
# simultaneously along those axis.
print(res.tolist())
>> [ 9, 18, 27, 36, 45]
import numpy as np
lis=np.array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]],
[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]],
[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]]])
print(lis.sum(axis=0).sum(axis=1))
easiest I think with numpy.
output
[ 9 18 27 36 45]
Using zip()
Code:
import numpy as np
lis=np.array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]],
[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]],
[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]]])
res=[]
for i in zip(*lis):
res.append(sum(sum(i)))
print(res)
Output:
[9, 18, 27, 36, 45]
List comprehension:
print([sum(sum(i)) for i in zip(*lis)]) #Same output.
a = [[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]],
[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]],
[[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]]]
result = [sum(x)+sum(y)+sum(z) for x,y,z in zip(a[0], a[1], a[1])]
This is really not 'python-like' solution, but I believe it will suit you well.
import numpy as np
A = np.array([[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5]],
[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5]],
[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5]]])
s = [0 for i in range(len(A[0]))]
for element in A:
for i, ar in enumerate(element):
s[i] += sum(ar)
print(s)

Permutation of 2D matrix

I would like to be able to generate all unique permutations of a 2d array in python and keep the order.
Let's say I have a 2D matrix [[1, 2, 3], [4, 5, 6]]. Expected result should be a in 8 x 3 in the form
[[1, 2, 3],
[1, 2, 6],
[1, 5, 3],
[1, 5, 6],
[4, 2, 3],
[4, 2, 6],
[4, 5, 3],
[4, 5, 6]].
Thanks
Transpose the array, and then use itertools.product:
from itertools import product
list(map(list, product(*zip(*data))))
This outputs:
[[1, 2, 3], [1, 2, 6], [1, 5, 3], [1, 5, 6], [4, 2, 3], [4, 2, 6], [4, 5, 3], [4, 5, 6]]
The itertools method with product would be the most readible way to do this, but since your question is tagged numpy, here is how you can do this using only Numpy methods.
1. Permutations without expected order
You can use this method if order is not important for you. This uses np.meshgrid and np.stack with some .reshape to get the permutations you need, minus the order you expect.
import numpy as np
lst = [[1, 2, 3], [4, 5, 6]]
arr = np.array(lst)
np.stack(np.meshgrid(*arr.T),-1).reshape(-1,3)
array([[1, 2, 3],
[1, 2, 6],
[4, 2, 3],
[4, 2, 6],
[1, 5, 3],
[1, 5, 6],
[4, 5, 3],
[4, 5, 6]])
2. Permutations with expected order
Getting the order to work is a bit "hacky" but a small modification over the above array with simple reordering of columns can solve this with pretty much the same code.
import numpy as np
lst = [[1, 2, 3], [4, 5, 6]]
order = [1,0,2]
arr = np.array(lst)[:,order]
np.stack(np.meshgrid(*arr.T),-1).reshape(-1,3)[:,order]
array([[1, 2, 3],
[1, 2, 6],
[1, 5, 3],
[1, 5, 6],
[4, 2, 3],
[4, 2, 6],
[4, 5, 3],
[4, 5, 6]])

how to delete the short lists from a long nested list even the items are not continuous using Python?

For example:
t=[[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [3, 5], [4, 5, 6],[4,5,6],[6,7], [6], [1]]
I want to delete the short lists if the items are included in a long one, even the items are not continuous. So, I expect the result to be:
[[1, 2, 3, 4, 5, 6],[6,7]]
I might figure out this by myself, but my way is not smart enough. Could anyone help me here?
Since all the elements in a list is unique, AND I like using sets
here's my code. Haven't checked it's efficiency but it looks cleaner :D
t = [[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [3, 5], [4, 5, 6],[4,5,6],[6,7], [6], [1]]
t = [set(l) for l in t]
t = [list(x) for x in t if not any([x.issubset(y) for y in t if x != y])]
Sort from small to large, make them sets then pop them off the list to reduce the list size for every computation.
t=[[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [3, 5], [4, 5, 6],[4,5,6],[6,7], [6], [1]]
t = sorted(t, key=lambda x: len(x))
t = [set(x) for x in t]
for i in range(len(t)):
a = t.pop(0)
if not any([a.issubset(x) for x in t]):
print(a)
My approach is very simple
I check the last element is already present in our longer list. If we present then we don't need to add to the longer list if it is not the case then we will add to the longerlists
sorted_lists=[[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [2, 3, 4, 5, 6], [3, 5], [4, 5, 6],[4,5,6],[6,7], [6], [1]]
sorted_big_lists =[]
for sorted_list in sorted_lists:
for test_list in sorted_big_lists:
if sorted_list[-1] in test_list:
break
else:
sorted_big_lists.append(sorted_list)
print(sorted_big_lists)

Why is my original 2D array being modified in the following code

Im trying to solve a coding problem where i have to generate some matrices, im new to python and i cant figure out why my matrix keeps getting changed to the latest matrix being generated. Here's the code i have so far:
def mirrorHorizontal(m):
l = list(m)
matrix_len = len(l)
for x in range(0,matrix_len):
temp = l[x][matrix_len-1]
l[x][matrix_len-1] = l[x][0]
l[x][0] = temp
return l
def mirrorVertical(m):
l = list(m)
matrix_len = len(l)
for x in range(0,matrix_len):
temp = l[0][x]
l[0][x] = l[matrix_len-1][x]
l[matrix_len-1][x] = temp
return l
def rotateMatrix(m):
l = list(m)
matrix_len = len(l)
rotated_matrix = []
for x in range(0,matrix_len):
rotated_row = []
for y in range(0, matrix_len):
rotated_row.append([(matrix_len-1) -y][y])
rotated_matrix.append(rotated_row)
return rotated_matrix
# Complete the formingMagicSquare function below.
def formingMagicSquare(s):
all_matrices = []
base_matrix = [[8,3,4],[1,5,9],[6,7,2]]
all_matrices.append(base_matrix)
print("all matrices after first append",all_matrices)
base_h_mirror = list(mirrorHorizontal(base_matrix))
print("horizontal_mirror",base_h_mirror)
all_matrices.append(base_h_mirror)
print("all matrices after first h mirror append",all_matrices)
base_v_mirror = list(mirrorVertical(base_matrix))
print("vertical_mirror",base_v_mirror)
all_matrices.append(base_v_mirror)
print("all matrices after first v mirror append",all_matrices)
#Same as vertical mirror of horizontal mirror
base_v_h_mirror = list(mirrorHorizontal(base_v_mirror))
all_matrices.append(base_h_mirror)
print("h_mirror added",all_matrices)
all_matrices.append(base_v_mirror)
print("h_mirror added",all_matrices)
all_matrices.append(base_v_h_mirror)
print("base_v_h_mirror added",all_matrices)
print("mirrored matrices= ",all_matrices)
matrix_len = len(all_matrices)
for x in range(0,matrix_len):
all_matrices.append(rotateMatrix(all_matrices[x]))
print(all_matrices)
formingMagicSquare()
The output , if you run it is something like:
all matrices after first append [[[8, 3, 4], [1, 5, 9], [6, 7, 2]]]
horizontal_mirror [[4, 3, 8], [9, 5, 1], [2, 7, 6]]
all matrices after first h mirror append
[
[[4, 3, 8], [9, 5, 1], [2, 7, 6]],
[[4, 3, 8], [9, 5, 1], [2, 7, 6]]
]
vertical_mirror [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
all matrices after first v mirror append
[[[2, 7, 6], [9, 5, 1], [4, 3, 8]],
[[2, 7, 6], [9, 5, 1], [4, 3, 8]],
[[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
h_mirror added [[[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]]]
h_mirror added [[[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]]]
base_v_h_mirror added [[[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]]]
mirrored matrices= [[[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]]]
Why is my matrix being populated with the same matrix again and again? I converted the list in the functions i used by slicing it [:]
This is also related to mutable and immutable types. You are dealing with lists of lists, which are lists of mutables. So the inner lists are passed by reference, even if you use [:] which makes a shallow copy: a copy of the container, but holding references to the inner object if they are mutables.
That's why, when you change the inner lists, you are changing the same inner lists each time. They are all references to the same lists.
Have a look at the copy docs to more details about shallow copies and deep copies. This will also give you a solution: use a deep copy.
In each function, replace:
l = list(m)
with:
l = copy.deepcopy(m)

Filling an array with arrays or vectors in python using numpy without a loop

I'm trying to find a way to fill an array with rows of values. It's much easier to express my desired output with an example. Given the input of an N x M matrix, array1,
array1 = np.array([[2, 3, 4],
[4, 8, 3],
[7, 6, 3]])
I would like to output an array of arrays in which each row is an N x N consisting of the values from the respective row. The output would be
[[[2, 3, 4],
[2, 3, 4],
[2, 3, 4]],
[[4, 8, 3],
[4, 8, 3],
[4, 8, 3]],
[[7, 6, 3],
[7, 6, 3],
[7, 6, 3]]]
You can reshape the array from 2d to 3d, then use numpy.repeat() along the desired axis:
np.repeat(array1[:, None, :], 3, axis=1)
#array([[[2, 3, 4],
# [2, 3, 4],
# [2, 3, 4]],
# [[4, 8, 3],
# [4, 8, 3],
# [4, 8, 3]],
# [[7, 6, 3],
# [7, 6, 3],
# [7, 6, 3]]])
Or equivalently you can use numpy.tile:
np.tile(array1[:, None, :], (1,3,1))
Another solution which is sometimes useful is the following
out = np.empty((3,3,3), dtype=array1.dtype)
out[...] = array1[:, None, :]

Categories

Resources