gpt4 book ai didi

netlogo - 如何同时删除列表中的多个项目

转载 作者:行者123 更新时间:2023-12-02 17:00:57 25 4
gpt4 key购买 nike

如何同时删除列表中的多个项目?我有一个 list :

let list1 map [ -1 * ? ] reverse (n-values ( ( max-pxcor -  round (max-pxcor / 3) ) + 1 ) [?]) 
print list1
[-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0]

例如,我想删除:- 列表中的最后 4 项:

[-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4]
  • 列表中的最后 9 项:

    [-17 -16 -15 -14 -13 -12 -11 -10 -9]
  • 列表中的最后 13 项:

    [-17 -16 -15 -14 -13]

非常感谢您的帮助。

最佳答案

您可以使用sublist从列表中保留您想要的内容。

例如,删除列表中的最后 4 项:sublist list1 0 (length list1 - 4)

您可以将其概括为一个函数:

to-report remove-last-n [ lst n ]
report sublist lst 0 (length lst - n)
end

然后您可以像这样使用:remove-last-n list1 4

将此与不幸命名的 sentence 结合起来,您可以删除任意子列表:

to-report remove-sublist [ lst position1 position2 ]
report (sentence (sublist lst 0 position1) (sublist lst position2 length lst))
end

这可以让你做到

observer> show remove-sublist [-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0] 2 13
observer: [-17 -16 -4 -3 -2 -1 0]

关于netlogo - 如何同时删除列表中的多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21806821/

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