2016-07-04 280 views
-1

我有一個列表列表的清單列表,它是列表列表的一個R列表上應用的某些功能的結果(轉換爲Sage格式)將Sage列表列表轉換爲R列表列表

的代碼是這樣的:

%r 
my_r_list<-load("my_r_list.RData") 

for d in range(D): 
    for s in range(S): 
     for c in range(C): 
      my_sage_list[d][s][c] = ("my_r_list")[[d+1]][[s+1]][[c+1]]._sage_() 
      output_my_sage_list[d][s][c] = some_function(my_sage_list[d][s][c]) 

我想output_my_sage_list轉換回成R列表,以便它有R保存到一個.RData文件的接口賢者

文檔主要是關於從去R到Sage,但不是另一種方式d。 This question使用r.matrix()' so i tried using 'r.list()' but with no luck. I also tried the simple將sagemath矩陣轉換爲R矩陣output_my_r_list = r(output_my_sage_list)which gives no error but the output is not correct as doing output_my_r_list [0] [0]`不給出列表的最後一級,而是直接給出一些值。

一個可重複的代碼(繞過R鍵賢者部分)將是:

output_d = [[] for _ in range(9)] 
    for d in range(9): 
     output_s = [[] for _ in range(4)] 
     output_d[d] = output_s 
     for s in range(4): 
      output_c = [[] for _ in range(2)] 
      output_s[s] = output_c 
      for k in range(2): 
       res = random() 
       output_c[k] = res 

我想output_d轉換的R列表

回答

0

這裏的關鍵是你的發言

我也嘗試了簡單的output_my_r_list = r(output_my_sage_list),它不給出錯誤,但輸出不正確,因爲做output_my_r_list[0][0]不給列表的最後一級,但直接一些val UE的。

賢者實際上文件,這將使列表只是一個連續清單,或者更恰當的R 矢量

sage: x = r([10.4,5.6,3.1,6.4,21.7]); x 
    [1] 10.4 5.6 3.1 6.4 21.7 

The following assignment creates a vector $y$ with 11 entries which 
consists of two copies of $x$ with a 0 in between:: 

    sage: y = r([x,0,x]); y 
    [1] 10.4 5.6 3.1 6.4 21.7 0.0 10.4 5.6 3.1 6.4 21.7 

這是直接modeled upon R usage,想必(通過一些嵌套程序)甚至只是來自於它:

The further assignment 

> y <- c(x, 0, x) 

would create a vector y with 11 entries consisting of two copies of 
with a zero in the middle place. 

所以你的牛肉是不完全與鼠尾草,但R,因爲(在某種程度上)賢者在這裏得到「正確」的事情。賢者這樣做是因爲here它說

def _left_list_delim(self): 
    return "c(" 

,然後將R行爲遵循,因爲這是完成遞歸,它使向量的向量。

對此的破解將在那裏返回"list("。另一個黑客將嘗試使用r.eval("list(something)"),但我沒有經歷過很多成功與此。

這將是很好找到一種方式來處理這個更容易,當然,並在返回方向Sage attempts to take nested R stuff and keep it nested(有什麼成功,我不知道)。