Coming from Python and its version of list comprehensions, I am looking for a way to
accomplish in Elixir, transposition of a list of lists. Here is single line Python version:
>>> m = [[1,2],[3,4],[5,6]]
>>> rez = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
>>> rez
[[1, 3, 5], [2, 4, 6]]
I would like to keep it short and elegant, if possible! Thanks for your help!
1 Like
Adapting https://stackoverflow.com/questions/5389254/transposing-a-2-dimensional-matrix-in-erlang to elixir:
defmodule Transp do
def transpose([[] | _]), do: []
def transpose(m) do
[Enum.map(m, &hd/1) | transpose(Enum.map(m, &tl/1))]
end
end
iex(2)> matrix = [[1,2],[3,4],[5,6]]
[[1, 2], [3, 4], [5, 6]]
iex(3)> Transp.transpose(matrix)
[[1, 3, 5], [2, 4, 6]]
1 Like
Or simply:
iex> Enum.zip [[1,2],[3,4],[5,6]]
[{1, 3, 5}, {2, 4, 6}]
# or if you need list of lists:
iex> Enum.zip([[1,2],[3,4],[5,6]]) |> Enum.map(&Tuple.to_list/1)
[[1, 3, 5], [2, 4, 6]]
10 Likes
Another way (from http://erlang.org/pipermail/erlang-questions/2009-June/044609.html):
defmodule Transp do
def transpose([]), do: []
def transpose([[] | xss]), do: transpose(xss)
def transpose([[x | xs] | xss]) do
[[x | (for [h | _t] <- xss, do: h)] | transpose([xs | (for [_h | t] <- xss, do: t)])]
end
end
although it’s practically the same.
1 Like
If you want to shoehorn a comprehension in:
iex(1)> m = [[1,2],[3,4],[5,6]]
[[1, 2], [3, 4], [5, 6]]
iex(2)> for t <- Enum.zip(m), do: Tuple.to_list(t)
[[1, 3, 5], [2, 4, 6]]
Personally I find the explicit map
clearer.
Wow, great answers! Thank you all!
2 Likes