## Fix Python – How does NumPy’s transpose() method permute the axes of an array?

In [28]: arr = np.arange(16).reshape((2, 2, 4))

In [29]: arr
Out[29]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],

[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])

In [32]: arr.transpose((1, 0, 2))
Out[32]:
array([[[ 0, 1, 2, 3],
[ 8, 9, 10, 11]],

[[ 4, 5, 6, 7],
[12, 13, 14, 15]]])

When we pass a….

## Fix Python – Transposing a 1D NumPy array

I use Python and NumPy and have some problems with “transpose”:
import numpy as np
a = np.array([5,4])
print(a)
print(a.T)

Invoking a.T is not transposing the array. If a is for example [[],[]] then it transposes correctly, but I need the transpose of […,…,…].
….

## Fix Python – Transpose list of lists

Let’s take:
l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

The result I’m looking for is
r = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

and not
r = [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

….

## Fix Python – Transpose/Unzip Function (inverse of zip)?

I have a list of 2-item tuples and I’d like to convert them to 2 lists where the first contains the first item in each tuple and the second list holds the second item.
For example:
original = [(‘a’, 1), (‘b’, 2), (‘c’, 3), (‘d’, 4)]
# and I want to become…
result = ([‘a’, ‘b’, ‘c’, ‘d’], [1, 2, 3, 4])

Is there a builtin function that does that?….