简体   繁体   English

将列表列表转换成列表列表(即行到列)

[英]Convert map of list into list of maps (i.e. rows to colums)

I have the following data structure in Clojure 我在Clojure中具有以下数据结构

{:a [1 2 3]
 :b [4 5 6]
 :c [7 8 9]} 

And I'd like to convert it into something like 我想将其转换为

[{:a 1 :b 4 :c 7}
 {:a 2 :b 5 :c 8}
 {:a 3 :b 6 :c 9}]

At the moment I'm kinda stumped as to how to do this. 此刻,我对如何做到这一点有些困惑。

In Clojure you can never guarantee the order of keys in maps after transformations. 在Clojure中,您永远无法保证变换后键在地图中的顺序。 They're indexed by key, not by order. 它们是通过键而不是顺序来索引的。

Vectors are, however. 向量是。 And with get-in you can do a lookup on position with a vector of coordinates . 借助get-in您可以使用坐标向量在位置上进行查找。

=> (def mat
     [[1 2 3]
      [4 5 6]
      [7 8 9]])

=> (defn transpose
     [m]
     (apply mapv vector m))

=> (get-in (transpose mat) [1 2])
8

Got it: 得到它了:

(defn transpose-lists [x] 
  (map (fn [m] (zipmap (keys x) m)) (apply map vector (vals x))))

Unfortunately it doesn't preserve order of the keys. 不幸的是,它不保留键的顺序。 If anyone has a better solution then of course I'd like to hear it! 如果有人有更好的解决方案,那么我当然想听听!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将地图列表转换为 dart 中的 Map 列表 - How can I convert List of Maps into List of Map in dart 将列表的 map 转换为地图列表 - Convert a map of lists into a list of maps 如何将地图列表转换为python中的集合映射? - How can I convert a list of maps into a map of sets in python? Haskell 是否有 List Slices(即 Python)? - Does Haskell have List Slices (i.e. Python)? 在Groovy中将地图列表转换为单个地图 - Convert List of Maps into Single Map In Groovy 什么时候应该使用数组列表(即列表) <string []> =新清单 <string []> ())在C#中? - When should I use List of Array (i.e. List<string []> = new List<string []>()) in C#? 如果该子列表中的任何元素在另一个列表中,如何删除列表中的列表(即子列表)? - how to delete a list within a list (i.e., a sublist) if any element of that sublist is in another list? 计算特定列表项的行和列 - Calculate rows and colums of specific list items 容器的投影,即转换List的方法<Object>列出<Object.Member> - Projection of containers, i.e. method to transform List<Object> to List<Object.Member> 如何清除 Javascript 中的列表,即一键删除列表中的所有项目? - How do you clear a list in Javascript, i.e. delete all items in a list with one button click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM