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]]
Related
I want to convert a normal list(main_list) into a nested one(ans_list) but I don't know how.
main_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ans_list = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Use a comprehension over appropriate slices:
n = 3
ans_list = [main_list[i::n] for i in range(n)]
# [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
if you are open to using NumPy and there are no other conditions other than how many rows you want to split in to then try this
import numpy as np
np.array(main_list).reshape(-1,3).tolist()
This question already has answers here:
How to remove an element from a list by index
(18 answers)
Closed 2 years ago.
so I have this matrix:
matrix = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
And I want to remove the last row, so it return me something like this:
matrix = [ [1, 2, 3],
[4, 5, 6]
]
P.S. I cant use Numpy.
matrix = [ [1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix[:-1]
This gives [[1, 2, 3], [4, 5, 6]] back
So what you can do is:
matrix = matrix[:-1]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 2 years ago.
I'm aware of how completely flattening a list of sublists is done, however, I am unsure of how to do so by only one level.
For example, a sublist like [[[1, 2], 3], [[4, 5], 6], [[7, 8], 9]] would get flattened into [1, 2, 3, 4, 5, 6, 7, 8, 9].
However, I'm struggling to figure out a way for the result to be [[1, 2, 3], [4, 5, 6], [7, 8, 9]], without ending up flattening the entire list.
Any help will be appreciated!
input_array = [([1, 2], 3), [[4, 5], 6], [[7, 8, (9, [10])], 11]]
result_array = []
def flat_element(el):
global result_array
_class = ''
try:
_class = str(type(el)).split("'")[1]
except:
pass
if _class in ('list', 'tuple'):
for elem in el:
flat_element(elem)
else:
result_array.append(el)
flat_element(input_array)
print(result_array)
main_array = []
for sub_array in input_array:
result_array = []
flat_element(sub_array)
main_array.append(result_array)
print(main_array)
example_array = [[[1, 2], 3], [[4, 5], 6], [[7, 8], 9]]
main_array = []
for sub_array in example_array:
result_array = []
flat_element(sub_array)
main_array.append(result_array)
print(main_array)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10, 11]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Working with arrays and tuples
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 an answer here:
Appending matrix A with matrix B
(1 answer)
Closed 2 years ago.
I have a array:
a = np.array([[1, 2, 3],
[4, 5, 6])
b = np.array([[7, 8],
[9, 10]])
I want to extend array with expected result:
c = np.array([[1, 2, 3, 7, 8],
[4, 5, 6, 9, 10])
Thanks!
You can use .hstack:
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]])
b = np.array([[7, 8],
[9, 10]])
c = np.hstack((a, b))
print(c)
Output:
[[ 1 2 3 7 8]
[ 4 5 6 9 10]]