【问题标题】:Converting Python function to Lua function将 Python 函数转换为 Lua 函数
【发布时间】:2017-05-11 18:27:14
【问题描述】:

我正在尝试将现有的 python 函数转换为 lua 函数。但是我的 lua 函数产生的结果与 python 函数不同。任何帮助表示赞赏。

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 代码(产生的结果与 python 函数不同)

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))))

【问题讨论】:

  • 如果您告诉人们该函数应该做什么以及它做什么,这将有所帮助......变量应该尽可能地是本地的。只要让自己清楚你的表 t 会发生什么,因为每当你调用 tri() 时它是全局的。

标签: lua lua-table


【解决方案1】:
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

【讨论】:

    【解决方案2】:

    使用 this Lua JSON 模块中的 json.traverse() 函数可以更轻松地完成您的任务。
    遍历让您可以使用 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"
    -- }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-02
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多