Is there a simple way to count the number of occurrences of all elements of a list into that same list in C#?
Something like this:
using System; using System.IO; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Linq; string Occur; List<string> Words = new List<string>(); List<string> Occurrences = new List<string>(); // ~170 elements added. . . for (int i = 0;i<Words.Count;i++){ Words = Words.Distinct().ToList(); for (int ii = 0;ii<Words.Count;ii++){Occur = new Regex(Words[ii]).Matches(Words[]).Count;} Occurrences.Add (Occur); Console.Write("{0} ({1}), ", Words[i], Occurrences[i]); } }
Using the count() Function The "standard" way (no external libraries) to get the count of word occurrences in a list is by using the list object's count() function. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.
The easiest way to count the number of occurrences in a Python list of a given item is to use the Python . count() method. The method is applied to a given list and takes a single argument. The argument passed into the method is counted and the number of occurrences of that item in the list is returned.
Python List count() In this tutorial, we will learn about the Python List count() method with the help of examples. The count() method returns the number of times the specified element appears in the list.
Use bincount() to count occurrences of a value in a NumPy array. In python, the numpy module provides a function numpy. bincount(arr), which returns a count of number of occurrences of each value in array of non-negative ints. It returned the count of all occurences of 3 in the array.
How about something like this ...
var l1 = new List<int>() { 1,2,3,4,5,2,2,2,4,4,4,1 }; var g = l1.GroupBy( i => i ); foreach( var grp in g ) { Console.WriteLine( "{0} {1}", grp.Key, grp.Count() ); }
Edit per comment: I will try and do this justice. :)
In my example, it's a Func<int, TKey>
because my list is ints. So, I'm telling GroupBy how to group my items. The Func takes a int and returns the the key for my grouping. In this case, I will get an IGrouping<int,int>
(a grouping of ints keyed by an int). If I changed it to (i => i.ToString()
) for example, I would be keying my grouping by a string. You can imagine a less trivial example than keying by "1", "2", "3" ... maybe I make a function that returns "one", "two", "three" to be my keys ...
private string SampleMethod( int i ) { // magically return "One" if i == 1, "Two" if i == 2, etc. }
So, that's a Func that would take an int and return a string, just like ...
i => // magically return "One" if i == 1, "Two" if i == 2, etc.
But, since the original question called for knowing the original list value and it's count, I just used an integer to key my integer grouping to make my example simpler.
You can do something like this to count from a list of things.
IList<String> names = new List<string>() { "ToString", "Format" }; IEnumerable<String> methodNames = typeof(String).GetMethods().Select(x => x.Name); int count = methodNames.Where(x => names.Contains(x)).Count();
To count a single element
string occur = "Test1"; IList<String> words = new List<string>() {"Test1","Test2","Test3","Test1"}; int count = words.Where(x => x.Equals(occur)).Count();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With