计算同一只乌龟在列表中的出现次数 [英] Count number of occurrences in a list from same turtle

查看:84
本文介绍了计算同一只乌龟在列表中的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在列表中找到项目的来源".对于此任务,我需要考虑的操作如下:

I would like to know how to find the 'source' of an item in a list. The actions that I need to consider for this task are the following:

1)在乌龟创建的列表中添加了一个项目;

1) an item is added in a list created by a turtle;

2)由于每个乌龟都有其自己的列表,其中包含由不同乌龟创建的物品,因此我想设置一个计数器,说明该乌龟多久选择一次甲龟的物品.

2) as each turtle has its own list with items created by different turtles, I would like to set a counter that says how often this turtle has picked turtle A's item.

APPROACH&代码:

这段代码在选定的乌龟列表中添加了一个名为"this_item"的项(局部变量):

This piece of code adds an item (local variable) called 'this_item' in the selected turtle's list:

ask one-of turtles [

              set archive fput this_item archive
]

和其他代码将相同的项目添加到邻居的列表中:

and this other code adds the same item to the neighbours' lists:

 ask in-link-neighbors [
              set archive fput this_item archive
]

我将设置一个局部变量,例如从列表中选择第一项.

I would set set a local variable, e.g. picked, as the first item from the list.

let picked first archive

要找到该项目的来源,我想使用who.但是,who用于乌龟,在将其提取后将其添加到自己的列表中.

To find the source of the item, I thought to use who. However, who is used for the turtle who adds the item to its own list, after extracted it.

print (word "Source: " who " has this list " list " after added item " picked)

如果我在乌龟创建新物品时使用自己将变量source定义为物品的来源,则此报告仅报告来源(学生)的品种,而不报告该来源的相应乌龟(例如学生2). 这使得无法计算选择一个来源项目的次数.

If I consider a variable source defined as the source of the item by using myself when a turtle creates a new item, this reports me only the breed of the source (student), but not the corresponding turtle of the source (e.g. student 2). This makes impossible to count how many times one source's item has been selected.

问题:

我如何计算同一只乌龟被选中的时间?

How can I count the number of time an item by the same turtle was selected?

在此先感谢您的帮助和建议.

Thanks in advance for your help and suggestions.

推荐答案

我想到的最简单的方法是让每个this-item是一个包含两个元素的列表,项目0本身就是项目",项目1是源乌龟.然后this-item将由带有

The easiest way I can think of to do this is to have each this-item be a two-element list, item 0 being the "item" itself and item 1 being the source turtle. this-item would then be created by a turtle with

set this-item list x self

(请注意,"item"本身是一个保留关键字,所以我的"item"是x.)任何将this-item放在列表中的乌龟都会知道item 0 this-item是事物本身,而item 1 this-item是它来的乌龟.要计算列表中由给定乌龟(例如,turtle 3)拥有的给定乌龟(例如,turtle 1)拥有的列表(例如,archive)的数量,您可以使用

(Note that "item" itself is a reserved keyword, so my "item" is x.) Any turtle that puts this-item in its list would then know that item 0 this-item was the thing itself and that item 1 this-item was the turtle from which it came. To count the number of entries in a list, say archive, owned by a given turtle, say turtle 1, from a given turtle, say turtle 3, you could use

ask turtle 1 [show length filter [a -> item 1 a = turtle 3] archive]

如果需要turtle 3在所有海龟档案中进行记录的次数,则可以分步进行.首先创建所有存档列表的列表.

If you need the number of times turtle 3 has made entries in all turtles' archives, you can do it in steps. First create a list of all the archive lists.

let all-archives [archives] of turtles

然后使用地图遍历该列表,以列出每个档案中的条目数列表并对该列表求和.

Then use map to go through that list to make a list of the number of entries in each archive and sum that list.

show sum map [t -> length filter [a -> item 1 a = turtle 3] t ] all-archives

(我现在无法进行测试,因此请检查语法.)当然,如果您愿意,turtles可以是所有海龟的子集.

(I can't test this right now so check the syntax.) Of course, turtles could be a subset of all turtles if you like.

希望这会有所帮助, 查尔斯

Hope this helps, Charles

这篇关于计算同一只乌龟在列表中的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆