holder66
Transpose a list of lists using list comprehension
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!
Most Liked
kip
ex_cldr Core Team
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
holder66
Wow, great answers! Thank you all!
2
idi527

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
idi527
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
Popular in Questions
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Hi,
is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New







