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

Netlogo 检查列表中的两个连续项并计数频率,netlogo,Netlogo,我想检查列表中的两个或多个连续项是否相同,并查找列表中不同项的出现次数(频率)。 输入列表的一个例子是 [ (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 leng

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

[ (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])
但是,如果我有
[(项目211)(项目211)(项目187)]
,计数器将返回值
[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
            ]
          ]  

我希望你能帮助我。谢谢

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

您发布的代码示例有三个不同的问题

  • “item”是一个保留字。这是字典中的一条命令。将其用作变量 将产生不可预测的结果或无法通过编辑器的语法检查

    如果你仔细查看编辑器,单词“item”是紫色的,而不是黑色的 你应该知道这条线索。(感谢JenB在我尝试使用时向我指出了这一点 “e”是一个变量名,它有同样的问题。“e”是NetLogo中的一个命令。)

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

    你从哪儿弄来的?你写过自己的“频率”记者吗?如果是,, 你需要把它贴在这里,因为这可能也会引起问题

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

争论

(n-values length my-list [i -> i])
只生成一个序列号列表,如[01 2 3 4 5]。您不需要在我的列表中包含这样一个索引值列表,因为“map”已经隐式地包含在列表中的每个项目中。您需要的是实际的列表!因此,您所需要的是:

 let counter map [ i -> frequency I my-list]   my-list 
下面是一些使用新代码的工作代码,并演示了它的工作原理

顺便说一句,我在NetLogo用户词典中找到了名为“事件”的“频率”报告器的漂亮代码,这是使用命令“reduce”的一个例子

这里有一种方法可以检查列表中是否至少有一个连续项与前一项相同。我写这篇文章是为了打印出各种步骤,以便更清楚地了解它是如何工作的。我相信有人可以将所有这些压缩成更短更快的代码,但是,嘿,这就是工作,即使它使用了一个缓慢的“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”是一个保留字。这是字典中的一条命令。将其用作变量 将产生不可预测的结果或无法通过编辑器的语法检查

    如果你仔细查看编辑器,单词“item”是紫色的,而不是黑色的 你应该知道这条线索。(感谢JenB在我尝试使用时向我指出了这一点 “e”是一个变量名,它有同样的问题。“e”是NetLogo中的一个命令。)

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

    你从哪儿弄来的?你写过自己的“频率”记者吗?如果是,, 你需要把它贴在这里,因为这可能也会引起问题

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

争论

(n-values length my-list [i -> i])
只生成一个序列号列表,如[01 2 3 4 5]。您不需要在我的列表中包含这样一个索引值列表,因为“map”已经隐式地包含在列表中的每个项目中。您需要的是实际的列表!因此,您所需要的是:

 let counter map [ i -> frequency I my-list]   my-list 
下面是一些使用新代码的工作代码,并演示了它的工作原理

顺便说一句,我在NetLogo用户词典中找到了名为“事件”的“频率”报告器的漂亮代码,这是使用命令“reduce”的一个例子

这里有一种方法可以检查列表中是否至少有一个连续项与前一项相同。我写这篇文章是为了打印出各种步骤,以便更清楚地了解它是如何工作的。我相信有人可以将所有这些压缩成更短更快的代码,但是,嘿,这就是工作,即使它使用了一个缓慢的“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

请提供带有所需outputHi JenB的示例输入数据,输入数据由生成列表的代码创建(文章中的最后一个代码)。inf期望的输出是列表中相同的连续元素的计数以及列表中每个元素的频率。我在邮报上写的。如果仍然不清楚,请让我知道,我改变后,更具体。我不清楚。我们不关心如何创建列表,只关心其中一些列表的外观(这就是我所说的示例输入)。您只告诉我们您的代码正在生成什么,而不是您期望它生成什么。请提供示例输入数据和所需的outputHi JenB,输入数据由生成列表的代码创建(文章中的最后一个代码)。inf期望的输出是列表中相同的连续元素的计数以及列表中每个元素的频率。我在邮报上写的。如果仍然不清楚,请让我知道我更改了帖子t