The dictionary <string, int> increases the value

I have a Dictionary<string, int> , and I'm reading some lines from the list ... I want to add them to the dictionary, but if the string is already in the dictionary, I want its value to increase by 1.

The code I tried is below, but there are some lines that increase with each input. Something is wrong?

  Dictionary<string, int> dictionary = new Dictionary<string, int>(); foreach (String recordline in tags) { String recordstag = recordline.Split('\t')[1]; String tagToDic = recordstag.Substring(0, (recordstag.Length-1) ); if (dictionary.ContainsKey(tagToDic) == false) { dictionary.Add(tagToDic, 1); } else { try { dictionary[tagToDic] = dictionary[tagToDic] + 1; } catch (KeyNotFoundException ex) { System.Console.WriteLine("X" + tagToDic + "X"); dictionary.Add(tagToDic, 1); } } } 

EDIT: To answer your comments ... I am deleting the last char of the line because it is always empty space ... My input is as follows:

 10000301 business 0 0,000 10000301 management & auxiliary services 0 0,000 10000316 demographie 0 0,000 10000316 histoire de france 0 0,000 10000347 economics 0 0,000 10000347 philosophy 1 0,500 

and I want only a line like "business" or "management and support services", etc.

+4
source share
8 answers

You split each row in the array of input strings and select the second row in the array of strings. Then you delete the last character of this second line using SubString . Therefore, all lines that differ only in the last character will be considered the same and increase. That is why you can see "some lines that increase with each input."

EDIT: If the goal of removing the last char is to remove the place, use String.Trim instead. Another board uses TryGetValue instead of ContainsKey, which better increases your value. The code has been edited below.

Try the following:

  Dictionary<string, int> dictionary = new Dictionary<string, int>(); foreach(string recordline in tags) { string recordstag = recordline.Split('\t')[1].Trim(); int value; if (!dictionary.TryGetValue(recordstag, out value)) dictionary.Add(recordstag, 1); else dictionary[recordstag] = value + 1; } 
+6
source

There is no need for a dictionary, it can be solved with this Linq query.
(Assuming you want to get the full line after \t )

 var q = from s in tags.Select (t => t.Substring(t.IndexOf("\t"))) group s by s into g select new { g.Key, Count = g.Count() }; 

And if you need this as a dictionary, just add:

 var dic = q.ToDictionary (x => x.Key, x => x.Count); 
+2
source

Your input string is split first and then the substring from it is back in tagToDic. Therefore, perhaps n lines have the tag tag tagToDic.

+1
source

It's probably easier to just add a dictionary value after you get a counter from an existing one.

Here is some psuedo code to handle the search logic.

 Dictionary<string, int> _dictionary = new Dictionary<string, int>(); private void AdjustWordCount(string word) { int count; bool success = _dictionary.TryGetValue(word, out count); if (success) { //Remove it _dictionary.Remove(word); //Add it back in plus 1 _dictionary.Add(word, count + 1); } else //could not get, add it with a count of 1 { _dictionary.Add(word, 1); } } 
0
source

What about:

 Dictionary<string, int> dictionary = new Dictionary<string, int>(); string delimitedTags = "some tab delimited string"; List<string> tags = delimitedTags.Split(new char[] {'\t'}, StringSplitOptions.None).ToList(); foreach (string tag in tags.Distinct()) { dictionary.Add(tag, tags.Where(t => t == tag).Count()); } 
0
source

If you have them in the list, you can simply group them and make a list.

 list.GroupBy(recordline => recordline.Split('\t').Substring(0, (recordstag.Length-1), (key, ienum) => new {word = key, count = ienum.Count()}); 

Then you can put this into a dictionary or iterate over it or something like that.

0
source

Your dictionary code looks as if it will function as you expect.

My best guess is that your split code is not working properly.
You would have to give us some test inputs in order to verify this.

In any case, the entire code block can be simplified and rewritten with LINQ as:

 var dictionary = tags .Select(t => { var recordstag = t.Split('\t')[1]; return recordstag.Substring(0, recordstag.Length-1); }) .GroupBy(t => t) .ToDictionary(k => k.Key, v => v.Count()) ; 
0
source

Extension method

 public static void Increment(this Dictionary<string, int> dictionary, string key) { int val; dictionary.TryGetValue(key, out val); if (val != null) dictionary[key] = val + 1; } Dictionary<string, int> dictionary = new Dictionary<string, int>(); // fill with some data dictionary.Increment("someKey"); 
0
source

Source: https://habr.com/ru/post/1384997/


All Articles