List Netlogo-如何获取列表中连续项目的频率?

List Netlogo-如何获取列表中连续项目的频率?,list,netlogo,agent-based-modeling,List,Netlogo,Agent Based Modeling,如何在Netlogo中获取列表中连续数字的频率 例如,如果我的列表是: list = [1 1 1 0 0 1 1 0 1 1 0 0 0 ] 然后输出应如下所示: output = [3 2 2 1 2 3] 这不是一个代码编写服务,但我得到了,所以给你。我已经决定这是递归的工作: to-report count-consecutive-items [ xs ] report count-consecutive-items-loop [] nobody xs end to-repor

如何在Netlogo中获取列表中连续数字的频率

例如,如果我的列表是:

list = [1 1 1 0 0 1 1 0 1 1 0 0 0 ]
然后输出应如下所示:

output = [3 2 2 1 2 3]

这不是一个代码编写服务,但我得到了,所以给你。我已经决定这是递归的工作:

to-report count-consecutive-items [ xs ]
  report count-consecutive-items-loop [] nobody xs
end

to-report count-consecutive-items-loop [ counts-so-far last-item remaining-items ]
  report ifelse-value (empty? remaining-items) [
    ; no more items to count, return the counts as they are
    counts-so-far
  ] [
    (count-consecutive-items-loop
      (ifelse-value (first remaining-items = last-item) [
        ; this is the same item as the last,
        ifelse-value (empty? counts-so-far) [
          ; if our list of counts is empty, start a new one
          [1]
        ] [
          ; add one to the current count and keep going
          replace-item (length counts-so-far - 1) counts-so-far (1 + last counts-so-far)          
        ]
      ] [
        ; this is an item we haven't seen before: start a new count
        lput 1 counts-so-far
      ])
      (first remaining-items)
      (but-first remaining-items)
    )
  ]
end

to test
  let result count-consecutive-items [1 1 1 0 0 1 1 0 1 1 0 0 0]
  print result
  print result = [3 2 2 1 2 3]
end
<>我确信其他人可以想出一个很好的命令版本,比这更容易理解,但是你可以把它看作是一个教学练习:如果你能理解这段代码,它将帮助你在NetLogo启蒙运动的路上。


(我现在没有时间写解释,但如果您有什么特别需要帮助的地方,请在评论中提问。)

这不是一项代码编写服务,但我得到了,给您。我已经决定这是递归的工作:

to-report count-consecutive-items [ xs ]
  report count-consecutive-items-loop [] nobody xs
end

to-report count-consecutive-items-loop [ counts-so-far last-item remaining-items ]
  report ifelse-value (empty? remaining-items) [
    ; no more items to count, return the counts as they are
    counts-so-far
  ] [
    (count-consecutive-items-loop
      (ifelse-value (first remaining-items = last-item) [
        ; this is the same item as the last,
        ifelse-value (empty? counts-so-far) [
          ; if our list of counts is empty, start a new one
          [1]
        ] [
          ; add one to the current count and keep going
          replace-item (length counts-so-far - 1) counts-so-far (1 + last counts-so-far)          
        ]
      ] [
        ; this is an item we haven't seen before: start a new count
        lput 1 counts-so-far
      ])
      (first remaining-items)
      (but-first remaining-items)
    )
  ]
end

to test
  let result count-consecutive-items [1 1 1 0 0 1 1 0 1 1 0 0 0]
  print result
  print result = [3 2 2 1 2 3]
end
<>我确信其他人可以想出一个很好的命令版本,比这更容易理解,但是你可以把它看作是一个教学练习:如果你能理解这段代码,它将帮助你在NetLogo启蒙运动的路上。


(我现在没有时间写解释,但如果您有什么特别需要帮助的地方,请在评论中提问。)

您到目前为止尝试了什么,其结果是什么。请注意StackOverflow不是一个代码编写网站。请提供您迄今为止尝试的最低代码、详细信息,以便我们可以帮助您。谢谢您的回复,但我不知道如何启动代码,我问了!在这种情况下,你甚至不知道如何开始,一个好的方法是使问题变得更简单;解决那个简单的问题;然后尝试扩展解决方案以解决整个问题。(你可以从一个非常简单的问题开始,然后逐渐变得更复杂)如果在任何时候你真的陷入困境,你会有一个问题要问,而不仅仅是“为我写代码”-您可以展示上一个成功解决方案的代码,并解释进一步推广该解决方案时遇到的困难。到目前为止,您尝试了什么,其结果是什么。请注意,StackOverflow不是一个代码编写网站。请提供您迄今为止尝试的最低代码、详细信息,以便我们可以帮助您。谢谢您的回复,但我不知道如何启动代码,我问了!在这种情况下,你甚至不知道如何开始,一个好的方法是使问题变得更简单;解决那个简单的问题;然后尝试扩展解决方案以解决整个问题。(你可以从一个非常简单的问题开始,然后逐渐变得更复杂)如果在任何时候你真的陷入困境,你会有一个问题要问,而不仅仅是“为我写代码”-您可以显示上一个成功解决方案的代码,并解释在进一步推广时遇到的困难。感谢Nicolas Payette花宝贵的时间编写代码!我理解代码谢谢Nicolas Payette花宝贵的时间编写代码!我理解密码