我刚接触过Netlogo,还在学习,我想做的是在更新完成后调用一个列表,然后对它执行另一个更新,直到达到一个条件,如果有一个列表。
let xy [[-2 6][-1 6][0 6][-1 6][-2 6][1 5][2 5][-3 9][-4 9][-5 9][-6 9][-3 9]]
let same true我试图从列表中删除两个相同元素之间的第一个子列表-2 6 0 6-2 6,然后我还想删除其他两个相同元素之间的子列表-3 9-5 9-3 9,直到在本例中不再重复的元素应该是[1 5],并且在删除带有条件的子列表之后,我控制该列表:
if length xy = length remove-duplicates xy [ set same false ]我已经完成了下面的删除代码,它移除了第一个子列表,但是我可能有很多子列表,我想知道在一次删除之后,我如何才能再次获得更新的列表(在这种情况下,我应该以某种方式获取代码中的最终列表),并使用这些条件再次控制它。我正在考虑做一个报告过程和一个while循环,例如(也许我错了)。
to-report update-list [list]
while [same != false ]
[ ; do the removal stuff
set list-xy item POSITION (FIRST MODES xy) xy xy
let first-pos POSITION (FIRST MODES xy) xy
set list-temp remove-item first-pos xy
set sec-pos position list-xy list-temp + 1
set sublist-1 sublist xy 0 first-pos
set sublist-2 sublist xy sec-pos length xy
set final-list sentence sublist-1 sublist-2
set xy final-list
; the condition if i don't have any duplicates the size of two lists should have the same size , no duplicates
if length xy = length remove-duplicates xy
[ set same false ]
]
report update-list xy 我不知道在程序结束时报告什么,以及如何再次回忆列表,所以我可以删除所有这些子列表。
任何想法都很感谢,谢谢。
发布于 2015-06-22 12:32:51
这是使用递归最容易解决的:
to-report remove-fenced-sublists [xs]
if empty? xs
[ report [] ]
let pos position first xs butfirst xs
if not is-number? pos
[ report fput first xs remove-fenced-sublists butfirst xs ]
report remove-fenced-sublists sublist xs (pos + 2) length xs
end样本运行:
observer> show remove-fenced-sublists [[-2 6][-1 6][0 6][-1 6][-2 6][1 5][2 5][-3 9][-4 9][-5 9][-6 9][-3 9]]
observer: [[1 5] [2 5]]发布于 2015-06-22 12:52:02
如果我理解你的目标,你的核心需求可以通过逐项建立一个新的列表来捕捉,但是每当出现重复的时候就会修剪它。对于一个新项目,这是:
to-report trim-or-grow [#list #item]
let _i position #item #list
report ifelse-value (_i != false) [sublist #list 0 _i] [lput #item #list]
end然后你可以用这个记者减少:
to test
let xy [[-2 6][-1 6][0 6][-1 6][-2 6][1 5][2 5][-3 9][-4 9][-5 9][-6 9][-3 9]]
print reduce trim-or-grow fput [] xy
endhttps://stackoverflow.com/questions/30923263
复制相似问题