This question already has answers here:
Matrix Transpose in Python [duplicate]
(19 answers)
Closed 8 years ago.
I have [[1,2,3], [5,6,7]] and want to produce the list [[1,5], [2,6], [3,7]]. How do I do this in Python?
>>> zip([1,2,3], [4, 5, 6])
[(1, 4), (2, 5), (3, 6)]
To convert each element to a list
>>> [list(a) for a in zip([1, 2, 3], [4, 5, 6])]
[[1, 4], [2, 5], [3, 6]]
result = [list(x) for x in zip([1, 2, 3], [5, 6, 7])]
Related
This question already has answers here:
Transpose list of lists
(14 answers)
Closed last year.
I have a numpy array like this:
array = [[1, 3, 5, 7], [2, 4, 6, 8]]
I would like to concatenate them element-wise. Most of the solutions I have found are able to do this with two separate 2d arrays, but I would like to do this within a single 2d array.
Desired output:
array = [[1, 2], [3, 4], [5, 6], [7, 8]]
Just only line code
array = [[1, 3, 5, 7], [2, 4, 6, 8]]
print(list(zip(*array)))
output :
[(1, 2), (3, 4), (5, 6), (7, 8)]
ljdyer is almost right, but you have only one array, so you can do this:
list(zip(*array))
This is a zip operation. Try:
arr1 = [1, 3, 5, 7]
arr2 = [2, 4, 6, 8]
print(list(zip(arr1,arr2)))
Output:
[(1, 2), (3, 4), (5, 6), (7, 8)]
Or for numpy arrays you have the stack operation:
import numpy as np
a = np.array([1, 3, 5, 7])
b = np.array([2, 4, 6, 8])
c = np.stack((a,b), axis = 1)
print(c)
See https://www.delftstack.com/howto/numpy/python-numpy-zip/
The following code will concatenate array element wise in each column
import numpy as np
array = np.array([[1, 3, 5, 7], [2, 4, 6, 8]])
res = []
for i in range(array.shape[1]):
res.append(array[:, i])
res = np.array(res)
print(res.tolist())
Numpy transpose:
In [382]: np.transpose([[1, 3, 5, 7], [2, 4, 6, 8]])
Out[382]:
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
If it already is a numpy array (as opposed to just a list of lists),you can use the T shortcut:
In [383]: arr = np.array([[1, 3, 5, 7], [2, 4, 6, 8]])
In [384]: arr
Out[384]:
array([[1, 3, 5, 7],
[2, 4, 6, 8]])
In [385]: arr.T
Out[385]:
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
list-zip is a well known list version of transpose:
In [386]: list(zip(*arr))
Out[386]: [(1, 2), (3, 4), (5, 6), (7, 8)]
Note the result is a list of tuples.
This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Power set and Cartesian Product of a set python
(1 answer)
Closed 1 year ago.
So say you have n numbers in a list as so
[n1, n2, n3, ...., n]
How would you get all possible combinations?
For example if you had
[1,2,3,4]
You return a list like:
[[1,2], [1,3], [1,4], [2,3], [2,4], [3,4], [1,2,3], [1,2,4], [1,3,4], [2, 3, 4], [1,2,3,4]]
Here is a generator function, using the go-to itertools.combinations:
from itertools import combinations
def combos(lst):
for n in range(2, len(lst)+1):
yield from combinations(lst, n)
list(combos([1,2,3,4]))
# [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4),
# (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4),
# (1, 2, 3, 4)]
If you desperately need lists instead of tuples:
list(map(list, combos([1,2,3,4])))
# [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4],
# [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4],
# [1, 2, 3, 4]]
This question already has answers here:
Transpose list of lists
(14 answers)
Closed 2 years ago.
We have an array
a = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
and looking for array:
b = [[1, 4, 7],[2, 5, 8],[3, 6, 9]]
Thank you!
Try with numpy.transpose(). See below:
import numpy as np
a = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
[list(i) for i in np.array(a).transpose()]
Output:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
This question already has answers here:
Matrix Transpose in Python [duplicate]
(19 answers)
Closed 2 years ago.
Let's say that:
>>> data
[[1, 2],
[3, 4],
[5, 6],
[7, 8]]
When rows and columns are swapped:
>>> data
[[1, 3, 5, 7],
[2, 4, 6, 8]]
How do I achieve the following without the use of external libraries such as pandas or numpy?
data = [[1, 2],
[3, 4],
[5, 6],
[7, 8]]
print([*map(list, zip(*data))])
Prints:
[[1, 3, 5, 7], [2, 4, 6, 8]]
This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 2 years ago.
I have m-length list of tuples.
For example:
m = 2
mylist = [(1, 2), (3, 4)]
I need to get all combinations of these tuples, using only one element from one tuple:
[1, 3]
[1, 4]
[2, 3]
[2, 4]
For m=3:
mylist = [(1, 2), (3, 4), (5, 6)]
[1, 3, 5]
[1, 3, 6]
[1, 4, 5]
[1, 4, 6]
[2, 3, 5]
[2, 3, 6]
[2, 4, 5]
[2, 4, 6]
Is there any good way to do it for any m?
You can use itertools.product:
import itertools
for el in itertools.product(*mylist):
print(el)
Outputs:
(1, 3)
(1, 4)
(2, 3)
(2, 4)
Ref. https://docs.python.org/3/library/itertools.html#itertools.product