如何在 netlogo 中使用逗号将值与列表分开?
How to separate values from a list using commas in netlogo?
情况:我有一个代码可以根据下面的代码导出海龟坐标:
to path
file-open (word fileName ".csv")
file-print (word self xcor " " ycor)
file-close
end
结果类似于:
(turtle 1)[1 1 1 1 1 2] [4 4 4 2 1 5]
问题:如何导出同一个列表,但其中的项目用逗号分隔?
例如,从 [1 2 1 1 1]
到 [1,2,1,1,1]
。
提前致谢
如果您尝试在 R 中或事后处理此问题,我建议可能以长格式报告(即,每行表示一只海龟、一个刻度线 [或类似],以及坐标)-我觉得处理起来更简单。
回答您的实际问题 - 一种方法是手动将每个坐标列表折叠成以逗号分隔的字符串。例如下面的玩具模型。
简单设置:
extensions [csv]
globals [ test ]
turtles-own [ xcor-list ycor-list ]
to setup
ca
crt 10 [
set xcor-list []
set ycor-list []
]
repeat 5 [
ask turtles [
rt random 90 - 45
fd 1
set xcor-list lput pxcor xcor-list
set ycor-list lput pycor ycor-list
]
]
reset-ticks
end
这位记者实际上是在将列表折叠成一个简单的输出字符串:
to-report collapse-string-list [str-list]
report reduce word ( sentence map [ str -> word str ", " ] but-last str-list last str-list )
end
这个块将所需的 turtle 变量拉入列表列表,调用 collapse-string-list
报告器,然后导出到 csv:
to output-coord-file
let all-turtles sort turtles
; Pull coordinates from each turtle
let who-coord-list map [
current-turtle ->
(list
[who] of current-turtle
collapse-string-list [xcor-list] of current-turtle
collapse-string-list [ycor-list] of current-turtle
)] all-turtles
; Add headers
set who-coord-list fput ["who" "x" "y"] who-coord-list
; Export
csv:to-file "toy.csv" (map [ row -> (map [i -> (word i)] row ) ] who-coord-list)
end
输出:
情况:我有一个代码可以根据下面的代码导出海龟坐标:
to path
file-open (word fileName ".csv")
file-print (word self xcor " " ycor)
file-close
end
结果类似于:
(turtle 1)[1 1 1 1 1 2] [4 4 4 2 1 5]
问题:如何导出同一个列表,但其中的项目用逗号分隔?
例如,从 [1 2 1 1 1]
到 [1,2,1,1,1]
。
提前致谢
如果您尝试在 R 中或事后处理此问题,我建议可能以长格式报告(即,每行表示一只海龟、一个刻度线 [或类似],以及坐标)-我觉得处理起来更简单。
回答您的实际问题 - 一种方法是手动将每个坐标列表折叠成以逗号分隔的字符串。例如下面的玩具模型。
简单设置:
extensions [csv]
globals [ test ]
turtles-own [ xcor-list ycor-list ]
to setup
ca
crt 10 [
set xcor-list []
set ycor-list []
]
repeat 5 [
ask turtles [
rt random 90 - 45
fd 1
set xcor-list lput pxcor xcor-list
set ycor-list lput pycor ycor-list
]
]
reset-ticks
end
这位记者实际上是在将列表折叠成一个简单的输出字符串:
to-report collapse-string-list [str-list]
report reduce word ( sentence map [ str -> word str ", " ] but-last str-list last str-list )
end
这个块将所需的 turtle 变量拉入列表列表,调用 collapse-string-list
报告器,然后导出到 csv:
to output-coord-file
let all-turtles sort turtles
; Pull coordinates from each turtle
let who-coord-list map [
current-turtle ->
(list
[who] of current-turtle
collapse-string-list [xcor-list] of current-turtle
collapse-string-list [ycor-list] of current-turtle
)] all-turtles
; Add headers
set who-coord-list fput ["who" "x" "y"] who-coord-list
; Export
csv:to-file "toy.csv" (map [ row -> (map [i -> (word i)] row ) ] who-coord-list)
end
输出: