<分区>
我正在翻译一些 python 代码,想知道 Python 的 pop() 函数是否可以在 R 中翻译。谢谢!
<分区>
我正在翻译一些 python 代码,想知道 Python 的 pop() 函数是否可以在 R 中翻译。谢谢!
最佳答案
您可以编写一个自定义函数来模仿 Python 弹出函数,但我只想补充一点,这种方法不符合我认为 R 应该实现相同任务的方式(我不喜欢玩全局变量)环境)。
此示例基于官方 python 文档:http://docs.python.org/2/tutorial/datastructures.html#using-lists-as-stacks
pop <- function(list, i = length(list)) {
stopifnot(inherits(list, "list"))
res <- list[[i]]
assign(deparse(substitute(list)), list[-i], envir = .GlobalEnv)
res
}
stack <- list(3, 4, 5, 6, 7)
pop(stack)
## [1] 7
stack
## [[1]]
## [1] 3
## [[2]]
## [1] 4
## [[3]]
## [1] 5
## [[4]]
## [1] 6
pop(stack)
## [1] 6
stack
## [[1]]
## [1] 3
## [[2]]
## [1] 4
## [[3]]
## [1] 5
关于r - 是否有 Python 的 pop() 函数的 R 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596538/