在多个位置检查值并仅在源唯一的情况下返回匹配项 [英] Checking values across multiple location and returning a match only if the sources are unique
问题描述
让我们说我有一个供应商的列表:Asda,Tesco和Spar.
Lets say I have a list of Vendors: Asda, Tesco, Spar.
我有一个来源(或类似的供应商)列表:Kellogg,Cadbury,Nestle,Johnsons,Pampers,Simple等(定义好的列表大约有20个).
And I have a list of Sources (or suppliers in this analogy): Kellogg, Cadbury, Nestle, Johnsons, Pampers, Simple, etc. (there is a defined list of around 20).
数据流中的其他位置.我要针对多个不同的事物返回一个结果,对于每个供应商来说,是/否.
Elsewhere in the flow of data. I am returning a result, which is Yes/No for each vendor, for multiple different things.
例如:Asda:ukOnly =是"; Spar:ukOnly =否"等等
For example: Asda: ukOnly = "Yes"; Spar: ukOnly = "No" etc.
在此特定部分中,我正在整理结果.
In this specific section, I am collating results.
大多数情况下,供应商的来源是否重叠都没有关系.所以我只能说:
Mostly it doesn't matter if the sources from the vendors overlap. So I can just say:
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")
太好了!
现在,说我想做些更复杂的事情.
Now, say I want to do something more comple.
我想知道有多少独特的供应商在提供巧克力和谷物.仅当涉及至少两个来源(供应商)并且它们至少必须供应巧克力时,以下示例才被用于进一步生成事实suppliesSweet
的过程.这将分别为每个供应商完成(请假设我已经根据输入数据定义了变量:
I want to know how many unique suppliers are providing chocolate and cereal. The example below is being used further up the process to produce a fact suppliesSweet
, only if there is at least two sources (suppliers) involved and they must be at least supplying chocolate. This will be done for each vendor separately (please assume I have already defined my variables based on the input data:
if (suppliesChoc > 0 and suppliesCereal > 0 and numSources > 1) or (suppliesChoc > 1) then
then suppliesSweet = "Yes"
else suppliesSweet = "No"
end
还没有问题.
当我尝试在各个供应商之间汇总这些结果时,问题就来了(就像我以前对ukOnly
所做的那样).
The issue comes when I try to aggregate these results across vendors (as I did before with ukOnly
).
我已经使用了以下功能:
I already have the following function being used:
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
但是我不确定从那里去哪里.
But I'm just not sure where to go from there.
您可以假设我已经获得了每个供应商的唯一来源列表,并且我不介意我们是否过于严格. IE.如果两个供应商之间有任何来源重叠,那将使整个结果无效.
You can assume I have already got a list of unique sources for each vendor and I don't mind if we're over restrictive. I.e. if any sources overlap between the two vendors, that would invalidate the entire result.
您还可以假设我有每个事实":分别返回的每个供应商的suppliesChoc
,suppliesCereal
,numSources
和suppliesSweet
.
You can also assume I have each "fact": suppliesChoc
, suppliesCereal
, numSources
and suppliesSweet
for each vendor returned separately.
推荐答案
我相信您正在寻找两个集合的交集.
I believe your looking for the intersection of two sets.
一组是您的供应商的供应商,另一组是提供糖果的供应商.
One set being your vendor's suppliers and the other being the suppliers who supply sweets.
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
以下是用于处理集合的库的一些示例:
Here are some examples of a libraries for handling sets:
两者都可以使您了解如何使用集合来完成交集之类的事情
Both can give you an idea of how you can use sets to accomplish things like intersection, and much more
这篇关于在多个位置检查值并仅在源唯一的情况下返回匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!