英文:
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 <- list()
green <- list()
red <- 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 <- blue <- green <- list()
ls()
[1] "blue" "green" "red"
Note, that the lists at first have the same memory address,
sapply(list(red, blue, green), data.table::address)
# [1] "0x55ee16ce46a0" "0x55ee16ce46a0" "0x55ee16ce46a0"
but it will change as soon the objects are manipulated (i.e. a copy is created).
red[[1]] <- 'foo'; blue[[1]] <- 'foo'; green[[1]] <- 'foo'
sapply(list(red, blue, green), data.table::address)
# [1] "0x55ee16f1ea48" "0x55ee16f1e930" "0x55ee16f1e818"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论