I am receiving a output like the below:
y = [[[1,1,1,1,1,1,1] ,[2,2,2,2,2,2,2], [3,3,3,3,3,3,3]],[[4,4,4],[5, 5, 5],[6, 6, 6]]]
the final output should be as below:
Y = [[1, 2, 3], [4, 5, 6]]
How to convert y to Y? [Here, the mean of the innermost array (or optionally any element) need to be used as a basic element and the final array (Y) need to be 2D, whereas the pre-final array (y) is 3D.
(I am new to numpy/python, so this question my look stupid, if any other information is needed, please let me know)
Try this,
result = []
for i in lst:
result.append([j[0] for j in i])
print result
Result
[[1, 2, 3], [4, 5, 6]]
With list comprehension:
y = [[[1,1,1,1,1,1,1] ,[2,2,2,2,2,2,2], [3,3,3,3,3,3,3]],[[4,4,4],[5, 5, 5],[6, 6, 6]]]
Y = [[x[0] for x in z] for z in y] # [[1, 2, 3], [4, 5, 6]]
double loops and insert will do the trick
import numpy as np
y = [[[1, 1, 1, 1, 1, 1, 1 ], [2, 2, 2, 2, 2, 2, 2 ] ,[3, 3, 3, 3, 3, 3, 3]],[[4, 4, 4],[5, 5, 5],[6, 6, 6]]]
Y = []
for l in y:
inner_l = []
for sub_l in l :
inner_l.append(np.mean(sub_l))
Y.append(inner_l)
print Y
y=[[[1,1,1,1,1,1,1] ,[2,2,2,2,2,2,2], [3,3,3,3,3,3,3]],[[4,4,4],[5, 5, 5],[6, 6, 6]]]
rslt = []
for x in y:
lst=[]
for m in x:
lst.append([m[0]])
rslt.append(lst)
Result:
rslt = [[1, 2, 3], [4, 5, 6]]
Related
There are two lists of lists, I need to make one list out of them.
a = [[1,2,3],[4,5,6]]
b = [[1,2,3],[4,5,6]]
I_need = [[1,1,2,2,3,3],[4,4,5,5,6,6]]
or one more question, how to duplicate the list to have the same result.
I will be glad for any help!
As you marked your question with Numpy tag, I assume that you
want to use just Numpy.
To easier tell apart elements of both source arrays, I defined them as:
a = [[ 1, 2, 3],[ 4, 5, 6]]
b = [[10,20,30],[40,50,60]]
To get your expected result, run:
result = np.dstack([a, b]).reshape(2, -1)
The result is:
array([[ 1, 10, 2, 20, 3, 30],
[ 4, 40, 5, 50, 6, 60]])
If you want a plain pythonic list (instead of a Numpy array),
append .tolist() to your code.
If you have python lists:
I_need = [[e for x in zip(*l) for e in x] for l in zip(a,b)]
output: [[1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6]]
If you have numpy arrays:
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[1,2,3],[4,5,6]])
I_need = np.c_[a.ravel(), b.ravel()].reshape(2,-1)
output:
array([[1, 1, 2, 2, 3, 3],
[4, 4, 5, 5, 6, 6]])
Why this code is returning [[2, 3]] instead of [[1, 2, 3], [2, 4, 6], [3, 6, 9]] ?
i = (i for i in range(1, 4))
l = [[x * y for y in i] for x in i]
print(l)
i is generator object, which means the values in it are consumed after the first iteration. In [[x * y for y in i] for x in i] you iterate over it twice, in the seconed time its empty. You can change i to list instead
i = [i for i in range(1, 4)]
l = [[x * y for y in i] for x in i]
print(l)
# output [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
As already noted you created i which is exhaustable, I suggest different fix: just use range directly i.e:
i = range(1, 4)
l = [[x * y for y in i] for x in i]
print(l)
output:
[[1, 2, 3], [2, 4, 6], [3, 6, 9]]
call to range does create instance of class range which might be used multiple times for example
x = range(1,4)
print(list(x))
print(list(x))
print(list(x))
output:
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
I have a list
a = [[1,2,3],[3,4,5]]
In every row at the end I want to insert values from a different list
b=[6,7]
I want the results to be
[[1,2,3,6],[3,4,5,7]]
I am using:
for i in range (0,len(a)):
for j in range (0,len(b)):
if j==0:
a[i].append(b[j])
m.append(a[i])
else:
a[i][3]=b[j]
m.append(a[i])
print m
But I am not getting the expected results. This gives me:
[[1, 2, 3, 7], [1, 2, 3, 7], [3, 4, 5, 7], [3, 4, 5, 7]]
Could someone help me out with the correct code snippet.
Here is a solution using zip:
result = [sublist_a + [el_b] for sublist_a, el_b in zip(a, b)]
which gives the expected output:
[[1, 2, 3, 6], [3, 4, 5, 7]]
Using zip
Ex:
a=[[1,2,3],[3,4,5]]
b=[6,7]
for i, j in zip(a,b):
i.append(j)
print(a)
Output:
[[1, 2, 3, 6], [3, 4, 5, 7]]
I'm very new to python (using python3) and I'm trying to add numbers from one list to another list. The only problem is that the second list is a list of lists. For example:
[[1, 2, 3], [4, 5, 6]]
What I want is to, say, add 1 to each item in the first list and 2 to each item in the second, returning something like this:
[[2, 3, 4], [6, 7, 8]]
I tried this:
original_lst = [[1, 2, 3], [4, 5, 6]]
trasposition_lst = [1, 2]
new_lst = [x+y for x,y in zip(original_lst, transposition_ls)]
print(new_lst)
When I do this, I get an error
can only concatenate list (not "int") to list
This leads me to believe that I can't operate in this way on the lists as long as they are nested within another list. I want to do this operation without flattening the nested list. Is there a solution?
One approach using enumerate
Demo:
l = [[1, 2, 3], [4, 5, 6]]
print( [[j+i for j in v] for i,v in enumerate(l, 1)] )
Output:
[[2, 3, 4], [6, 7, 8]]
You can use enumerate:
l = [[1, 2, 3], [4, 5, 6]]
new_l = [[c+i for c in a] for i, a in enumerate(l, 1)]
Output:
[[2, 3, 4], [6, 7, 8]]
Why don't use numpy instead?
import numpy as np
mat = np.array([[1, 2, 3], [4, 5, 6]])
mul = np.array([1,2])
m = np.ones(mat.shape)
res = (m.T *mul).T + mat
You were very close with you original method. Just fell one step short.
Small addition
original_lst = [[1, 2, 3], [4, 5, 6]]
transposition_lst = [1, 2]
new_lst = [[xx + y for xx in x] for x, y in zip(original_lst, transposition_lst)]
print(new_lst)
Output
[[2, 3, 4], [6, 7, 8]]
Reasoning
If you print your original zip it is easy to see the issue. Your original zip yielded this:
In:
original_lst = [[1, 2, 3], [4, 5, 6]]
transposition_lst = [1, 2]
for x,y in zip(original_lst, transposition_lst):
print(x, y)
Output
[1, 2, 3] 1
[4, 5, 6] 2
Now it is easy to see that you are trying to add an integer to a list (hence the error). Which python doesn't understand. if they were both integers it would add them or if they were both lists it would combine them.
To fix this you need to do one extra step with your code to add the integer to each value in the list. Hence the addition of the extra list comprehension in the solution above.
A different approach than numpy that could work even for lists of different lengths is
lst = [[1, 2, 3], [4, 5, 6, 7]]
c = [1, 2]
res = [[l + c[i] for l in lst[i]] for i in range(len(c))]
Let's say I'm not allowed to use libraries.
How do I go about calculating the product of indexes in a list.
Let's assume none of the integers are 0 or less.
The problem gets harder as I'm trying to calculate the indexes vertically.
bigList = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
With numpy the solution for my problem would be:
import numpy as np
print([np.prod(l) for l in zip(*bigList)])
[1, 32, 243, 1024, 3125]
However without it my solution is much more chaotic:
rotateY = [l for l in zip(*bigList)]
productList = [1]* len(bigList)
count = 0
for l in rotateY:
for i in l:
productList[count] *= i
count += 1
print(productList)
[1, 32, 243, 1024, 3125]
You can iterate over each row getting each row's n-th element, and multiplying each element together:
>>> from functools import reduce
>>>
>>> def mul_lst(lst):
return reduce(lambda x, y: x * y, lst)
>>>
>>> bigList = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
>>>
>>> [mul_lst([row[i] for row in bigList]) for i in range(len(bigList))]
[1, 32, 243, 1024, 3125]
If you cannot use any libraries, including functools, you can write the logic for the mul_lst function manually:
>>> def mul_lst(lst):
product = lst[0]
for el in lst[1:]:
product *= el
return product
>>> mul_lst([3, 3])
9
>>> mul_lst([2, 2, 2, 2, 2])
32
And why not simply:
productList = []
for i in range(len(bigList[0]):
p = 1
for row in bigList:
p *= row[i]
productList.append(p)
Alternatively, a small improvement over your solution:
productList = [1]* len(bigList[0])
for row in bigList:
for i, c in enumerate(row):
productList[i] *= c
We can transpose the nested list and then use reduce (a Python built-in) in Python 2.x on each element (list) for a one-liner -
>>> [reduce(lambda a,b: a*b, i) for i in map(list, zip(*bigList))]
[1, 32, 243, 1024, 3125]
Here's a quick recursive solution
def prod(x):
""" Recursive approach with behavior of np.prod over axis 0 """
if len(x) is 1:
return x
for i, a_ in enumerate(x.pop()):
x[0][i] *= a_
return prod(x)