Is there any way to transport map table?

I have a table org. I want to transport the table into transposed.
Is there any way to transport it?

org = %{
  "1" => [1,2,3],
  "2" => [4,5,6],
  "3" => [7,8,9],
}
transposed = [
  %{
    "1" => 1,
    "2" => 4,
    "3" => 7,
  },
  %{
    "1" => 2,
    "2" => 5,
    "3" => 8,
  },
  %{
    "1" => 3,
    "2" => 6,
    "3" => 9,
  },
]
1 Like

That does not look like matrix transposition to me, but assuming the output is what is wanted and the rows and columns are the same count then I’d probably just do this horror as a first step (then I’d probably break it up into individual function calls for readability and maintainability):

iex> org = %{"1" => [1,2,3], "2" => [4,5,6], "3" => [7,8,9],}
iex> transposed = org |> Enum.map(&(elem(&1,1))) |> Enum.zip() |> Enum.map(fn(v)->v|>Tuple.to_list()|>Enum.with_index(1)|>Enum.reduce(%{},&(Map.put_new(&2,to_string(elem(&1,1)),elem(&1,0)))) end)
[%{"1" => 1, "2" => 4, "3" => 7}, %{"1" => 2, "2" => 5, "3" => 8},
 %{"1" => 3, "2" => 6, "3" => 9}]

Or so? Though the problem given is a little bit ill-defined. :slight_smile:

2 Likes

I’ll scrutinize your solution. Thank you!

2 Likes