开发者

c# compare two arrays with conditions

开发者 https://www.devze.com 2023-03-23 07:36 出处:网络
If i have two开发者_如何学Python arrays First: A, B, C Second: ?, B, C I want to compare only items from Second with items from First which is does not contains question mark \"?\".

If i have two开发者_如何学Python arrays

   First: A, B, C
   Second: ?, B, C

I want to compare only items from Second with items from First which is does not contains question mark "?".

So for such case:

   First: A, B, C
   Second: ?, B, C

I wan to compare just items 1 and 2 because item 0 in the Second array contains ?.

For that one:

   First: A, B, C
   Second: 2, ?, C

I want to compare only items 0 and 2 because item 1 in Second array contains ?

Any ideas how better and with minimum amount of code to do that? If it would required to create any lists or any another collections that is fine.


KISS

for(int i = 0; i < listB.length; i++)
   if(listB[i] != "?")
       compare(listA[i], listB[i]);


Using LINQ from the comfort of your armchair:

// Input data:
var first = new[] { "A", "B", "?" };
var second = new[] { "?", "B", "C" };

// This actually filters out pairs where either first or second
// is equal to "?". If you only want to filter out pairs where the
// second is "?", adjust accordingly.
var toCompare = first.Zip(second, Tuple.Create)
                     .Where(t => t.Item1 != "?" && t.Item2 != "?");

// And now you have a collection of Tuple<string, string>, which
// can be processed in a multitude of ways:
Console.Out.WriteLine(string.Format(
    "Compared {0} items, and found {1} of them equal.",
    toCompare.Count(),
    toCompare.All(t => t.Item1 == t.Item2) ? "all" : "not all"));            

Update:

If the comparison predicate (the method that compares a pair of items and returns a boolean) is going to be more complicated than the above, it makes sense to define it either as a lambda (if it's not going to be reused and it's short enough) or as a proper method otherwise.

For example, to implement what's mentioned in a comment below:

Func<Tuple<string, string>, bool> predicate = t =>
    t.Item2 == "?" ||
    t.Item2 == "_" && t.Item1 != string.Empty ||
    t.Item2 == t.Item1;

// And now you have a collection of Tuple<string, string>, which
// can be processed in a multitude of ways:
Console.Out.WriteLine(string.Format(
    "Compared {0} items, and found {1} of them equal.",
    toCompare.Count(),
    toCompare.All(predicate) ? "all" : "not all"));            


Do you need something other than the following?

for (int ii = 0; ii < second.Length; ii++)
{
    if (second[ii] == "?")
        continue;

    // Else do your compare.
}


[TestMethod]
public void TestArrayWithConditions()
{
    var First = new [] {"A", "B", "C"};
    var Second = new[] { "?", "B", "C" };
    var Third = new[] { "2", "?", "C" };

    var result12 = CompareWithConditions(First, Second);
    var result13 = CompareWithConditions(First, Third);

    Assert.AreEqual(null, result12[0]);
    Assert.AreEqual(true, result12[1]);
    Assert.AreEqual(true, result12[2]);

    Assert.AreEqual(false, result13[0]);
    Assert.AreEqual(null, result13[1]);
    Assert.AreEqual(true, result13[2]);
}

private static List<bool?> CompareWithConditions(string[] first, string[] second)
{
    var result = new List<bool?>();

    var length = Math.Min(first.Length, second.Length);

    for (int i = 0; i < length; i++)
    {
        if (second[i] == "?")
        {
            result.Add(null);
        }
        else
        {
            result.Add(second[i] == first[i]);
        }
    }

    return result;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号