在列表列表中查找单个重复项 Netlogo
Find a single duplicate in a list of lists Netlogo
如果我有这个列表,我正试图在列表的子列表之间找到重复项
let listA [[-9 2] [-9 1] [-9 0][-9 -1][-9 -2][-9 -3][-9 -4][-8 0][-9 0]]
这是一个限制,这个列表只能有一个可以重复的子列表,在这种情况下是 [-9 0] 我想将这两个元素保存在 2 个变量中,例如:
let element-x item 0 ?
let element-y item 1 ?
但我实际上不知道如何比较列表的两个子列表(如果它们具有相同的元素)。
获取这些变量(element-x element-y)后,我必须删除 listA 中包含这些变量之一 -9 或 0 的每个子列表,并将剩余坐标保存在新列表(list-corordinates)中
我已经通过在下面的这些代码中将这个变量(重复子列表的)作为常量(用于测试目的)来完成此操作:
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 应该 return 列表中的第一个重复项(在本例中是子列表)。
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
您的其余代码应该从那里开始。
如果我有这个列表,我正试图在列表的子列表之间找到重复项
let listA [[-9 2] [-9 1] [-9 0][-9 -1][-9 -2][-9 -3][-9 -4][-8 0][-9 0]]
这是一个限制,这个列表只能有一个可以重复的子列表,在这种情况下是 [-9 0] 我想将这两个元素保存在 2 个变量中,例如:
let element-x item 0 ?
let element-y item 1 ?
但我实际上不知道如何比较列表的两个子列表(如果它们具有相同的元素)。
获取这些变量(element-x element-y)后,我必须删除 listA 中包含这些变量之一 -9 或 0 的每个子列表,并将剩余坐标保存在新列表(list-corordinates)中
我已经通过在下面的这些代码中将这个变量(重复子列表的)作为常量(用于测试目的)来完成此操作:
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 应该 return 列表中的第一个重复项(在本例中是子列表)。
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
您的其余代码应该从那里开始。