2011-12-06 69 views
4

我有一个Dictionary<string, int>,我正在阅读列表中的一些字符串...我想将它们添加到字典中,但是如果字符串已经在字典中,我希望它的值增加1字典<string, int>增加值

我试过的代码如下,但是有一些字符串会随着每个输入而增加..是不是有问题?

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); 
      } 
     } 
    } 

编辑:要回答你的意见......我删除字符串的最后一个字符,因为它始终是一个空白...... 我的输入是这样的:

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 

和我只需要像“业务”或“管理&辅助服务”等字符串。

+2

对于它的价值,你可以清理你的代码*很多*:http://codepad.org/3DD4H1zl – Ryan

+6

样品输入显示你所看到的行为的字符串? –

+1

请给出你想要操纵的字符串的例子,以澄清你的问题 – lafama

回答

2

不需要字典,可以使用此Linq查询来解决。
(假设你想要的完整的字符串\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() 
    }; 

如果你需要它作为一个字典只需添加:

var dic = q.ToDictionary (x => x.Key, x => x.Count); 
1

你输入的字符串第一分裂然后的子它返回到tagToDic,所以也许n个字符串有相同 tagToDic。

0

从现有的检索计数后重新添加字典值可能更容易。

下面是一些用于处理查找逻辑的伪代码。

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

如何:

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

如果你有他们在列表中你可以只将它们分组,使您的列表。

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

然后你可以把它放在字典或迭代它或其他东西。

0

您的dictionary代码看起来像它会按照您期望的方式运行。

我最好的猜测是你的字符串分裂代码不能正常工作。
你必须给我们一些样本输入来验证这一点,但。

无论如何,代码整个块可以简化并与LINQ改写为:

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

我不明白需要'recordstag.Substring(0,recordstag.Length-1);'。为什么要删除最后的字符? – Kash

+2

@卡什我不知道,你为什么问我?问@SpDaglas! –

+0

我做到了。我想知道你是否有这方面的见解。 – Kash

6

要拆分输入字符串数组中的每个字符串,并选择字符串数组中的第二根弦。 然后,您使用SubString删除此第二个字符串的最后一个字符。因此,只有最后一个字符不同的字符串才会被视为相同并递增。这就是为什么你可能会看到“一些随着每个输入而增加的字符串”。

编辑:如果删除最后一个字符的目的是删除空间,请改用String.Trim。 另一个编辑使用TryGetValue而不是ContainsKey,它可以更好地增加您的价值。代码已在下面进行了编辑。

试试这个:

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; 
    } 
0

扩展方法

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");