[英]How do I convert a sequence of sequences into a sequence of maps with clojure?
I'm reading data from a file where each line has two values. 我正在读取文件中的数据,其中每行有两个值。 Each line is represented by a sequence within an outer sequence representing the file.
每一行由表示文件的外部序列内的序列表示。
I'd like to restructure the data into a sequence of maps for further processing. 我想将数据重组为一系列地图以供进一步处理。
I know how to create a map from a key set and sequence of values: 我知道如何从键集和值序列创建映射:
=> (defstruct entry :name :age)
=> (apply struct entry '("John" 34))
{:name "John", :age 34}
But how do I create a sequence of such maps based on a sequence of value sequences? 但是,如何根据一系列值序列创建一系列此类地图?
(map (apply struct entry) '(("John" 34) ("Lisa" 41))))
results in: 结果是:
java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.PersistentStructMap$Def
EDIT: Renamed symbol for clarity. 编辑:为了清晰起见重命名符号。
structs are obsolete, the preference is to use records now. 结构已经过时,首选是使用记录。
(defrecord Person [name age])
(map (partial apply ->Person) '(("John" 34) ("Lisa" 41)))
Use zipmap
使用
zipmap
(map (partial zipmap [:name :age]) '(("John" 34) ("Lisa" 41)))
;-> ({:name "John", :age 34} {:name "Lisa", :age 5})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.