检查列表中的两个连续项目并计数频率



我想检查列表中的两个或多个连续项目是否相同,并查找列表中不同项目的出现次数(频率(。 输入列表的一个例子是

[ (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)],计数器会[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
]
]  

我希望你能帮助我。谢谢

以下是有关如何检查列表中项目频率的答案。

您发布的代码示例存在三个不同的问题。

  • "项目"是一个保留字。它是字典中的命令。将其用作变量 将产生不可预知的结果或未通过编辑器的语法检查。

    如果你仔细观察编辑器,"项目"这个词是紫色的,而不是黑色的,给 你这个线索。 (感谢 JenB 在我尝试使用 "e"作为变量名,有同样的问题。"e"是NetLogo中的一个命令。

  • 即使允许将"item"作为品种类型,也没有名为 "频率"。 我在任何通常的扩展中都找不到这样的命令。

    你从哪里得到的?你写了自己的"频率"记者吗?㞖 您需要在此处发布它,因为这也可能导致问题。

  • 最后,在您编写的命令中

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 ]。 你不需要这样的索引值列表到my-list中,因为"map"已经隐式地向下移动列表中的每个项目。您需要的是实际列表! 所以你所需要的只是这个:

let counter map [ i -> frequency I my-list]   my-list 

下面是一些使用该新代码并演示其工作有效的工作代码。

顺便说一下,我在NetLogo用户词典中找到了名为"出现次数"的">频率"报告器的漂亮代码,作为命令"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

最新更新