我是Netlogo的新手,所以如果这是一个微不足道的问题,我深表歉意。我想将列表的值分配/复制到patches属性中,确保值的顺序得到尊重。下面的代码执行相反的操作,即将补丁属性(attr1(的值复制到list1中
patches-own [ attr1 attr2]
to setup
clear-all
ask patches [set attr1 random 10]
let patch-list sort patches
let list1 map [ p -> [attr1] of p ] patch-list
end
现在,假设我想将varX的值分配/复制到patches属性attr2中,确保值的顺序得到尊重。
let nPix world-width * world-height
let varX (range nPix)
????
有什么建议吗?感谢
您可以通过向匿名过程提供两个输入,让Netlogo同时遍历两个列表。要做到这一点,您需要在整个过程周围加上括号。
patches-own [attr2]
to setup
ca
resize-world 0 4 0 4
end
to go
let nPix world-width * world-height
let varX n-values nPix [random 140]
(foreach sort patches varX [ [the-patch the-var] ->
ask the-patch [set attr2 the-var]
]
)
show varX
ask patches [set pcolor attr2]
end
您也可以始终选择一个好的旧while循环,在该循环中增加一个索引(i(,并使用该索引将两个列表中的相应项耦合起来。
to go-3
let nPix world-width * world-height
let varX n-values nPix [random 140]
let patch-list sort patches
let i 0
while [i < nPix] [
ask item i patch-list [set attr2 item i varX]
set i i + 1
]
show varX
ask patches [set pcolor attr2]
end