Netlogo添加到列表列表 [英] Netlogo adding to list of lists
问题描述
我希望将补丁变量值添加到空列表中.这些补丁被划分为不同的区域,我正在尝试查看某些补丁变量如何随区域而变化. 我有一个空列表列表(实际上包含12个列表,但是为了简单起见):
I am looking to add patch variable values to a list of empty lists. The patches are divided into different zones, and I'm trying to see how certain patch variables differ by zone. I have an empty list of lists (actually contains 12 lists, but for simplicity):
set mylist [[] [] [] []]
以及对应于不同区域的列表:
And a list corresponding to the different zones:
set zone-list [1 2 3 4]
这是我尝试建立列表的方式:
Here's how I'm trying to build the lists:
(foreach mylist zone-list [set ?1 lput (sum-zone-variable ?2) ?1])
to-report sum-zone-variable [ n ]
report (sum [patch-variable] of patches with [zone = n])
end
运行此命令时,我的列表保持空白(即不变).我认为问题出在foreach语句上,但我不知道它是什么.有帮助吗?
When I run this, mylist stays empty (ie unchanged). I think the problem is with the foreach statement, but I can't figure out what it is. Any help?
推荐答案
我可以看到foreach mylist [ set ?1 ... ]
背后的想法,但是NetLogo不能那样工作. set ?1 ...
对原始列表无效. NetLogo列表是不可变的,?1
不是对列表中可更新位置的引用-只是一个将值复制到其中的临时变量.所以set ?1 ...
是您基本上不会写的东西.
I can see the thinking behind foreach mylist [ set ?1 ... ]
, but NetLogo doesn't work that way. set ?1 ...
has no effect on the original list. NetLogo lists are immutable, and ?1
is not a reference to an updatable location in a list — it's just a temporary variable into which a value has been copied. So set ?1 ...
is something you will basically never write.
如果我正确理解了您的问题,则此处的相关原语是map
.这应该可以完成工作:
If I understand your question correctly, the relevant primitive here is map
. This should do the job:
set mylist (map [lput (sum-zone-variable ?2) ?1] mylist zonelist)
这篇关于Netlogo添加到列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!