如何在R中设置此Python数据结构

如何在R中设置此Python数据结构,python,r,Python,R,我想在R中设置以下Python数据结构。实现这一点的正确方法是什么,这与Python设置最为相似 testing = [ [[12,14], [4]], [[2,1], [5]], [[42,11], [13]] ] 编辑1 基于agstudy提出的解决方案,使用以下代码 library(rjson) json_file <- "/Path/JSONdata.json" json_data <- fromJSON(paste

我想在R中设置以下Python数据结构。实现这一点的正确方法是什么,这与Python设置最为相似

testing = [
        [[12,14], [4]],
        [[2,1], [5]],
        [[42,11], [13]]
    ]
编辑1

基于agstudy提出的解决方案,使用以下代码

library(rjson)

json_file <- "/Path/JSONdata.json"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

这是什么?嵌套列表类型结构

ll <- list( c( list(c(12,14)),4) , c(list(c(2,1)),5),c(list(c(42,11)),13))

str( ll )
List of 3
 $ :List of 2
  ..$ : num [1:2] 12 14
  ..$ : num 4
 $ :List of 2
  ..$ : num [1:2] 2 1
  ..$ : num 5
 $ :List of 2
  ..$ : num [1:2] 42 11
  ..$ : num 13

这是什么?嵌套列表类型结构

ll <- list( c( list(c(12,14)),4) , c(list(c(2,1)),5),c(list(c(42,11)),13))

str( ll )
List of 3
 $ :List of 2
  ..$ : num [1:2] 12 14
  ..$ : num 4
 $ :List of 2
  ..$ : num [1:2] 2 1
  ..$ : num 5
 $ :List of 2
  ..$ : num [1:2] 42 11
  ..$ : num 13

这不是答案,但可能是将结构从python转换为R的一般方法。为什么不使用中间格式将python结构转换为R结构呢。例如,通过
json
格式

python R 您可以这样使用python结果:

library(RJSONIO)
str(fromJSON("testing.json"))
List of 3
 $ :List of 2
  ..$ : num [1:2] 12 14
  ..$ : num 4
 $ :List of 2
  ..$ : num [1:2] 2 1
  ..$ : num 5
 $ :List of 2
  ..$ : num [1:2] 42 11
  ..$ : num 13

这不是答案,但可能是将结构从python转换为R的一般方法。为什么不使用中间格式将python结构转换为R结构呢。例如,通过
json
格式

python R 您可以这样使用python结果:

library(RJSONIO)
str(fromJSON("testing.json"))
List of 3
 $ :List of 2
  ..$ : num [1:2] 12 14
  ..$ : num 4
 $ :List of 2
  ..$ : num [1:2] 2 1
  ..$ : num 5
 $ :List of 2
  ..$ : num [1:2] 42 11
  ..$ : num 13


我刚刚试过你的代码,它可以用
RJSONIO
而不是
rjson
工作。你有什么特别的理由选择rjson吗?@dickoa谢谢!没有什么特别的原因(过去RJSONIO的速度更快,但我认为,
rjson
只是赶上了)。你能用你的结果编辑你的答案吗?(我仍在进行一些安装)非常好的解决方案。非常感谢你。导入后,它会生成:list(list(c(12,14),4),list(c(2,1),5),list(c(42,11,13)),我不是一个流利的pythonista,所以我不知道这是否是将
json.dump
写入文件的标准方式。我刚刚尝试了你的代码,但它与
RJSONIO
一起工作,而不是
rjson
。你有什么特别的理由选择rjson吗?@dickoa谢谢!没有什么特别的原因(过去RJSONIO的速度更快,但我认为,
rjson
只是赶上了)。你能用你的结果编辑你的答案吗?(我仍在进行一些安装)非常好的解决方案。非常感谢你。导入后,它会生成:list(list(c(12,14),4),list(c(2,1),5),list(c(42,11,13)),我不是一个流利的pythonista,所以我不知道这是否是将
json.dump
写入文件的标准方式。谢谢你的回答。我认为您不需要太多的c()函数。检查我的编辑。@jtromans哈哈,是的。很高兴你对它进行了排序,并且从agstudy得到了一个有用的答案。如果我想选择Pat[[:][[1]],而不是Pat[[3]][[1]],我该如何实现这一点?
[[:]]
表示所有顶级元素?是的,我想选择,例如,所有顶级列表元素中的第一个列表元素。我从Matlab借用了冒号符号。谢谢你的回答。我认为您不需要太多的c()函数。检查我的编辑。@jtromans哈哈,是的。很高兴你对它进行了排序,并且从agstudy得到了一个有用的答案。如果我想选择Pat[[:][[1]],而不是Pat[[3]][[1]],我该如何实现这一点?
[[:]]
表示所有顶级元素?是的,我想选择,例如,所有顶级列表元素中的第一个列表元素。我从Matlab借用了冒号符号。
library(RJSONIO)
str(fromJSON("testing.json"))
List of 3
 $ :List of 2
  ..$ : num [1:2] 12 14
  ..$ : num 4
 $ :List of 2
  ..$ : num [1:2] 2 1
  ..$ : num 5
 $ :List of 2
  ..$ : num [1:2] 42 11
  ..$ : num 13