如何解析R中的python列表?

ℕʘʘ*_*ḆḽḘ 1 r dplyr tibble

考虑这个简单的例子

tibble(mylist = c("['this is some text from Python!', 'and this is another one!']",
                  "['this is also some cool stuff', 'and this is awesome!']"))
# A tibble: 2 x 1
 mylist                                                        
  <chr>                                                         
1 ['this is some text from Python!', 'and this is another one!']
2 ['this is also some cool stuff', 'and this is awesome!']      
Run Code Online (Sandbox Code Playgroud)

我想解析类似 python 的列表,以便 dplyr 理解这是一个句子列表(字符变量)。也就是说,像

> tibble(mylist = list(list('this is some text from Python!', 'and this is another one!'),
+                      list('this is also some cool stuff', 'and this is awesome!'))) 
# A tibble: 2 x 1
  mylist    
  <list>    
1 <list [2]>
2 <list [2]>
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?谢谢!

Ony*_*mbu 6

对于有效的 python 值,您可以使用reticulate包:

res<- tb %>%
  rowwise() %>%
  mutate(mylist=list(reticulate::py_eval(mylist)))

  mylist   
  <list>   
1 <chr [2]>
2 <chr [2]>
Run Code Online (Sandbox Code Playgroud)

输出:

res$mylist
[[1]]
[1] "this is some text from Python!" "and this is another one!"      

[[2]]
[1] "this is also some cool stuff" "and this is awesome!"  
Run Code Online (Sandbox Code Playgroud)

如果您想要与您的输出完全相似,则包括 as.list

res1<- tb %>%
      rowwise() %>%
      mutate(mylist=list(as.list(reticulate::py_eval(mylist))))%>%
      ungroup()
  mylist    
  <list>    
1 <list [2]>
2 <list [2]>
Run Code Online (Sandbox Code Playgroud)
all.equal(tb_res, res1)
[1] TRUE
Run Code Online (Sandbox Code Playgroud)