(cons 1 (list 2 3))
返回clojure.lang.cons
。如何将其转换为clojure.lang.PersistentList
?
答案 0 :(得分:9)
使用conj
:
cons
并尝试转换结果
(conj (list 2 3) 1)
=> (1 2 3)
(type (conj (list 2 3) 1))
=> clojure.lang.PersistentList
答案 1 :(得分:5)
Clojure: how to convert cons to list
Don't!
Clojure is built upon extensible abstractions. One of the most important is the sequence.
I can think of no reason why you would want to convert a cons
into a list
or vice versa. They are both sequences and nothing much else. What you can do with one you can do with the other.
The above takes forward Leetwinski's comment on the question.
答案 2 :(得分:2)
您可以apply
返回序列的内容到list
函数:
(apply list (cons 1 (list 2 3)))
=> (1 2 3)
(type *1)
=> clojure.lang.PersistentList
答案 3 :(得分:-1)
最简单的方法是将其变成矢量。这种数据类型在Clojure中非常有用。实际上,在Clojure中编程时,大多数数据都保存在向量或映射中,而列表用于“代码作为数据”(宏系统)。
在您的情况下,解决方案将是:
m1 = (df['days_1'] < df['days']) & (df['days'] < df['days_2'])
s1 = df['amount'] * (df['percent_1'] - df['percent_2']) / 100
s11 = df['days'] - df['days_1']
m2 = (df['days_2'] < df['days']) & (df['days'] < df['period'])
s2 = df['amount'] * (df['percent_2']) / 100
s22 = df['days'] - df['days_2']
df['amount_missed'] = np.select([m1, m2], [s1, s2], default=0)
df['days_missed'] = np.select([m1, m2], [s11, s22], default=0)
我不知道你需要一个列表的情况,而不是矢量或seq。因为大多数函数在序列上运行但不是严格的集合类型。我相信user=> (vec (cons 1 (list 2 3)))
[1 2 3]
类型也应该有用。
如果您确实需要列表,可以使用cons
函数来转换集合的类型。但请记住,在处理清单时,订单将相反:
into
所以你需要先反转输入数据:
user=> (into '() (cons 1 (list 2 3)))
(3 2 1)