如何在使用嵌套列表中执行替换项目 [英] how to do replacing-item in use nested list

查看:74
本文介绍了如何在使用嵌套列表中执行替换项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

替换嵌套列表中的内容?

replacing in nested list ??

例如

[[1 2][3 4] [5 1]]

[[99 2][3 4] [5 1]]

任何人都可以澄清Netlogo中列表的使用吗?

Can anybody please clarify the use of lists in Netlogo?

推荐答案

您的问题未指定.您想如何确定要更改的项目?

Your question is somewhat underspecified. How do you want to decide which item gets changed?

我假设您想通过索引来执行此操作,例如第0个子列表的第0个项目".那就是:

I'll suppose that you want to do it by indices, e.g. "0th item of 0th sublist". Then that's:

to-report replace-subitem [index1 index2 lists value]
  let old-sublist item index1 lists
  report replace-item index1 lists (replace-item index2 old-sublist value)
end

observer> show replace-subitem 0 0 [[1 2] [3 4] [5 1]] 99
observer: [[99 2] [3 4] [5 1]]

您还可以想象根据其他条件进行更换.例如,假设我们要将子列表的第一项中所有出现的1都更改为99.然后是:

You could also imagine doing the replacement according to other criteria. For example, suppose we want to change all of the occurrences of 1 in the first item of a sublist to 99. Then that's:

to-report replace-first [old new the-list]
  if first the-list = old
    [ report replace-item 0 the-list new ]
  report the-list
end

to-report replace-firsts [old new lists]
  report map [replace-first old new ?] lists
end

observer> show replace-firsts 1 99 [[1 2] [3 4] [1 1]]
observer: [[99 2] [3 4] [99 1]]

取决于您要解决的问题,还有许多其他答案也是可能的.

A lot of other answers are possible as well, depending on exactly what problem you're trying to solve.

这篇关于如何在使用嵌套列表中执行替换项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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