简体   繁体   English

如何有效匹配多个值

[英]How to effectively match on multiple values

I am trying to come up with a matching algorithm that matches on 3 attributes and I can't think of an effective solution. 我试图提出一种匹配算法,可匹配3个属性,但我想不出有效的解决方案。 Here is the psuedocode for my algorithm 这是我的算法的伪代码

The algorithm does the following: 该算法执行以下操作:

  • Check if there is a match on the first criteria. 检查第一个条件是否匹配。
  • If there is match on first criteria then I try to narrow the match down based on the other 2 criterias. 如果第一个条件匹配,那么我尝试根据其他两个条件来缩小匹配范围。
  • If there is no match on the first criteria, then try to make a match on the second criteria. 如果第一个条件不匹配,则尝试在第二个条件上匹配。
  • If there is a match on the second criteria, then I try to narrow the match down based on the last criteria. 如果第二个条件匹配,那么我尝试根据最后一个条件缩小匹配范围。
  • Etc... 等等...

It seems like this algorithm just repeats itself, and if I add another value to match on, then this algorithm grows very large real quickly. 似乎此算法只是重复自身,如果我添加另一个值进行匹配,则该算法实际会很快变得很大。

//try to match on criteria 1
if results exist for match on criteria 1 {
    //try to match on criteria 1 & 2
    if results exist for match on criteria 1 & 2 {
        //try to match on criteria 3
        if result exist for match on criteria 1,2,3 {
            return results for match on 1,2,3
        }
        else
            return results for match on 1,2
    }
    else
        return results for match on 1
}
//try to match on criteria 2
else if results exist for match on criteria 2 {
    //try to match on criteria 2 & 3
    if result exist for match on criteria 2,3 {
        return results for match on 2,3
    }
    else
        return results for match on 2
}
//try to match on criteria 3
else if results exist for match on criteria 3 {
    return results for match on 3
}
else {
    no match
}

Is there any better way to do this? 有什么更好的方法吗? It seems like 这好像是

Here is an implementation in Javascript if I understand you correctly. 如果我正确理解,这是Javascript中的实现。

function filter(arr, condition) {
  var retval = [];
  for (i in arr) if (condition(arr[i])) retval.push(arr[i]);
  return retval;
}

function first_match(arr, conditions) {
  if (conditions.length == 0) return("No match");
  var initial_arr = Array.prototype.slice.call(arr);
  var prev_arr;
  var i = 0;
  for (; i < conditions.length && arr.length != 0; i++) {
    prev_arr = Array.prototype.slice.call(arr);
    arr = filter(arr, conditions[i]);
  }
  if (i <= 1 && arr.length == 0) return first_match(initial_arr, Array.prototype.slice.call(conditions, 1));
  else if (i == conditions.length && arr.length != 0) return arr;
  else return prev_arr;
}

Examples 例子

var conditions = [function(x) { return x > 3; },
                  function(x) { return x % 2 == 0; },
                  function(x) { return Math.sqrt(x) % 1 == 0; }];

var example1 = [1,2,3,4,5,6,7,8,9,10];
console.log(first_match(example1, conditions)); // [4] - all three hold
var example2 = [1,2,3,5,6,7,8,9,10];
console.log(first_match(example2, conditions)); // [6, 8, 10] - First two hold
var example3 = [5, 7, 9];
console.log(first_match(example3, conditions)); // [5, 7, 9] - First one holds
var example4 = [-2, 0, 2];
console.log(first_match(example4, conditions)); // [0] - 2 & 3 hold
var example5 = [-2, 2];
console.log(first_match(example5, conditions)); // [-2, 2] - Only 2 holds
var example6 = [1];
console.log(first_match(example6, conditions)); // [1] - Last one holds
var example7 = [-1];
console.log(first_match(example7, conditions)); // "No match" - None hold

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何有效地将字符串与大量正则表达式匹配 - How to effectively match a string with lots of regular expressions 如何从多个表中选择一个(或零个)条目,并将这些值加在一起以匹配特定值? - How can I select one(or zero) entry from multiple tables, and add these values together to match a specific value? 如何在飞机上有效地分配点 - How to effectively distribute points on plane 如何有效地确定R中data.table中每行中的变量值和后续行值中的相同变量之间的最大差异 - How to effectively determine the maximum difference between the variable value in each row and same variable subsequent row values in data.table in R 如何快速匹配字符串(短语)与HashMap值? - How to match a string (phrase) to a HashMap values quickly? 如何在不使用“ /”和“%”的情况下有效地获得商和余数? - How to get the quotient and remainder effectively without using “/” and “%”? 如何有效地回答整数数组中的范围查询? - How to effectively answer range queries in an array of integers? 如何有效地比较二维数组 - How to compare 2d array effectively 如何更有效地存储数组中的词汇? - How to store vocabulary in an array more effectively? 如何有效监控远程位置的变化? - How to effectively monitor change on a remote location?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM