检查列表中的两个连续项目并计算频率
Checking two consecutive items in a list and count frequency
我想检查列表中的两个或多个连续项目是否相同,以及查找列表中不同项目的出现次数(频率)。
输入列表的一个例子是
[ (item 1) (item 2) (item 2) (item 4) (item 1) (item 2) ...]
我想做的是检查两个或多个相同的项目是否连续(例如项目2)以及每个项目的频率(例如项目1只有两次,项目2有三次,项目4只有一次) .
对于频率,我写了
let counter map [ i -> frequency I my-list] (n-values length my-list [i -> i])
但是,如果我有 [(item 211) (item 211) (item 187)]
,计数器 returns 我的值 [2 2 2]
。
检查物品,不知道怎么办
我的列表构建如下:
set selected people
set color blue
hatch-items 1 [
hide-turtle
set me selected
set this-item self
ask myself [
set my-list fput this-item my-list
]
]
希望你能帮助我。谢谢
这是关于如何检查列表中项目频率的答案。
您 post 编辑的代码示例存在三个不同的问题。
"item" 是保留字。这是字典中的命令。将其用作变量
将有不可预测的结果或无法通过编辑器的语法检查。
如果仔细看编辑器,"item"这个词是紫色的,不是黑色的,给
你这个线索。 (感谢 JenB 在我尝试使用时向我指出了这一点
"e" 作为变量名,也有同样的问题。 "e" 是 NetLogo 中的命令。)
即使 "item" 被允许作为品种类型,也没有名为
"frequency"。我在任何常用扩展中也找不到这样的命令。
你从哪里得到这个的?你自己写了 "frequency" 记者吗?如果是这样,
您需要在此处 post 因为这也可能导致问题。
终于在你写的命令中
let counter map [ i -> frequency I my-list] (n-values length my-list [i -> i])
争论
(n-values length my-list [i -> i])
只是生成一个序列号列表,例如 [ 0 1 2 3 4 5 ]。您 不需要 将这样的索引值列表放入我的列表中,因为 "map" 已经隐式地向下列出了列表中的每个项目。您做需要的是实际列表!所以你只需要这个:
let counter map [ i -> frequency I my-list] my-list
这里有一些工作代码使用了新代码并证明它可以工作。
顺便说一下,我在 NetLogo 用户词典中找到了 "frequency" 记者 的 nifty 代码作为示例之一命令的使用次数 "reduce".
globals [
my-list
]
;; item is a reserved word by the way. You need to use a different name.
to setup
clear-all
set my-list []
;; generate a sample my-list of agents to test this code
create-turtles 3 [ set my-list fput self my-list]
ask one-of turtles [ set my-list fput self my-list]
ask one-of turtles [ set my-list fput self my-list]
;;set my-list [ 33 44 55 33 10 33 44 0 ] ;; a simpler test
reset-ticks
end
to go
show my-list
let counter map [ i -> frequency I my-list] my-list
show counter
tick
end
to-report frequency [x the-list]
;; this snippet of code is given as an example in the NetLogo Dictionary when defining "reduce"
report reduce
[ [occurrence-count next-item] -> ifelse-value (next-item = x) [occurrence-count + 1] [occurrence-count] ] (fput 0 the-list)
end
这是检查列表中至少一个连续项目是否与前一个项目相同的方法。我写这篇文章是为了打印出各个步骤,以便更清楚地了解它是如何工作的。我敢肯定有人可以将所有这些压缩成更短更快的代码,但是,嘿,即使它使用缓慢的 "foreach" 测试,它也能完成工作。
我利用了 "foreach" 可以将一个列表中的每个项目与第二个列表中的相应项目进行比较的事实
;; I would like to check if two or more consecutive items in a list are the same
globals [
my-list
my-shifted-list
]
to setup
clear-all
set my-list []
;; generate a sample my-list of agents to test this code
create-turtles 3 [ set my-list fput self my-list]
ask one-of turtles [ set my-list fput self my-list]
ask one-of turtles [ set my-list fput self my-list]
; set my-list [ 333 4 5 333 333 3 333 4 0 ] ;; a simpler test
; set my-list [ 1 2 3 4 5 6 7 8 9 ] ;; a simpler test
show my-list
reset-ticks
end
to go
show has-sequential-duplicates my-list
end
to-report has-sequential-duplicates [ a-list ]
;; create a second list by shifting my-list one place to the left.
;; add a fake item to the end of the second list so it is the same length as my-list
;; so that the "foreach" command will work
let templist remove-item 0 a-list
set my-shifted-list lput -999 templist ;; add something that will never be in my-list
show my-shifted-list ;; this is one item shorted than a-list
let dup-count 0
;; compare lists and count occurrences of identical items in the same place in each list
(foreach my-list my-shifted-list [ [ a b ] ->
show ( word " Comparing " a " to " b )
if ( a = b ) [ set dup-count (dup-count + 1) ] ])
print word "count of duplicates: " dup-count
if-else ( dup-count > 0 ) [ report true ][ report false ]
end
我想检查列表中的两个或多个连续项目是否相同,以及查找列表中不同项目的出现次数(频率)。 输入列表的一个例子是
[ (item 1) (item 2) (item 2) (item 4) (item 1) (item 2) ...]
我想做的是检查两个或多个相同的项目是否连续(例如项目2)以及每个项目的频率(例如项目1只有两次,项目2有三次,项目4只有一次) . 对于频率,我写了
let counter map [ i -> frequency I my-list] (n-values length my-list [i -> i])
但是,如果我有 [(item 211) (item 211) (item 187)]
,计数器 returns 我的值 [2 2 2]
。
检查物品,不知道怎么办
我的列表构建如下:
set selected people
set color blue
hatch-items 1 [
hide-turtle
set me selected
set this-item self
ask myself [
set my-list fput this-item my-list
]
]
希望你能帮助我。谢谢
这是关于如何检查列表中项目频率的答案。
您 post 编辑的代码示例存在三个不同的问题。
"item" 是保留字。这是字典中的命令。将其用作变量 将有不可预测的结果或无法通过编辑器的语法检查。
如果仔细看编辑器,"item"这个词是紫色的,不是黑色的,给 你这个线索。 (感谢 JenB 在我尝试使用时向我指出了这一点 "e" 作为变量名,也有同样的问题。 "e" 是 NetLogo 中的命令。)
即使 "item" 被允许作为品种类型,也没有名为 "frequency"。我在任何常用扩展中也找不到这样的命令。
你从哪里得到这个的?你自己写了 "frequency" 记者吗?如果是这样, 您需要在此处 post 因为这也可能导致问题。
终于在你写的命令中
let counter map [ i -> frequency I my-list] (n-values length my-list [i -> i])
争论
(n-values length my-list [i -> i])
只是生成一个序列号列表,例如 [ 0 1 2 3 4 5 ]。您 不需要 将这样的索引值列表放入我的列表中,因为 "map" 已经隐式地向下列出了列表中的每个项目。您做需要的是实际列表!所以你只需要这个:
let counter map [ i -> frequency I my-list] my-list
这里有一些工作代码使用了新代码并证明它可以工作。
顺便说一下,我在 NetLogo 用户词典中找到了 "frequency" 记者 的 nifty 代码作为示例之一命令的使用次数 "reduce".
globals [
my-list
]
;; item is a reserved word by the way. You need to use a different name.
to setup
clear-all
set my-list []
;; generate a sample my-list of agents to test this code
create-turtles 3 [ set my-list fput self my-list]
ask one-of turtles [ set my-list fput self my-list]
ask one-of turtles [ set my-list fput self my-list]
;;set my-list [ 33 44 55 33 10 33 44 0 ] ;; a simpler test
reset-ticks
end
to go
show my-list
let counter map [ i -> frequency I my-list] my-list
show counter
tick
end
to-report frequency [x the-list]
;; this snippet of code is given as an example in the NetLogo Dictionary when defining "reduce"
report reduce
[ [occurrence-count next-item] -> ifelse-value (next-item = x) [occurrence-count + 1] [occurrence-count] ] (fput 0 the-list)
end
这是检查列表中至少一个连续项目是否与前一个项目相同的方法。我写这篇文章是为了打印出各个步骤,以便更清楚地了解它是如何工作的。我敢肯定有人可以将所有这些压缩成更短更快的代码,但是,嘿,即使它使用缓慢的 "foreach" 测试,它也能完成工作。
我利用了 "foreach" 可以将一个列表中的每个项目与第二个列表中的相应项目进行比较的事实
;; I would like to check if two or more consecutive items in a list are the same
globals [
my-list
my-shifted-list
]
to setup
clear-all
set my-list []
;; generate a sample my-list of agents to test this code
create-turtles 3 [ set my-list fput self my-list]
ask one-of turtles [ set my-list fput self my-list]
ask one-of turtles [ set my-list fput self my-list]
; set my-list [ 333 4 5 333 333 3 333 4 0 ] ;; a simpler test
; set my-list [ 1 2 3 4 5 6 7 8 9 ] ;; a simpler test
show my-list
reset-ticks
end
to go
show has-sequential-duplicates my-list
end
to-report has-sequential-duplicates [ a-list ]
;; create a second list by shifting my-list one place to the left.
;; add a fake item to the end of the second list so it is the same length as my-list
;; so that the "foreach" command will work
let templist remove-item 0 a-list
set my-shifted-list lput -999 templist ;; add something that will never be in my-list
show my-shifted-list ;; this is one item shorted than a-list
let dup-count 0
;; compare lists and count occurrences of identical items in the same place in each list
(foreach my-list my-shifted-list [ [ a b ] ->
show ( word " Comparing " a " to " b )
if ( a = b ) [ set dup-count (dup-count + 1) ] ])
print word "count of duplicates: " dup-count
if-else ( dup-count > 0 ) [ report true ][ report false ]
end