gpt4 book ai didi

python - 用 Hy 中的索引替换列表/字典元素

转载 作者:太空宇宙 更新时间:2023-11-04 02:15:13 28 4
gpt4 key购买 nike

有什么方法可以用 Hy 中的索引替换列表或字典元素的值吗?nth 函数似乎不对应 Python 的方括号。

我期待的是下面的翻译。

(setv lst [1 2 3])
(setv (nth lst 1) 20)

lst=[1, 2, 3]
lst[1]=20

最佳答案

根据 the documentation ,您必须使用 assoc 函数在列表中的特定索引处设置值。因此,您的代码应该是:

(assoc lst 1 20)

这应该会产生预期的结果。

关于python - 用 Hy 中的索引替换列表/字典元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52820960/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com