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]])
INPUT:
M = [[1,2,3],
[1,2,3],
[1,2,3]]
how take the sum of the columns in the two first rows and implement it in the array M
OUTPUT:
M = [[2,4,6],
[1,2,3]]
Use numpy.add.reduceat:
import numpy as np
M = [[1,2,3],
[1,2,3],
[1,2,3]]
np.add.reduceat(M, [0, 2])
# indices [0,2] splits the list into [0,1] and [2] to add them separately,
# you can see help(np.add.reduceat) for more
# array([[2, 4, 6],
# [1, 2, 3]])
M = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
M[:2] = [[a + b for a, b in zip(M[0], M[1])]]
print(M) # [[5, 7, 9], [7, 8, 9]]
Things to google to understand this:
M[:2] =: python slice assignment
[... for .. in ...]: python list comprehension
for a, b in ...: python tuple unpacking loop
I have List = [0, 1, 2, 3] and I want to assign all of them a number say, 5: List = [5, 5, 5, 5]. I know I can do List = [5] * 4 easily. But I also want to be able to assign say, 6 to the last 3 elements.
List = [5, 5, 5, 5]
YourAnswer(List, 6, 1, 3)
>> [5, 6, 6, 6]
where YourAnswer is a function.
Anything similar would be helpful. Prefer to avoid for-loop. But if there is no known possibility, please tell me.
You can actually use the slice assignment mechanism to assign to part of the list.
>>> some_list = [0, 1, 2, 3]
>>> some_list[:1] = [5]*1
>>> some_list[1:] = [6]*3
>>> some_list
[5, 6, 6, 6]
This is beneficial if you are updating the same list, but in case you want to create a new list, you can simply create a new list based on your criteria by concatenation
>>> [5]*1 + [6]*3
[5, 6, 6, 6]
You can wrap it over to a function
>>> def YourAnswer(lst, repl, index, size):
lst[index:index + size] = [repl] * size
>>> some_list = [0, 1, 2, 3]
>>> YourAnswer(some_list, 6, 1, 3)
>>> some_list
[0, 6, 6, 6]
In [138]: def func(lis,x,y,z):
.....: return lis[:y]+[x]*z
.....:
In [139]: lis=[5,5,5,5]
In [140]: func(lis,6,1,3)
Out[140]: [5, 6, 6, 6]
you can use + to implement it
def createList(val1, n1, val2, n2):
return [val1] * n1 + [val2]*n2
Does anybody know how to extract a column from a multi-dimensional array in Python?
>>> import numpy as np
>>> A = np.array([[1,2,3,4],[5,6,7,8]])
>>> A
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>> A[:,2] # returns the third columm
array([3, 7])
See also: "numpy.arange" and "reshape" to allocate memory
Example: (Allocating a array with shaping of matrix (3x4))
nrows = 3
ncols = 4
my_array = numpy.arange(nrows*ncols, dtype='double')
my_array = my_array.reshape(nrows, ncols)
Could it be that you're using a NumPy array? Python has the array module, but that does not support multi-dimensional arrays. Normal Python lists are single-dimensional too.
However, if you have a simple two-dimensional list like this:
A = [[1,2,3,4],
[5,6,7,8]]
then you can extract a column like this:
def column(matrix, i):
return [row[i] for row in matrix]
Extracting the second column (index 1):
>>> column(A, 1)
[2, 6]
Or alternatively, simply:
>>> [row[1] for row in A]
[2, 6]
If you have an array like
a = [[1, 2], [2, 3], [3, 4]]
Then you extract the first column like that:
[row[0] for row in a]
So the result looks like this:
[1, 2, 3]
check it out!
a = [[1, 2], [2, 3], [3, 4]]
a2 = zip(*a)
a2[0]
it is the same thing as above except somehow it is neater
the zip does the work but requires single arrays as arguments, the *a syntax unpacks the multidimensional array into single array arguments
>>> x = arange(20).reshape(4,5)
>>> x array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
if you want the second column you can use
>>> x[:, 1]
array([ 1, 6, 11, 16])
If you have a two-dimensional array in Python (not numpy), you can extract all the columns like so,
data = [
['a', 1, 2],
['b', 3, 4],
['c', 5, 6]
]
columns = list(zip(*data))
print("column[0] = {}".format(columns[0]))
print("column[1] = {}".format(columns[1]))
print("column[2] = {}".format(columns[2]))
Executing this code will yield,
>>> print("column[0] = {}".format(columns[0]))
column[0] = ('a', 'b', 'c')
>>> print("column[1] = {}".format(columns[1]))
column[1] = (1, 3, 5)
>>> print("column[2] = {}".format(columns[2]))
column[2] = (2, 4, 6)
def get_col(arr, col):
return map(lambda x : x[col], arr)
a = [[1,2,3,4], [5,6,7,8], [9,10,11,12],[13,14,15,16]]
print get_col(a, 3)
map function in Python is another way to go.
array = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
col1 = [val[1] for val in array]
col2 = [val[2] for val in array]
col3 = [val[3] for val in array]
col4 = [val[4] for val in array]
print(col1)
print(col2)
print(col3)
print(col4)
Output:
[1, 5, 9, 13]
[2, 6, 10, 14]
[3, 7, 11, 15]
[4, 8, 12, 16]
[matrix[i][column] for i in range(len(matrix))]
The itemgetter operator can help too, if you like map-reduce style python, rather than list comprehensions, for a little variety!
# tested in 2.4
from operator import itemgetter
def column(matrix,i):
f = itemgetter(i)
return map(f,matrix)
M = [range(x,x+5) for x in range(10)]
assert column(M,1) == range(1,11)
You can use this as well:
values = np.array([[1,2,3],[4,5,6]])
values[...,0] # first column
#[1,4]
Note: This is not working for built-in array and not aligned (e.g. np.array([[1,2,3],[4,5,6,7]]) )
let's say we have n X m matrix(n rows and m columns) say 5 rows and 4 columns
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
To extract the columns in python, we can use list comprehension like this
[ [row[i] for row in matrix] for in range(4) ]
You can replace 4 by whatever number of columns your matrix has.
The result is
[ [1,5,9,13,17],[2,10,14,18],[3,7,11,15,19],[4,8,12,16,20] ]
I think you want to extract a column from an array such as an array below
import numpy as np
A = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
Now if you want to get the third column in the format
D=array[[3],
[7],
[11]]
Then you need to first make the array a matrix
B=np.asmatrix(A)
C=B[:,2]
D=asarray(C)
And now you can do element wise calculations much like you would do in excel.
One more way using matrices
>>> from numpy import matrix
>>> a = [ [1,2,3],[4,5,6],[7,8,9] ]
>>> matrix(a).transpose()[1].getA()[0]
array([2, 5, 8])
>>> matrix(a).transpose()[0].getA()[0]
array([1, 4, 7])
Just use transpose(), then you can get the columns as easy as you get rows
matrix=np.array(originalMatrix).transpose()
print matrix[NumberOfColumns]
Well a 'bit' late ...
In case performance matters and your data is shaped rectangular, you might also store it in one dimension and access the columns by regular slicing e.g. ...
A = [[1,2,3,4],[5,6,7,8]] #< assume this 4x2-matrix
B = reduce( operator.add, A ) #< get it one-dimensional
def column1d( matrix, dimX, colIdx ):
return matrix[colIdx::dimX]
def row1d( matrix, dimX, rowIdx ):
return matrix[rowIdx:rowIdx+dimX]
>>> column1d( B, 4, 1 )
[2, 6]
>>> row1d( B, 4, 1 )
[2, 3, 4, 5]
The neat thing is this is really fast. However, negative indexes don't work here! So you can't access the last column or row by index -1.
If you need negative indexing you can tune the accessor-functions a bit, e.g.
def column1d( matrix, dimX, colIdx ):
return matrix[colIdx % dimX::dimX]
def row1d( matrix, dimX, dimY, rowIdx ):
rowIdx = (rowIdx % dimY) * dimX
return matrix[rowIdx:rowIdx+dimX]
If you want to grab more than just one column just use slice:
a = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
print(a[:, [1, 2]])
[[2 3]
[5 6]
[8 9]]
Despite using zip(*iterable) to transpose a nested list, you can also use the following if the nested lists vary in length:
map(None, *[(1,2,3,), (4,5,), (6,)])
results in:
[(1, 4, 6), (2, 5, None), (3, None, None)]
The first column is thus:
map(None, *[(1,2,3,), (4,5,), (6,)])[0]
#>(1, 4, 6)
I prefer the next hint:
having the matrix named matrix_a and use column_number, for example:
import numpy as np
matrix_a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
column_number=2
# you can get the row from transposed matrix - it will be a column:
col=matrix_a.transpose()[column_number]
All columns from a matrix into a new list:
N = len(matrix)
column_list = [ [matrix[row][column] for row in range(N)] for column in range(N) ]