I am a beginner in Python.
I want to create the matrix below, how should I create it?
[
[0,1], [0,2], [0,3],
[1,1], [1,2], [1,3],
[2,1], [2,2], [2,3],
[3,1], [3,2], [3,3]
]
I looked up numpy, maybe I'm not looking in the right way, I didn't find any good way.
This is almost what numpy.ndindex is doing, except you want one of the values to start with 1. You can fix it by converting to array and adding 1:
np.array(list(np.ndindex(4,3)))+[0,1]
Output:
array([[0, 1],
[0, 2],
[0, 3],
[1, 1],
[1, 2],
[1, 3],
[2, 1],
[2, 2],
[2, 3],
[3, 1],
[3, 2],
[3, 3]])
A rather simple list comprehension will generate this data structure. Numpy not required.
[[x, y] for x in range(4) for y in range(1, 4)]
Result:
[[0, 1], [0, 2], [0, 3],
[1, 1], [1, 2], [1, 3],
[2, 1], [2, 2], [2, 3],
[3, 1], [3, 2], [3, 3]]
Related
I have a vector containing 15 values and would like to find all the possible ways to partition the vector into 3 equally sized partitions. I know there is n!/(n-r)!r! combinations to take r values out of a list of n values with replacement & this is easily generated with itertools in Python.
Does there exist an easy solution to list all combinations in this case as well?
Mathematically there will be n!/((n/3!)^3)/3! solutions, for example if n=15 there will be 126126 combinations and if n=6 there will be 15 combinations.
As the task needs to remove duplicates, which is not supported by itertools, I would recommend package more_itertools:
import more_itertools
n = 6
assert n%3 == 0
[x for x in more_itertools.set_partitions(range(n), 3) if len(x[0]) == len(x[1]) == len(x[2])]
[[[0, 1], [2, 3], [4, 5]],
[[0, 1], [3, 4], [2, 5]],
[[0, 1], [2, 4], [3, 5]],
[[1, 2], [0, 3], [4, 5]],
[[0, 2], [1, 3], [4, 5]],
[[1, 2], [3, 4], [0, 5]],
[[0, 2], [3, 4], [1, 5]],
[[1, 2], [0, 4], [3, 5]],
[[0, 2], [1, 4], [3, 5]],
[[2, 3], [1, 4], [0, 5]],
[[2, 3], [0, 4], [1, 5]],
[[1, 3], [2, 4], [0, 5]],
[[0, 3], [2, 4], [1, 5]],
[[1, 3], [0, 4], [2, 5]],
[[0, 3], [1, 4], [2, 5]]]
I'd like to know how to sort the lists in the list. However, I don't want to align by key. I'd like to change it according to the following method.
arr = [[2, 3], [5, 1], [4, 1], [5, 3], [4, 2]]
# solution...
I_want_arr = [[2, 3], [1, 5], [1, 4], [3, 5], [2, 4]]
i tried it
for i in arr:
i.sort()
but, it didn't work
using list comprehenstion:
arr = [[2, 3], [5, 1], [4, 1], [5, 3], [4, 2]]
sorted_output = [sorted(l) for l in arr]
using map():
sorted_output = list(map(sorted, arr))
#Gabip's solution includes this and a more time efficient one, check that out first!
How about
arr = [[2, 3], [5, 1], [4, 1], [5, 3], [4, 2]]
I_want_arr = [sorted(x) for x in arr]
This outputs
[[2, 3], [1, 5], [1, 4], [3, 5], [2, 4]]
Here's the list I want to break:
List A = [[[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]]], [[[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]]
List A has 2 sublists, each of which contains 3 pairs of coordinates. I'm wondering if I could keep the order of those coordinates, but regroup a pair of coordinate as a sublist. So here's the desired output:
List B = [[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]
Thanks!!
You can convert it to a numpy array, reshape it, and then convert it back.
import numpy as np
A = [[[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]]], [[[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]]
npA = np.array(A)
B = npA.reshape(6, 2, 2).tolist()
Or, if you want it to generalize to different input sizes
B = npA.reshape(npA.size // 4, 2, 2).tolist()
As for your specific question, we can get B from A[0]+A[1]
>>> A = [[[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]]], [[[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]]
>>> B = [[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]
>>> A[0] + A[1]
[[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]
>>> A[0] + A[1] == B
True
For your specific requirement, You can take the first element of the list A and extend it with the second element of the list A
B = A[0]
B.extend(A[1])
I'm working with list comprehension but I'm having a trouble working this out, so, I have a 3D list in which I'm trying to obtain pairs in the inner lists, I created a code, in which I can obtain pairs, but it's not exactly what I need, here is my code:
mylist = [[[3, 2, 4, 3], [3, 2, 1], [2, 1]], [[1, 2, 3], [3, 1], [2, 1]]]
res = [[x[idx: idx+2] for i in mylist for x in i for idx in range(0, len(x) - 1)]]
print(res)
#res = [[[3, 2], [2, 4], [4, 3], [3, 2], [2, 1], [2, 1], [1, 2], [2, 3], [3, 1], [2, 1]]]
As you can see, I do get a 3D list with the pairs, but, it's not separated, its just a plain 3D list, I was expecting this:
#Output
res = [[[3, 2], [2, 4], [4, 3], [3, 2], [2, 1], [2, 1]], [[1, 2], [2, 3], [3, 1], [2, 1]]]
# ^
# Here is the separation
I'm working on my list comprehension, but I can't see where is happening the problem, I believe there is something wrong with the bracket, but I been trying different combinations but nothing seems to work, so any help will be appreciated.
Also, maybe this is bit of a stretch, but there is some way I can eliminate some repeated inner list in the 3D list, I mean, using res to get:
newres = [[[3, 2], [2, 4], [4, 3], [2, 1]], [[1, 2], [2, 3], [3, 1], [2, 1]]]
#[3, 2], [2, 1] eliminated
If you can point me to the right direction that would be great, thank you so much!
[[x[idx: idx+2] for x in i for idx in range(0, len(x) - 1)] for i in mylist ]
Sorry that I am not good at writing nested loops in one line. But this will remove duplicates and creates a 3D list with pairs:
mylist = [[[3, 2, 4, 3], [3, 2, 1], [2, 1]], [[1, 2, 3], [3, 1], [2, 1]]]
res = []
for inner in mylist:
temp = []
for each in inner:
for e in zip(each, each[1:]):
if list(e) not in temp:
temp.append(list(e))
res.append(temp)
print(res) # [[[3, 2], [2, 4], [4, 3], [2, 1]], [[1, 2], [2, 3], [3, 1], [2, 1]]]
I have an matrix represented by a np array. Here is an example of what I am talking about. You can see it has 3 "vectors" inside of it
x = np.array([[1, 1], [1,2],[2,3]])
[1, 1], [1,2] and [2,3]
The goal is to turn this into a matrix where these vectors are repeated. So the 0th row of said matrix should simply be [1,1] repeated n times. And the 1st row should be [1,2] repeated n times. I believe this would look somewhat like for n=4
xresult = np.array([[[1, 1], [1, 1], [1, 1], [1, 1]],
[[1, 2], [1, 2], [1, 2], [1, 2]],
[[2, 3], [2, 3], [2, 3], [2, 3]]])
And therefore
xresult[0,0] = [1,1]
xresult[0,1] = [1,1]
xresult[0,2] = [1,1]
xresult[1,2] = [1,2]
The goal is of course to do this without loops if possible as that is an obvious but perhaps less elegant/performant solution.
Here are some attempts that do not work
np.tile([x],(2,1))
>>>array([[[1, 1],
[1, 2],
[2, 3],
[1, 1],
[1, 2],
[2, 3]]])
np.tile([x],(2,))
>>>array([[[1, 1, 1, 1],
[1, 2, 1, 2],
[2, 3, 2, 3]]])
np.append(x,x,axis=0)
>>>array([[1, 1],
[1, 2],
[2, 3],
[1, 1],
[1, 2],
[2, 3]])
np.append([x],[x],axis=1)
>>>array([[[1, 1],
[1, 2],
[2, 3],
[1, 1],
[1, 2],
[2, 3]]])
np.array([[x],[x]])
>>>array([[[[1, 1],
[1, 2],
[2, 3]]],
[[[1, 1],
[1, 2],
[2, 3]]]])
(Some of these were just with n=2 as a goal)
It is worth noting that the ultimate end goal is to take x and y (a similarly crafted array of vectors of the same dimension but not necessarily the same number of vectors
y = np.array([[99,11], [23,44],[33,44], [2, 1], [9, 9]])
And run the procedure on x so that columns of the result are the number of vectors in y. And run a procedure on y that is similar but does this row-wise.
y after this transform would have the following
yresult[0,0] = [99,11]
yresult[1,0] = [22,44]
yresult[2,0] = [33,44]
yresult[2,1] = [33,44]
This way I can subtract the two matrices. The goal is to create a matrix where x'vector index is the row, y'vector index is the row and the element is the difference between these two vectors.
ultimateResult[0,1]=[1,1]-[23,44]=[-22,-43]
Perhaps there is a better way to get this.