linq 中计算列表百分比的最佳方法是什么?

发布于 2024-10-19 18:28:02 字数 192 浏览 3 评论 0原文

我有一个 1 和 0 的列表,现在我必须计算百分比含义,如果 1 他实现了,否则他没有实现。例如,

{1,1,0,0,0}

如果列表有 5 个项目,而他有 2 个,那么他的百分比是 40%。 LINQ 中是否有一种函数或方法可以让我在一行中轻松完成?我确信 LINQ 专家有一种温和的方法来做到这一点?

I have a list of 1s and 0s and I have to now calculate the percent meaning if 1 he achieved it else he doesn't. So e.g -

{1,1,0,0,0}

So for e.g If List has 5 items and he got 2 ones then his percent is 40%. Is there a function or way in LINQ I could do it easily maybe in one line ? I am sure LINQ experts have a suave way of doing it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如何视而不见 2024-10-26 18:28:02

所示

var list = new List<int>{1,1,0,0,0};
var percentage = ((double)list.Sum())/list.Count*100;

或者如果您想获取特定元素的百分比

var percentage = ((double)list.Count(i=>i==1))/list.Count*100;

编辑

请注意 BrokenGlass 的解决方案,并针对第一种情况使用 Average 扩展方法,如下

var percentage = list.Average() * 100;

What about

var list = new List<int>{1,1,0,0,0};
var percentage = ((double)list.Sum())/list.Count*100;

or if you want to get the percentage of a specific element

var percentage = ((double)list.Count(i=>i==1))/list.Count*100;

EDIT

Note BrokenGlass's solution and use the Average extension method for the first case as in

var percentage = list.Average() * 100;
谁的年少不轻狂 2024-10-26 18:28:02

在这种特殊情况下,您还可以使用 Average()

var list = new List<int> {1,1,0,0,0};
double percent = list.Average() * 100;

In this special case you can also use Average() :

var list = new List<int> {1,1,0,0,0};
double percent = list.Average() * 100;
故人如初 2024-10-26 18:28:02

如果您使用任何 ICollection(例如 List),Count 属性可能 为 O(1);但在 any 序列的更一般情况下,Count() 扩展方法将是 O(N),使其不太理想。因此,对于最一般的情况,您可能会考虑这样的方法,它一次性计算与指定谓词匹配的元素所有元素:

public static double Percent<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
    int total = 0;
    int count = 0;

    foreach (T item in source)
    {
        ++count;
        if (predicate(item))
        {
            total += 1;
        }
    }

    return (100.0 * total) / count;
}

然后您只需执行以下操作:

var list = new List<int> { 1, 1, 0, 0, 0 };
double percent = list.Percent(i => i == 1);

输出:

40

If you're working with any ICollection<T> (such as List<T>) the Count property will probably be O(1); but in the more general case of any sequence the Count() extension method is going to be O(N), making it less than ideal. Thus for the most general case you might consider something like this which counts elements matching a specified predicate and all elements in one go:

public static double Percent<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
    int total = 0;
    int count = 0;

    foreach (T item in source)
    {
        ++count;
        if (predicate(item))
        {
            total += 1;
        }
    }

    return (100.0 * total) / count;
}

Then you'd just do:

var list = new List<int> { 1, 1, 0, 0, 0 };
double percent = list.Percent(i => i == 1);

Output:

40
断爱 2024-10-26 18:28:02

最好的方法是:

var percentage = ((double)list.Count(i=>i==1))/list.Count*100;

或者

var percentage = ((double)list.Count(i=>i <= yourValueHere))/list.Count*100;

Best way to do it:

var percentage = ((double)list.Count(i=>i==1))/list.Count*100;

or

var percentage = ((double)list.Count(i=>i <= yourValueHere))/list.Count*100;
美人迟暮 2024-10-26 18:28:02

如果您

  • 想在一行中完成,并且
  • 不想维护扩展方法,
  • 则无法利用 list.Sum() 因为您的列表数据不是1 和 0

你可以这样做:

percentAchieved = (int)
                  ((double)(from MyClass myClass
                  in myList
                  where MyClass.SomeProperty == "SomeValue"
                  select myClass).ToList().Count / 
                  (double)myList.Count * 
                  100.0
                  );

If You

  • want to do it in one line
  • don't want to maintain an extension method
  • can't take advantage of list.Sum() because your list data isn't 1s and 0s

you can do something like this:

percentAchieved = (int)
                  ((double)(from MyClass myClass
                  in myList
                  where MyClass.SomeProperty == "SomeValue"
                  select myClass).ToList().Count / 
                  (double)myList.Count * 
                  100.0
                  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文