List 在Netlogo列表中查找单个副本
如果我有这个列表,我试图在列表的子列表之间找到一个重复项List 在Netlogo列表中查找单个副本,list,duplicates,netlogo,List,Duplicates,Netlogo,如果我有这个列表,我试图在列表的子列表之间找到一个重复项 let listA [[-9 2] [-9 1] [-9 0][-9 -1][-9 -2][-9 -3][-9 -4][-8 0][-9 0]] 这是一个限制,该列表只能有一个子列表可以重复,在这种情况下是[-9 0],我想将这两个元素保存在两个变量中,例如: let element-x item 0 ? let element-y item 1 ? 但如果列表的两个子列表具有相同的元素,我实际上不知道如何相互比较 获取这些变量(
let listA [[-9 2] [-9 1] [-9 0][-9 -1][-9 -2][-9 -3][-9 -4][-8 0][-9 0]]
这是一个限制,该列表只能有一个子列表可以重复,在这种情况下是[-9 0],我想将这两个元素保存在两个变量中,例如:
let element-x item 0 ?
let element-y item 1 ?
但如果列表的两个子列表具有相同的元素,我实际上不知道如何相互比较
获取这些变量(element-x-element-y)后,我必须删除listA中包含其中一个变量-9或0的每个子列表,并将剩余的cordinate保存在一个新列表中(list cordinates)
在下面的代码中,我已经通过将这个变量(复制子列表的)作为常量(用于测试目的)来实现这一点:
globals [
list-cordinates
element-x
element-y
]
set element-x -9
set element-y 0
foreach listA [
if item 0 ? != element-x AND item 1 ? != element-y[
let x item 0 ?
let y item 1 ?
set list-cordinates lput( list x y ) list-cordinates
]
]
现在,我只需要这些变量不是常量,而是listA的复制子列表中的2个项。这很快,但find dup应该返回列表中的第一个复制项(在本例中是子列表)
to go
let listA [[-9 2] [-9 1] [-9 0][-9 -1][-9 -2][-9 -3][-9 -4][-8 0][-9 0]]
show find-dup listA
end
to-report find-dup [ c ]
;returns the first duplicated item, or false if no duplicates
if length c = 1 [ report false ] ;we've run out of list before a dup is found
;compare the first element of the list to the rest
let a first c
let b butfirst c
;does the first element match any remaining element?
foreach b [
if (a = ?) [report a ] ;found a duplicate, report it.
]
;no matches. test the remainder of the list for a duplicate
report find-dup but-first b
end
代码的其余部分应该从那里开始。这很快而且很脏,但是find dup应该返回列表中的第一个重复项(在本例中是子列表)
to go
let listA [[-9 2] [-9 1] [-9 0][-9 -1][-9 -2][-9 -3][-9 -4][-8 0][-9 0]]
show find-dup listA
end
to-report find-dup [ c ]
;returns the first duplicated item, or false if no duplicates
if length c = 1 [ report false ] ;we've run out of list before a dup is found
;compare the first element of the list to the rest
let a first c
let b butfirst c
;does the first element match any remaining element?
foreach b [
if (a = ?) [report a ] ;found a duplicate, report it.
]
;no matches. test the remainder of the list for a duplicate
report find-dup but-first b
end
代码的其余部分应该从这里开始