简体   繁体   English

使用LINQ在数组字典中计算非零值

[英]Counting non zero values in a dictionary of arrays with LINQ

I have a dictionary of arrays 我有一个数组字典

public static void Main(string[] args)
{
    Dictionary<string, int[]> ret = new Dictionary<string, int[]>();
    int[] a = {1,0,3,4,0};
    int[] b = { 3, 0, 9, 10, 0};
    int[] c = {2,3,3,5,0};
    ret.Add("Jack", a);
    ret.Add("Jane", b);
    ret.Add("James", c);

}

If I want to do an operation on the count of the columns such as v*column count I would do this: 如果我想对诸如v*column count这样的v*column count我会这样做:

        Dictionary<string, double[]> colcnt = ret.ToDictionary(r => r.Key,
                         r => r.Value.Select(v => v == 0 ? 0 :
                                  (double)v / (ret.Values.Count()) //equation
                                                   ).ToArray());

What is the LINQ code to perform operations such as count on rows with non zeros? LINQ代码执行什么操作,例如对非零行进行计数?

If I use a loop to count them it would be 如果我使用循环来计数它们

        foreach (var item in ret)
        {
          int vals= item.Value.Count(s => s != 0);

        }

So if I were to do v/column count then all items in a would be divided by 3, all items in b would be divided by 3 and all items in c would be divided by 4 所以,如果我是做v/column count ,然后在所有项目a将由3分,在所有项目b将由3分,并在所有项目c会除以4,

Is this what you want? 这是你想要的吗?

var result = ret.ToDictionary
(
    r => r.Key, 
    v => v.Value.Select(n => (double)n/v.Value.Count(i => i != 0)).ToArray()
);

This will set the values to NaN for a row if all the elements of that row are zero. 如果该行的所有元素均为零,则将该行的值设置为NaN If instead you want to make the results for that row zero, you could change the code to: 相反,如果您想使该行的结果为零,则可以将代码更改为:

var result = ret.ToDictionary
(
    r => r.Key, 
    v => v.Value.Select(n =>
    {
        double count = v.Value.Count(i => i != 0);
        return (count > 0) ? n/count : 0.0;
    }).ToArray()
);

If you just need a sum of all non zero values in all the dictionary items you can use 如果只需要所有字典项中所有非零值的总和,则可以使用

ret.Sum(x => x.Value.Count(y => y != 0));

If you need to iterate through all the key value pairs and you wish not to use a foreach loop then you have to come up with an extension method of your own as shown below 如果您需要遍历所有键值对并且不希望使用foreach循环,则必须提出自己的扩展方法,如下所示

 public static class DictionaryExtensionMethods
{
    public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> method)
    {
        foreach (T obj in enumerable)
        {
            method(obj);
        }
    }
}

And you can use it like this 你可以这样使用它

 class Program
{
    static void Main()
    {
       var ret = new Dictionary<string, int[]>();
        int[] a = { 1, 0, 3, 4, 0 };
        int[] b = { 3, 0, 9, 10, 0 };
        int[] c = { 2, 3, 3, 5, 0 };
        ret.Add("Jack", a);
        ret.Add("Jane", b);
        ret.Add("James", c);
        ret.ForEach(x => Write(x.Value.Count(y => y !=0), x.Key));
        Console.ReadLine();
    }

    public static void Write(int count, string key)
    {
        Console.WriteLine("Count of non zeroes in {0} is {1}", key, count);
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM