使用两个参数过滤所有列表
Filtering in all lists by using two parameters
根据用户对这个问题的回答:,我尝试使用他的建议来过滤项目和海龟,如下所示:
show sum map [t -> length filter [a -> item 1 a = turtle 3] [ all-archives ]] of all-turtles
上面的代码应该计算 turtle 3
创建的 item 1
包含在所有海龟 (all-turtles
) 的列表 (all-archives
) 中的次数。
但是,我从上面的公式中得到以下错误:
ERROR: Expected closing bracket
(在第二个 "a")。
我确定我写的仍然有语法错误,但我没有找到任何使用两个参数进行过滤的例子。
您确实放错了地方 ]
。有时将这些长命令分解成多个部分会更容易。下面的代码通过让两只海龟将两个条目放入给定数量的其他海龟档案以及它们自己的档案中来设置测试。然后它会以碎片为单位进行计数,最后将这些碎片放在一个命令中。在您的问题中,您使用 "all-turtles",但在 NetLogo 中这只是 turtles
。最后一条命令只查看海龟的随机子集。
turtles-own [ archive ]
to test
clear-all
create-turtles 100 [
set archive []
]
ask turtle 3 [
let archive-entry list "at home" self
set archive fput archive-entry archive
ask n-of 30 other turtles [ set archive fput archive-entry archive ]
set archive-entry list "not at home" self
set archive fput archive-entry archive
ask n-of 40 other turtles [ set archive fput archive-entry archive ]
]
ask turtle 4 [
let archive-entry list "at home" self
set archive fput archive-entry archive
ask n-of 20 other turtles [ set archive fput archive-entry archive ]
set archive-entry list "not at home" self
set archive fput archive-entry archive
ask n-of 50 other turtles [ set archive fput archive-entry archive ]
]
; get a list of all archives.
let list-of-all-archives [archive] of turtles
; make a list of the number of occurrences we are looking for in each archive.
let list-of-count-in-each-archive map [t -> length filter [a -> item 0 a = "at home" and item 1 a = turtle 3] t] list-of-all-archives
; sum up the number of occurences across all archives.
show sum list-of-count-in-each-archive
show sum map [t -> length filter [a -> item 0 a = "not at home" and item 1 a = turtle 3] t] [archive] of turtles
show sum map [t -> length filter [a -> item 0 a = "at home" and item 1 a = turtle 4] t] [archive] of turtles
show sum map [t -> length filter [a -> item 0 a = "not at home" and item 1 a = turtle 4] t] [archive] of turtles
show sum map [t -> length filter [a -> item 0 a = "not at home" and item 1 a = turtle 4] t] [archive] of n-of 50 turtles
end
根据用户对这个问题的回答:
show sum map [t -> length filter [a -> item 1 a = turtle 3] [ all-archives ]] of all-turtles
上面的代码应该计算 turtle 3
创建的 item 1
包含在所有海龟 (all-turtles
) 的列表 (all-archives
) 中的次数。
但是,我从上面的公式中得到以下错误:
ERROR: Expected closing bracket
(在第二个 "a")。
我确定我写的仍然有语法错误,但我没有找到任何使用两个参数进行过滤的例子。
您确实放错了地方 ]
。有时将这些长命令分解成多个部分会更容易。下面的代码通过让两只海龟将两个条目放入给定数量的其他海龟档案以及它们自己的档案中来设置测试。然后它会以碎片为单位进行计数,最后将这些碎片放在一个命令中。在您的问题中,您使用 "all-turtles",但在 NetLogo 中这只是 turtles
。最后一条命令只查看海龟的随机子集。
turtles-own [ archive ]
to test
clear-all
create-turtles 100 [
set archive []
]
ask turtle 3 [
let archive-entry list "at home" self
set archive fput archive-entry archive
ask n-of 30 other turtles [ set archive fput archive-entry archive ]
set archive-entry list "not at home" self
set archive fput archive-entry archive
ask n-of 40 other turtles [ set archive fput archive-entry archive ]
]
ask turtle 4 [
let archive-entry list "at home" self
set archive fput archive-entry archive
ask n-of 20 other turtles [ set archive fput archive-entry archive ]
set archive-entry list "not at home" self
set archive fput archive-entry archive
ask n-of 50 other turtles [ set archive fput archive-entry archive ]
]
; get a list of all archives.
let list-of-all-archives [archive] of turtles
; make a list of the number of occurrences we are looking for in each archive.
let list-of-count-in-each-archive map [t -> length filter [a -> item 0 a = "at home" and item 1 a = turtle 3] t] list-of-all-archives
; sum up the number of occurences across all archives.
show sum list-of-count-in-each-archive
show sum map [t -> length filter [a -> item 0 a = "not at home" and item 1 a = turtle 3] t] [archive] of turtles
show sum map [t -> length filter [a -> item 0 a = "at home" and item 1 a = turtle 4] t] [archive] of turtles
show sum map [t -> length filter [a -> item 0 a = "not at home" and item 1 a = turtle 4] t] [archive] of turtles
show sum map [t -> length filter [a -> item 0 a = "not at home" and item 1 a = turtle 4] t] [archive] of n-of 50 turtles
end