检查多个位置的值并仅在源唯一时返回匹配项
Checking values across multiple location and returning a match only if the sources are unique
假设我有一份 供应商 的列表:Asda、Tesco、Spar。
我有一份 来源(或此类比中的供应商)的列表:Kellogg、Cadbury、Nestle、Johnsons、Pampers、Simple 等(有一个已定义的列表约 20)。
在数据流的其他地方。对于多个不同的事物,我返回一个结果,每个供应商的结果是 Yes/No。
例如:阿斯达:ukOnly = "Yes"; Spar:ukOnly = "No" 等
在这个特定的部分,我正在整理结果。
大多数情况下,供应商的来源是否重叠并不重要。所以我只能说:
function concatResults(x) -- concats the result of "x" for each vendor
local pathAsda = *this is where I call the path location specific to Asda*
local pathTesco = *this is where I call the path location specific to Tesco*
local pathSpar = *this is where I call the path location specific to Spar*
if (pathAsda == "Yes" or pathTesco == "Yes" or pathSpar == "Yes") then
return "Yes"
else
return "No"
end
end
ukOnlyAgr = concatResults("ukOnly")
太棒了!
现在,假设我想做一些更复杂的事情。
我想知道有多少独特的供应商提供巧克力和麦片。下面的示例将进一步用于生成事实 suppliesSweet
的过程,前提是至少涉及两个来源(供应商)并且他们必须至少供应巧克力。这将分别为每个供应商完成(请假设我已经根据输入数据定义了我的变量:
if (suppliesChoc > 0 and suppliesCereal > 0 and numSources > 1) or (suppliesChoc > 1) then
then suppliesSweet = "Yes"
else suppliesSweet = "No"
end
还不是问题。
当我尝试跨供应商汇总这些结果时出现问题(就像我之前对 ukOnly
所做的那样)。
我已经使用了以下功能:
table.contains = function(t, value) -- Finds if "value" exists inside the table "t"
for index = 1, #t do
if t[index] == value then
return index
end
end
end
并且正在考虑创建这个:
table.overlap = function(t,g) -- Finds if tables "g" and "t" have any overlapping values
for i=1,#t do
if table.contains(g,t[i]) then
return true
else
return false
end
end
end
但我只是不确定从那里去哪里。
您可以假设我已经获得了每个供应商的唯一来源列表,并且我不介意我们是否过度限制。 IE。如果两个供应商之间有任何来源重叠,那将使整个结果无效。
您还可以假设我有每个 "fact":suppliesChoc
、suppliesCereal
、numSources
和 suppliesSweet
分别返回每个供应商。
我相信你在寻找两组的交集。
一组是您供应商的供应商,另一组是供应糖果的供应商。
local vendors = {
Asda = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true, Pampers = true, Simple = true},
Tesco = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true},
Spar ={Nestle = true, Johnsons = true, Pampers = true, Simple = true}
}
function intersection(s1, s2)
local output = {}
for key in pairs(s1) do
output[#output + 1] = s2[key]
end
return output
end
local sweetSuppliers = {Kellogg = true, Cadbury = true, Nestle = true}
for name, suppliers in pairs(vendors) do
local result = intersection(sweetSuppliers, suppliers)
print(name .. " has " .. #result .. " sweets suppliers")
end
以下是一些用于处理集合的库示例:
两者都可以让您了解如何使用集合来完成交集等事情
假设我有一份 供应商 的列表:Asda、Tesco、Spar。
我有一份 来源(或此类比中的供应商)的列表:Kellogg、Cadbury、Nestle、Johnsons、Pampers、Simple 等(有一个已定义的列表约 20)。
在数据流的其他地方。对于多个不同的事物,我返回一个结果,每个供应商的结果是 Yes/No。
例如:阿斯达:ukOnly = "Yes"; Spar:ukOnly = "No" 等
在这个特定的部分,我正在整理结果。
大多数情况下,供应商的来源是否重叠并不重要。所以我只能说:
function concatResults(x) -- concats the result of "x" for each vendor
local pathAsda = *this is where I call the path location specific to Asda*
local pathTesco = *this is where I call the path location specific to Tesco*
local pathSpar = *this is where I call the path location specific to Spar*
if (pathAsda == "Yes" or pathTesco == "Yes" or pathSpar == "Yes") then
return "Yes"
else
return "No"
end
end
ukOnlyAgr = concatResults("ukOnly")
太棒了!
现在,假设我想做一些更复杂的事情。
我想知道有多少独特的供应商提供巧克力和麦片。下面的示例将进一步用于生成事实 suppliesSweet
的过程,前提是至少涉及两个来源(供应商)并且他们必须至少供应巧克力。这将分别为每个供应商完成(请假设我已经根据输入数据定义了我的变量:
if (suppliesChoc > 0 and suppliesCereal > 0 and numSources > 1) or (suppliesChoc > 1) then
then suppliesSweet = "Yes"
else suppliesSweet = "No"
end
还不是问题。
当我尝试跨供应商汇总这些结果时出现问题(就像我之前对 ukOnly
所做的那样)。
我已经使用了以下功能:
table.contains = function(t, value) -- Finds if "value" exists inside the table "t"
for index = 1, #t do
if t[index] == value then
return index
end
end
end
并且正在考虑创建这个:
table.overlap = function(t,g) -- Finds if tables "g" and "t" have any overlapping values
for i=1,#t do
if table.contains(g,t[i]) then
return true
else
return false
end
end
end
但我只是不确定从那里去哪里。
您可以假设我已经获得了每个供应商的唯一来源列表,并且我不介意我们是否过度限制。 IE。如果两个供应商之间有任何来源重叠,那将使整个结果无效。
您还可以假设我有每个 "fact":suppliesChoc
、suppliesCereal
、numSources
和 suppliesSweet
分别返回每个供应商。
我相信你在寻找两组的交集。
一组是您供应商的供应商,另一组是供应糖果的供应商。
local vendors = {
Asda = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true, Pampers = true, Simple = true},
Tesco = {Kellogg = true, Cadbury = true, Nestle = true, Johnsons = true},
Spar ={Nestle = true, Johnsons = true, Pampers = true, Simple = true}
}
function intersection(s1, s2)
local output = {}
for key in pairs(s1) do
output[#output + 1] = s2[key]
end
return output
end
local sweetSuppliers = {Kellogg = true, Cadbury = true, Nestle = true}
for name, suppliers in pairs(vendors) do
local result = intersection(sweetSuppliers, suppliers)
print(name .. " has " .. #result .. " sweets suppliers")
end
以下是一些用于处理集合的库示例:
两者都可以让您了解如何使用集合来完成交集等事情