2015-04-06 83 views
-3

所以我遇到的问题是我想要计算列表中枚举的倍数。比如说,我有一个“Hi”“Hi”“Hello”“Hi”和“Hello”的列表。C#:计数列表中的倍数

有没有一种方法,我可以搜索数组,并找出有3个“嗨”,而不必做List.Where x =“Hi”>?

谢谢了。

+0

那么“你好”呢?你也要测试一下吗?请发布您的C#代码。 –

+0

我想知道“Hello”是否也有倍数。从我在网上找到的,以及我对c#的基本知识,我只看到了.Where <>功能,但是我必须预先定义列表中要查找的枚举或字符串。我想知道是否有办法让它只计算枚举或字符串本身并返回它们的倍数。 –

+0

所以你想要一个只包含多个枚举的结果列表? – Coder1409

回答

0

要计算列表中的某个元素的出现次数,你可以简单地做:

List.Count(x => x == "Hi"); 

编辑:如果你只是想讲的速记方式,其元素出现N次,你可以做到与嵌套查询:

List<string> greetings = new List<string> {"Hi", "Hi", "Hello", "Hello", "Hi"}; 

List<string> greetingsThatOccurThreeTimes = greetings 
    .Where(s1 => greetings.Count(s2 => s1 == s2) == 3).ToList(); 

编辑#2:您还可以通过使用扩展方法清理它。

通过声明如下:

public static class ListExtensions 
{ 
    public static List<T> WithNOccurrences<T>(this List<T> source, int n) 
    { 
     return source.Where(s1 => source.Count(s2 => s1.Equals(s2)) == n).ToList(); 
    } 
} 

你可以做你的调用代码是这样的:

List<string> greetings = new List<string> {"Hi", "Hi", "Hello", "Hello", "Hi"}; 

// This list will only contain "Hi" (but 3 times, though) 
List<string> greetingsThatOccurThreeTimes = greetings.WithNOccurrences(3); 

这应该更接近你的 “ThreeOfAKind ==真”。

如果你只是想找回出现N次的每个项目之一,只需添加.Distinct()的扩展方法,像这样:

public static class ListExtensions 
{ 
    public static List<T> WithNOccurrences<T>(this List<T> source, int n) 
    { 
     return source 
      .Where(s1 => source.Count(s2 => s1.Equals(s2)) == n) 
      .Distinct().ToList(); 
    } 
} 
+0

那么,我在想什么就像列表中有3个hi和2个hello。我们要计算这些项目的倍数。这些项目可能在list.GroupBy <>函数中工作,但是我想知道是否有一种方法,当我们迭代列表并在列表中找到三个“hi”时,我们可以拥有“threeofAkind = true”。 –

+0

我已经添加了一个扩展方法,就是这么做的。那是你在找什么? –

+0

这正是我所期待的。谢谢。我对编码相对来说比较陌生,我们只是说大学并没有向我们讲述这样的事情。 –

2

你可以尝试LINQ的计算频率,例如:

List<String> source = new List<string>() { 
    "Hi", "Hi", "Hello", "Hi", "Hello" 
}; 

Dictionary<String, int> freqs = source 
    .GroupBy(item => item) 
    .ToDictionary(item => item.Key, 
       item => item.Count()); 

.... 
int freqHi = freqs["Hi"]; // 3 
0

这LINQ查询会给你有哪些项目列表复制

var mutiples = l.GroupBy(e => e).Where(c=>c.Count()>1); 

您可以通过mutiples列表使用此代码

foreach (var pair in mutiples) 
      Console.WriteLine(pair.Key + " " + pair.Count()); 
0

在这里你去遍历:

 public List<string> GetDuplicates(List<string> list) 
     { 
      var q = from s in list 
        group s by s into g 
        select new { str = g.Key, count = g.Count() }; 

      return q.Where(str => str.count > 1).Select(str => str.str).ToList<string>(); 
     } 
0
string words = "Hi, Hi, Hello, Hi, Hello"; 
var countList = words.Split(new[] { " " }, StringSplitOptions.None); 
int count = countList.Where(s => s.Contains("Hi")).Count(); 

数= 3运行该代码时。

0

您必须先对它们进行分组,然后执行选择操作。如果你问算来,一个例子可以是:

List<string> stringList = new List<string>{ "hi", "hi", "hi", "hello" }; 
stringList.GroupBy(i => i).Select(i => new {i.Key, i.ToList().Count }).ToList(); 
0

看到这个SO发布https://stackoverflow.com/a/10812657/1339616

您还可以使用LINQ查询语法是这样的。

from s in new List<string> {"Hi", "Hi", "Hello", "Hi", "Hello"} 
group s by s into g 
where g.Count() > 1 
orderby g.Count() descending 
select new {Key = g.Key, Count = g.Count() }