如何使用向矩阵添加元素?
How to use add elements to a matrix?
我想将一个列表中的多个项目添加到另一个列表中,该列表组织在一个大列表中,就像一个矩阵。
let targetlists (list firstlist seccondlist thirdlist)
所以在我的双 while 循环中,我添加了这段代码;
set (item x targetlists) lput (item y sourcelist) (item x targetlists)
遗憾的是它给了我以下错误:
This isn't something you can use "set" on.
我发现这与我 select 目标列表的方式有关,因为以下代码确实有效,但没有按照我的意愿进行:
set firstlist lput (item y sourcelist) firstlist
JenB 是对的,一般来说,您使用 replace-item
。但是,将 while
循环替换为 map
会更有效。
我不完全确定您要做什么,但看起来您正试图将 sourcelist
的元素放到 targetlists
列表的末尾。即使这不是您正在做的,这也应该为您指明正确的方向:
set targetlists (map [ [ source-item target-row ] ->
lput source-item target-row
] sourcelist targetlists)
这将一起遍历 sourcelist
和 targetlists
的项目,在对上调用 lput
。
此外,还有一个方便的快捷方式,如果记者已经做了您想要的,您可以直接将其传递给 map
。所以你可以将其压缩为:
set targetlists (map lput sourcelist targetlists)
现在,鉴于您提到了嵌套的 while
s 并且您正在使用两个不同的索引对两个列表进行索引,您可能会尝试将 sourcelist
的全部内容放在末尾每个 targetlists
。如果是这样的话,你可以这样做
set targetlists map [ l -> (sentence l sourcelist) ] targetlists
如果我完全不在乎,而你正在尝试做一些完全不同的事情,请在评论中告诉我,我会更新我的答案。
我想将一个列表中的多个项目添加到另一个列表中,该列表组织在一个大列表中,就像一个矩阵。
let targetlists (list firstlist seccondlist thirdlist)
所以在我的双 while 循环中,我添加了这段代码;
set (item x targetlists) lput (item y sourcelist) (item x targetlists)
遗憾的是它给了我以下错误:
This isn't something you can use "set" on.
我发现这与我 select 目标列表的方式有关,因为以下代码确实有效,但没有按照我的意愿进行:
set firstlist lput (item y sourcelist) firstlist
JenB 是对的,一般来说,您使用 replace-item
。但是,将 while
循环替换为 map
会更有效。
我不完全确定您要做什么,但看起来您正试图将 sourcelist
的元素放到 targetlists
列表的末尾。即使这不是您正在做的,这也应该为您指明正确的方向:
set targetlists (map [ [ source-item target-row ] ->
lput source-item target-row
] sourcelist targetlists)
这将一起遍历 sourcelist
和 targetlists
的项目,在对上调用 lput
。
此外,还有一个方便的快捷方式,如果记者已经做了您想要的,您可以直接将其传递给 map
。所以你可以将其压缩为:
set targetlists (map lput sourcelist targetlists)
现在,鉴于您提到了嵌套的 while
s 并且您正在使用两个不同的索引对两个列表进行索引,您可能会尝试将 sourcelist
的全部内容放在末尾每个 targetlists
。如果是这样的话,你可以这样做
set targetlists map [ l -> (sentence l sourcelist) ] targetlists
如果我完全不在乎,而你正在尝试做一些完全不同的事情,请在评论中告诉我,我会更新我的答案。