gpt4 book ai didi

r - 修改列表的非递归版本?

转载 作者:行者123 更新时间:2023-12-01 13:36:42 24 4
gpt4 key购买 nike

modifyList 的行为是递归地修改列表 - 即,在这种情况下,fixed 是一个嵌套列表,也将被“修改”:

default.options <- list(a = 1, b = 2, c = "hello", fixed = list(a = 1))
user.options <- list(b = 3, e = 0, fixed = list())

opts <- modifyList(default.options, user.options)
dput(opts)
# list(a = 1, b = 3, c = "hello", fixed = list(a = 1), e = 0)

但是,如果我不希望它递归地跟随子列表 fixed ,而是将其作为一个整体替换,即期望的结果是这样的,该怎么办? :

list(a = 1, b = 3, c = "hello", fixed = list(), e = 0)

是否有一种简单的方法来进行这种非递归列表修改?

最佳答案

您可以简单地执行以下操作:

default.options[names(user.options)] <- user.options

identical(default.options, list(a = 1, b = 3, c = "hello", fixed = list(), e = 0))
[1] TRUE

关于r - 修改列表的非递归版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61543007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com