如何同时创建多个空列表?

huangapple go评论60阅读模式
英文:

How to create several empty lists at once?

问题

我想创建3个空列表并将它们分配给不同的变量。

blue <- list()
green <- list()
red <- list()

是否有可能进行同时赋值?

英文:

I'd like to create 3 empty lists and assign them to different variables.

blue &lt;- list()
green &lt;- list()
red &lt;- list()

Is there a possibility of a simultaneous assignment?

答案1

得分: 2

是的,这是可能的。

红色 <- 蓝色 <- 绿色 <- 列表()

ls()
[1] "蓝色"  "绿色" "红色"  

请注意,初始时这些列表具有相同的内存地址,

sapply(list(红色, 蓝色, 绿色), data.table::address)
# [1] "0x55ee16ce46a0" "0x55ee16ce46a0" "0x55ee16ce46a0"

但一旦对象被操作(即创建了一个副本),地址就会改变。

红色[[1]] <- 'foo'; 蓝色[[1]] <- 'foo'; 绿色[[1]] <- 'foo'

sapply(list(红色, 蓝色, 绿色), data.table::address)
# [1] "0x55ee16f1ea48" "0x55ee16f1e930" "0x55ee16f1e818"
英文:

Yes, it's possible.

red &lt;- blue &lt;- green &lt;- list()

ls()
[1] &quot;blue&quot;  &quot;green&quot; &quot;red&quot;  

Note, that the lists at first have the same memory address,

sapply(list(red, blue, green), data.table::address)
# [1] &quot;0x55ee16ce46a0&quot; &quot;0x55ee16ce46a0&quot; &quot;0x55ee16ce46a0&quot;

but it will change as soon the objects are manipulated (i.e. a copy is created).

red[[1]] &lt;- &#39;foo&#39;; blue[[1]] &lt;- &#39;foo&#39;; green[[1]] &lt;- &#39;foo&#39;

sapply(list(red, blue, green), data.table::address)
# [1] &quot;0x55ee16f1ea48&quot; &quot;0x55ee16f1e930&quot; &quot;0x55ee16f1e818&quot;

huangapple
  • 本文由 发表于 2023年6月6日 02:39:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定