X =[
[2, 10, 3, 1],
[4, 2, 0, 7],
[6, 9, 6, 9],
[1, 3, 4, 5]
]
Y =[
[4, 2, 3, 1],
[9, 9, 9, 7],
[1, 2, 3, 4],
[5, 6, 7, 8]
]
So i have to add these two matrixes. I solved it, but i have to create counter of iterations.
def summary(X, Y):
return [X[i][j] + Y[i][j] for j in range(len(X[0]))] for i in range(len(X))]
I was searching for the solution and also trying to figure it out by myself, but i have no idea what to do if there is already two loops inside this list.
Desired output:
[6, 12, 6, 2]
[13, 11, 9, 14]
[7, 11, 9, 13]
[6, 9, 11, 13]
number of iterations: 16
import numpy as np
np.reshape(np.array(X) + np.array(Y), (4,4)).tolist()
# output
# [[6, 12, 6, 2], [13, 11, 9, 14], [7, 11, 9, 13], [6, 9, 11, 13]]
You can use a nested list comprehension to do element-wise addition
>>> [[i+j for i,j in zip(a, b)] for a,b in zip(X,Y)]
[[6, 12, 6, 2],
[13, 11, 9, 14],
[7, 11, 9, 13],
[6, 9, 11, 13]]
In general (unless this is a learning exercise) I'd suggest using numpy as it is significantly faster for linear algebra operations
>>> import numpy as np
>>> np.array(X) + np.array(Y)
array([[ 6, 12, 6, 2],
[13, 11, 9, 14],
[ 7, 11, 9, 13],
[ 6, 9, 11, 13]])
If you wanted to do this "by hand" while keeping a counter you could do something like
def add(X,Y):
ops = 0
result = []
for a, b in zip(X,Y):
row = []
for i,j in zip(a, b):
row.append(i + j)
ops += 1
result.append(row)
print('number of iterations: {}'.format(ops))
return result
>>> add(X, Y)
number of iterations: 16
[[6, 12, 6, 2], [13, 11, 9, 14], [7, 11, 9, 13], [6, 9, 11, 13]]
Related
This question already has answers here:
What is the best way to perform an anti-transpose in python?
(4 answers)
Closed 4 months ago.
Does anyone could tell me how to reorder the matrix:
[[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]]
To:
[[15, 10, 5],
[14, 9, 4],
[13, 8, 3],
[12, 7, 2],
[11, 6, 1]]
Since you have tagged the question numpy, I surmise that those are numpy matrix, and you are looking for a numpy solution (otherwise, if those are lists, Ann's zip is the correct solution).
For numpy you can
M[::-1,::-1].T
Example
M=np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
M[::-1,::-1].T
returns
array([[15, 10, 5],
[14, 9, 4],
[13, 8, 3],
[12, 7, 2],
[11, 6, 1]])
as expected
I'd suggest using numpy, like so:
import numpy as np
matrix = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
transformed_matrix = matrix[::-1].T[::-1]
# array([[15, 10, 5],
# [14, 9, 4],
# [13, 8, 3],
# [12, 7, 2],
# [11, 6, 1]])
matrix[::-1] gives you the original matrix in reverse order (i.e. [11, 12, 13...] first and [1, 2, 3...] last).
Taking the transpose of that with .T rotates the matrix about - swapping rows and columns.
Lastly, indexing the transpose with [::-1] reverses the order, putting [15, 14, 13...] first and [5, 4, 3...] last.
Here is how you can use the zip() method to transpose your matrix:
m = [[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]]
print([list(i)[::-1] for i in zip(*m)][::-1])
Output:
[[15, 10, 5],
[14, 9, 4],
[13, 8, 3],
[12, 7, 2],
[11, 6, 1]]
The index [::-1] reverses the array.
Let say I have an array of
x = [[5, 9, 7, 17, 13, 0], [8, 10, 16, 35, 7, 5], [10, 17, 5]]
I want to sort the value so that it will look like this:
x = [[0, 5, 5, 5, 7, 7], [8, 9, 10, 10, 13, 16], [17, 17, 35]]
How could I achieve this?
code:
x = [[5, 9, 7, 17, 13, 0], [8, 10, 16, 35, 7, 5], [10, 17, 5]]
y = sorted(sum([i for i in x],[]))
result = []
for i in x:
temp = []
for j in range(len(i)):
temp.append(y.pop(0))
result.append(temp)
print(result)
result:
[[0, 5, 5, 5, 7, 7], [8, 9, 10, 10, 13, 16], [17, 17, 35]]
The best answer is given by #leaf_yakitori please look at it. Now I just added this code to reduce time complexity from O(n^2) to O(n)
x = [[5, 9, 7, 17, 13, 0], [8, 10, 16, 35, 7, 5], [10, 17, 5]]
y = sorted(sum([i for i in x],[]))
result = []
ind = 0
for i in x:
result.append(y[ind:ind + len(i)])
ind += len(i)
print(result)
I have lists of the following form:
a = [[1, 2, 3, [4, 5, 6, 7, [8, 9, [10, 11, 12]]]]]
These lists can be of varying "depth" and can have a varying number of elements at each level (i.e. the outer list can have a different number of elements than its inner).
How can I convert this into a list of lists with a depth of 1:
a = [[1, 2, 3], [4, 5, 6, 7], [8, 9], [10, 11, 12]]
You can try a recursive approach:
def listOfList(lst):
b = []
def recurs(lst):
c = []
for i in lst:
if isinstance(i, list):
if c:
b.append(c)
c = []
recurs(i)
else:
c.append(i)
if c: b.append(c)
return b
return recurs(lst)
Example:
>>> a = [[1, 2, 3, [4, 5, 6, 7, [8, 9, [10, 11, 12]]]]]
>>> listOfList(a)
[[1, 2, 3], [4, 5, 6, 7], [8, 9], [10, 11, 12]]
>>> a = [[[1, 2, 3, [4, 5, 6, 7, [8, 9, [10, 11, 12, [13, 14]]]]]]]
>>> listOfList(a)
[[1, 2, 3], [4, 5, 6, 7], [8, 9], [10, 11, 12], [13, 14]]
This is also useful in case the list is not nested in proper way:
>>> a = [[[1, 2, 3, [4, 5, 6, 7, [8, 9, [10, 11, 12, [13, 14]]]]]], [15, 16]]
>>> listOfList(a)
[[1, 2, 3], [4, 5, 6, 7], [8, 9], [10, 11, 12], [13, 14], [15, 16]]
>>> a = [[[1, 2, 3, [4, 5, 6, 7, [8, 9, [10, 11, 12, [13, 14]]]]]], [15, 16], 17, 18, 19]
>>> listOfList(a)
[[1, 2, 3], [4, 5, 6, 7], [8, 9], [10, 11, 12], [13, 14], [15, 16], [17, 18, 19]]
a = [[1, 2, 3, [4, 5, 6, 7, [8, 9, [10, 11, 12]]]]]
new_a = []
while isinstance(a, list):
last = a.pop()
if a:
new_a.append(a)
a = last
print(new_a)
You just need a recursion to ergodic your nested list.
def func(c):
d = []
if isinstance(c[-1], list):
return [c[:-1], *func(c[-1])]
else:
return [c]
print(func(a))
And the output is:
>>> [[], [1, 2, 3], [4, 5, 6, 7], [8, 9], [10, 11, 12]]
There is a empty list at first because the top level of your lists only have one item. To convert it as you want, you can use another for loop:
for b in a:
print(func(b))
Then the result is what you want:
>>> [[1, 2, 3], [4, 5, 6, 7], [8, 9], [10, 11, 12]]
I'm having trouble figuring out a code to write that will merge a two dimensional list to a three dimensional list. For example, "a" is two dimensional list and "b" is three dimensional list that i would like to merge them two together and sort in order. If this is possible can this be done with witting a list comprehension? For another example here is my two list below.
a = [[7, 28],[28],[28]]
b = [[[3, 9],[3, 9],[3, 9]],[[3, 4],[4, 7],[4, 7]],[[7, 11],[3, 11],[3, 7, 12]]]
I would like my result to be
c = [[[3, 7, 9, 28],[3, 7, 9, 28],[3, 7, 9, 28]],
[[3, 4, 28],[4, 7, 28],[4, 7,28]],
[[7, 11, 28],[3, 11, 28],[3, 7, 12, 28]]]
In case you fancy a oneliner:
result = [[sorted(i+x) for i in y] for x, y in zip(a,b)]
print (result)
#[[[3, 9, 7, 28], [3, 9, 7, 28], [3, 9, 7, 28]], [[3, 4, 28], [4, 7, 28], [4, 7, 28]], [[7, 11, 28], [3, 11, 28], [3, 7, 12, 28]]]
This should work:
c = [[sorted(i+x) for i in y] for x, y in zip(a,b)]
print(c)
#[[[3, 7, 9, 28],[3, 7, 9, 28],[3, 7, 9, 28]],[[3, 4, 28],[4, 7, 28],[4, 7,28]],[[7, 11, 28],[3, 11, 28],[3, 7, 12, 28]]
another 1-liner, using enumerate instead of zip:
c = [[sorted(s1+a[i]) for s1 in s0] for i, s0 in enumerate(b)]
Try this :
c = [[sorted(i+k) for k in j] for i,j in zip(a,b)]
Output :
[[[3, 7, 9, 28], [3, 7, 9, 28], [3, 7, 9, 28]],
[[3, 4, 28], [4, 7, 28], [4, 7, 28]],
[[7, 11, 28], [3, 11, 28], [3, 7, 12, 28]]]
Here is another way without one-liner :
c = b[:]
for i,j in zip(a,c):
for k in j:
k.extend(i)
k.sort()
i = [1,2,3,4,5,6,7,8,9,10]
def ndiv(l,n):
return [l[s:e] for s, e in zip(range(0,len(l)+1,n),xrange(n,len(l)+1,n))]
for i in xrange(1,15,1):
print "CLUSTER {}".format((ndiv(l,i)))
#print
CLUSTER [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17]]
CLUSTER [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14], [15, 16]]
CLUSTER [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]
CLUSTER [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
CLUSTER [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
CLUSTER [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
CLUSTER [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14]]
CLUSTER [[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16]]
CLUSTER [[1, 2, 3, 4, 5, 6, 7, 8, 9]]
Coding up to now. After dividing, the remaining values are not shown omitted.But I want to have the prices shown after we split them(EX.if divide '3' ->before [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]] / after [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15],[16],[17] ).How can I divide and then show the remaining numbers one by one?
You can compute 2 lists, one that splits contains batches of n and the other that maps remaining elements into one element list and finally merge them together
>>> l = list(xrange(1,18))
>>> def ndiv(l,n):
... return [l[i:i+n] for i in xrange(0,len(l)//n*n, n)] + [[e] for e in l[len(l)//n*n:]]
...
>>> ndiv(l, 5)
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16], [17]]