2017-05-11 103 views
1

我想將現有的python函數轉換爲lua函數。但我的lua函數並不像python函數那樣產生相同的結果。任何幫助表示讚賞。將Python函數轉換爲Lua函數

Python函數:

import json 

test = '{"http://localhost:8080/":{"phone":{"-detail/phone detail.template.html":"5167n,a,7,2","s/motorola-xoom-with-wifi.json":"516a0,5,4,3"},"favicon.ico":"016ad,3,3,2","img/phones/motorola-xoom-with-wi-fi.":{"1.jpg":"*02s,2s,4v,h3|116da,o,l,6","2.jpg":"*02s,2s,4v,kp|116da,j,i,8","3.jpg":"*02s,2s,4v,ob|116da,o,m,8,7,,7,7,7","4.jpg":"*02s,2s,4v,rx|116da,o,m,9,8,,7,7,7","5.jpg":"*02s,2s,4v,vj|116da,p,m,a,8,,7,7,7"}}}' 


def tri(param): 
    t = {} 
    for key in param: 
    if key not in param: 
     continue 
    if isinstance(param[key], dict) and param[key] is not None: 
     flat = tri(param[key]) 
     for x in flat: 
      if x not in flat: 
       continue 
      t[key + x] = flat[x] 
    else: 
     t[key] = param[key] 
return t 


print(tri(json.loads(test))) 

Lua代碼(其不產生相同的結果蟒功能)

local json = require('cjson') 

local test = '{"http://localhost:8080/":{"phone":{"-detail/phone-detail.template.html":"5167n,a,7,2","s/motorola-xoom-with-wi-fi.json":"516a0,5,4,3"},"favicon.ico":"016ad,3,3,2","img/phones/motorola-xoom-with-wi-fi.":{"1.jpg":"*02s,2s,4v,h3|116da,o,l,6","2.jpg":"*02s,2s,4v,kp|116da,j,i,8","3.jpg":"*02s,2s,4v,ob|116da,o,m,8,7,,7,7,7","4.jpg":"*02s,2s,4v,rx|116da,o,m,9,8,,7,7,7","5.jpg":"*02s,2s,4v,vj|116da,p,m,a,8,,7,7,7"}}}' 

local function tri(param) 
    t = {} 
    for key in pairs(param) do 
     if param[key] == nil then end 
     if type(param[key]) == "table" then 
     flat = tri(param[key]) 
     for k in pairs(flat) do 
      t[key .. k] = flat[k] 
     end 
     else 
     t[key] = param[key] 
     end 
    end 
    return t 
end 


print(json.encode(tri(json.decode(test)))) 
+1

如果你告訴人們該函數應該做什麼以及它做什麼,變量應該是可能的。只要讓你自己清楚你的表t發生了什麼,因爲每當你調用tri()時它就是全局的。 – Piglet

回答

3
local function tri(param) 
    t = {}   -- every time we call tri t will be "reset" to an empty table 
    for key in pairs(param) do 
     if param[key] == nil then end 
     if type(param[key]) == "table" then 
     flat = tri(param[key]) -- here we call tri, but we still need t! 
     for k in pairs(flat) do 
      t[key .. k] = flat[k] 
     end 
     else 
     t[key] = param[key] 
     end 
    end 
    return t 
end 

製作至少t全球應該解決這個問題。但是flat也沒有理由成爲全球性的,所以我們也使它成爲本地的。

local function tri(param) 
    local t = {} 
    for key in pairs(param) do 
     if param[key] == nil then end 
     if type(param[key]) == "table" then 
     local flat = tri(param[key]) 
     for k in pairs(flat) do 
      t[key .. k] = flat[k] 
     end 
     else 
     t[key] = param[key] 
     end 
    end 
    return t 
end 
+0

謝謝。有效。 –

1

你的任務可以用json.traverse()功能從this Lua的JSON模塊來完成更容易一點。
遍歷可讓您隨時使用JSON元素執行任意操作。

此代碼連接元素的路徑(對於除JSON容器之外的每個JSON元素:數組/對象)並將其用作Lua表的鍵。

local json = require'json' 

local t = {} 

local function callback(path, json_type, value) 
    if value ~= nil then -- value == nil for containers (arrays/objects) 
     t[table.concat(path)] = value 
    end 
end 

local test = '{"http://localhost:8080/":{"phone":{"-detail/phone detail.template.html":"5167n,a,7,2","s/motorola-xoom-with-wifi.json":"516a0,5,4,3"},"favicon.ico":"016ad,3,3,2","img/phones/motorola-xoom-with-wi-fi.":{"1.jpg":"*02s,2s,4v,h3|116da,o,l,6","2.jpg":"*02s,2s,4v,kp|116da,j,i,8","3.jpg":"*02s,2s,4v,ob|116da,o,m,8,7,,7,7,7","4.jpg":"*02s,2s,4v,rx|116da,o,m,9,8,,7,7,7","5.jpg":"*02s,2s,4v,vj|116da,p,m,a,8,,7,7,7"}}}' 

json.traverse(test, callback) 

-- Now t == { 
-- ["http://localhost:8080/favicon.ico"] = "016ad,3,3,2", 
-- ["http://localhost:8080/img/phones/motorola-xoom-with-wi-fi.1.jpg"] = "*02s,2s,4v,h3|116da,o,l,6", 
-- ["http://localhost:8080/img/phones/motorola-xoom-with-wi-fi.2.jpg"] = "*02s,2s,4v,kp|116da,j,i,8", 
-- ["http://localhost:8080/img/phones/motorola-xoom-with-wi-fi.3.jpg"] = "*02s,2s,4v,ob|116da,o,m,8,7,,7,7,7", 
-- ["http://localhost:8080/img/phones/motorola-xoom-with-wi-fi.4.jpg"] = "*02s,2s,4v,rx|116da,o,m,9,8,,7,7,7", 
-- ["http://localhost:8080/img/phones/motorola-xoom-with-wi-fi.5.jpg"] = "*02s,2s,4v,vj|116da,p,m,a,8,,7,7,7", 
-- ["http://localhost:8080/phone-detail/phone detail.template.html"] = "5167n,a,7,2", 
-- ["http://localhost:8080/phones/motorola-xoom-with-wifi.json"] = "516a0,5,4,3" 
-- }