如何删除 NetLogo 中两个列表中的重复项目?
How to remove duplicate items in two lists in NetLogo?
我很难从两个列表中删除重复项。我有海龟走过的代码,并导出它们经过的补丁的坐标。我想删除后面的重复行。例如:在一个刻度上,乌龟按顺序穿过补丁 (0 0),它只是转过身继续在补丁 (0 0) 中移动,然后到达补丁 (0 1),然后返回补丁 (0 0)。那我想知道有没有办法把重复的数据按顺序去掉
在示例中,.csv 文件为:
补丁 0 0
补丁 0 0
补丁 0 1
补丁 0 0
我想要 .csv 文件:
补丁 0 0
补丁 0 1
补丁 0 0
有可能吗?如果可以,我该怎么做?
extensions [ csv ]
globals [Outputlist outifle]
turtles-own [ pxcor-list pycor-list mytimer ]
to setup
clear-all
set Outputlist [ [ "id" "pxcor" "pycor" "mytimer" ] ]
crt 10
[
setxy random-xcor random-ycor
set size 0.5
set pxcor-list (list pxcor)
set pycor-list (list pycor)
set mytimer [0]
pen-down
]
let pcolors [ ]
set pcolors [ 1 10 ]
ask patches [ set pcolor item (random 2) pcolors ]
reset-ticks
end
to go
if ticks = 4 [
ask turtles [ output ]
stop
]
move
tick
end
to move
ask turtles [
rt random 360
fd 1
register-coordinates
]
end
to register-coordinates
set pxcor-list lput pxcor pxcor-list
set pycor-list lput pycor pycor-list
set mytimer lput ticks mytimer
end
To output
let cur-who-list n-values ( length pxcor-list ) [ who ]
; print cur-who-list
( foreach cur-who-list pxcor-list pycor-list mytimer
[
[ a b c d ] ->
let to-append ( list a b c d )
set Outputlist lput to-append Outputlist
]
)
; Export the list to csv
csv:to-file (word outifle ".csv") Outputlist
end
提前致谢
您可以通过让海龟在将它们的新坐标添加到坐标列表之前评估条件来实现此目的。在这种情况下,乌龟必须评估当前位置是否与最新注册的位置不同;只有在这种情况下海龟才会将新坐标添加到列表中:
to move
ask turtles [
rt random 360
fd 1
if (pxcor != last pxcor-list) or (pycor != last pycor-list) [
register-coordinates
]
]
end
另一种方法:你可以让海龟在移动前记下它们当前的补丁,并在移动后检查补丁是否相同:
to move
ask turtles [
let origin patch-here
rt random 360
fd 1
if (origin != patch-here) [
register-coordinates
]
]
end
也许第二种方法更可取,因为虽然海龟创建了一个局部变量,但它们只需要评估一个条件(而不是在第一个解决方案中可能有两个条件),并且因为海龟将不必访问列出来评估条件。这可能是推测,但这意味着第二种方法可能更快。
我很难从两个列表中删除重复项。我有海龟走过的代码,并导出它们经过的补丁的坐标。我想删除后面的重复行。例如:在一个刻度上,乌龟按顺序穿过补丁 (0 0),它只是转过身继续在补丁 (0 0) 中移动,然后到达补丁 (0 1),然后返回补丁 (0 0)。那我想知道有没有办法把重复的数据按顺序去掉
在示例中,.csv 文件为:
补丁 0 0
补丁 0 0
补丁 0 1
补丁 0 0
我想要 .csv 文件:
补丁 0 0
补丁 0 1
补丁 0 0
有可能吗?如果可以,我该怎么做?
extensions [ csv ]
globals [Outputlist outifle]
turtles-own [ pxcor-list pycor-list mytimer ]
to setup
clear-all
set Outputlist [ [ "id" "pxcor" "pycor" "mytimer" ] ]
crt 10
[
setxy random-xcor random-ycor
set size 0.5
set pxcor-list (list pxcor)
set pycor-list (list pycor)
set mytimer [0]
pen-down
]
let pcolors [ ]
set pcolors [ 1 10 ]
ask patches [ set pcolor item (random 2) pcolors ]
reset-ticks
end
to go
if ticks = 4 [
ask turtles [ output ]
stop
]
move
tick
end
to move
ask turtles [
rt random 360
fd 1
register-coordinates
]
end
to register-coordinates
set pxcor-list lput pxcor pxcor-list
set pycor-list lput pycor pycor-list
set mytimer lput ticks mytimer
end
To output
let cur-who-list n-values ( length pxcor-list ) [ who ]
; print cur-who-list
( foreach cur-who-list pxcor-list pycor-list mytimer
[
[ a b c d ] ->
let to-append ( list a b c d )
set Outputlist lput to-append Outputlist
]
)
; Export the list to csv
csv:to-file (word outifle ".csv") Outputlist
end
提前致谢
您可以通过让海龟在将它们的新坐标添加到坐标列表之前评估条件来实现此目的。在这种情况下,乌龟必须评估当前位置是否与最新注册的位置不同;只有在这种情况下海龟才会将新坐标添加到列表中:
to move
ask turtles [
rt random 360
fd 1
if (pxcor != last pxcor-list) or (pycor != last pycor-list) [
register-coordinates
]
]
end
另一种方法:你可以让海龟在移动前记下它们当前的补丁,并在移动后检查补丁是否相同:
to move
ask turtles [
let origin patch-here
rt random 360
fd 1
if (origin != patch-here) [
register-coordinates
]
]
end
也许第二种方法更可取,因为虽然海龟创建了一个局部变量,但它们只需要评估一个条件(而不是在第一个解决方案中可能有两个条件),并且因为海龟将不必访问列出来评估条件。这可能是推测,但这意味着第二种方法可能更快。