将一个对象附加到 R 中的一个列表中,以摊销的常数时间,O(1)? [英] Append an object to a list in R in amortized constant time, O(1)?

查看:22
本文介绍了将一个对象附加到 R 中的一个列表中,以摊销的常数时间,O(1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一些 R 列表 mylist,你可以像这样附加一个 obj 项目:

If I have some R list mylist, you can append an item obj to it like so:

mylist[[length(mylist)+1]] <- obj

但肯定有一些更紧凑的方式.当我刚接触 R 时,我尝试像这样编写 lappend():

But surely there is some more compact way. When I was new at R, I tried writing lappend() like so:

lappend <- function(lst, obj) {
    lst[[length(lst)+1]] <- obj
    return(lst)
}

但是由于 R 的按名称调用语义,这当然不起作用(lst 在调用时被有效地复制,因此对 lst 的更改在外部不可见lappend() 的范围.我知道您可以在 R 函数中进行环境黑客攻击以达到您的函数范围之外并改变调用环境,但这似乎是编写一个简单的大锤子追加函数.

but of course that doesn't work due to R's call-by-name semantics (lst is effectively copied upon call, so changes to lst are not visible outside the scope of lappend(). I know you can do environment hacking in an R function to reach outside the scope of your function and mutate the calling environment, but that seems like a large hammer to write a simple append function.

谁能建议一种更漂亮的方式来做到这一点?如果它适用于向量和列表,则加分.

Can anyone suggest a more beautiful way of doing this? Bonus points if it works for both vectors and lists.

推荐答案

如果是字符串列表,只需使用c()函数:

If it's a list of string, just use the c() function :

R> LL <- list(a="tom", b="dick")
R> c(LL, c="harry")
$a
[1] "tom"

$b
[1] "dick"

$c
[1] "harry"

R> class(LL)
[1] "list"
R> 

这也适用于向量,所以我可以获得奖励积分吗?

That works on vectors too, so do I get the bonus points?

编辑(2015 年 2 月 1 日): 这篇文章即将迎来五岁生日.一些好心的读者不断重复它的任何缺点,所以一定要看看下面的一些评论.list 类型的一项建议:

Edit (2015-Feb-01): This post is coming up on its fifth birthday. Some kind readers keep repeating any shortcomings with it, so by all means also see some of the comments below. One suggestion for list types:

newlist <- list(oldlist, list(someobj))

一般来说,R 类型很难让所有类型和用途都只有一个习语.

In general, R types can make it hard to have one and just one idiom for all types and uses.

这篇关于将一个对象附加到 R 中的一个列表中,以摊销的常数时间,O(1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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