使用字典中的键获取值
get out the value by using a key in the dictionary
使用我的代码,我在获取值时遇到了问题,该值是使用关键字本身的单词数
那是我的代码,
public static void essintial(string UserCorpus, string word)
{
// string str = "Alameer Ashraf Hassan Alameer ashraf,elnagar.";
string[] CorpusResult = UserCorpus.Split(' ', ',', '.');
//Creating the Dictionary to hold up each word as key & its occurance as Value ......!
Dictionary<string, int> Dict = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
//looping over the corpus and fill the dictionary in .........!
foreach (string item in CorpusResult)
{
if (item != "")
{
if (Dict.ContainsKey(item) == false)
{
Dict.Add(item, 1);
}
else
{
Dict[item]++;
}
}
}
//Console.WriteLine(Dict);
foreach (var item in Dict)
{
Console.WriteLine(item);
}
int ss = Dict[word];
Console.WriteLine(ss);
}
钥匙出问题了
我不知道你到底遇到了什么问题,但我有一个想法。
一个问题可能是您提供的单词不在字典中。这可能会导致 KeyNotFoundException
。一个简单的修复是这样的:
if(Dict.ContainsKey(word)){
Console.WriteLine(Dict[word]);
} else {
Console.WriteLine(0); //Or whatever you deem appropriate
}
您可能遇到的另一个问题是 foreach(var item in Dict)
。字典迭代元素对。变量 item
的推断类型为 KeyValuePair<string,int>
并且 Console.WriteLine(item);
可能不会打印您期望的内容。尝试用 Console.WriteLine(item.Key + " " +item.Value);
替换 Console.WriteLine(item)
试试这个:
string str = "Alameer Ashraf Hassan Alameer ashraf,elnagar.";
string[] CorpusResult = str.Split(' ', ',', '.');
//Creating the Dictionary to hold up each word as key & its occurance as Value ......!
Dictionary<string, int> Dict = new Dictionary<string, int>();
//loopnig over the corpus and fill the dictionary in .........!
foreach (string item in CorpusResult)
{
if (string.IsNullOrEmpty(item)) continue;
if (Dict.ContainsKey(item))
{
Dict[item]++;
}
else
{
Dict.Add(item, 1);
}
}
Console.WriteLine("Method 1: ");
foreach (var item in Dict)
{
Console.WriteLine(item.Value);
}
Console.WriteLine("Method 2: ");
foreach(var k in Dict.Keys)
{
Console.WriteLine(Dict[k]);
}
.NET Fiddle: https://dotnetfiddle.net/JZ9Eid
使用我的代码,我在获取值时遇到了问题,该值是使用关键字本身的单词数 那是我的代码,
public static void essintial(string UserCorpus, string word)
{
// string str = "Alameer Ashraf Hassan Alameer ashraf,elnagar.";
string[] CorpusResult = UserCorpus.Split(' ', ',', '.');
//Creating the Dictionary to hold up each word as key & its occurance as Value ......!
Dictionary<string, int> Dict = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
//looping over the corpus and fill the dictionary in .........!
foreach (string item in CorpusResult)
{
if (item != "")
{
if (Dict.ContainsKey(item) == false)
{
Dict.Add(item, 1);
}
else
{
Dict[item]++;
}
}
}
//Console.WriteLine(Dict);
foreach (var item in Dict)
{
Console.WriteLine(item);
}
int ss = Dict[word];
Console.WriteLine(ss);
}
钥匙出问题了
我不知道你到底遇到了什么问题,但我有一个想法。
一个问题可能是您提供的单词不在字典中。这可能会导致 KeyNotFoundException
。一个简单的修复是这样的:
if(Dict.ContainsKey(word)){
Console.WriteLine(Dict[word]);
} else {
Console.WriteLine(0); //Or whatever you deem appropriate
}
您可能遇到的另一个问题是 foreach(var item in Dict)
。字典迭代元素对。变量 item
的推断类型为 KeyValuePair<string,int>
并且 Console.WriteLine(item);
可能不会打印您期望的内容。尝试用 Console.WriteLine(item.Key + " " +item.Value);
Console.WriteLine(item)
试试这个:
string str = "Alameer Ashraf Hassan Alameer ashraf,elnagar.";
string[] CorpusResult = str.Split(' ', ',', '.');
//Creating the Dictionary to hold up each word as key & its occurance as Value ......!
Dictionary<string, int> Dict = new Dictionary<string, int>();
//loopnig over the corpus and fill the dictionary in .........!
foreach (string item in CorpusResult)
{
if (string.IsNullOrEmpty(item)) continue;
if (Dict.ContainsKey(item))
{
Dict[item]++;
}
else
{
Dict.Add(item, 1);
}
}
Console.WriteLine("Method 1: ");
foreach (var item in Dict)
{
Console.WriteLine(item.Value);
}
Console.WriteLine("Method 2: ");
foreach(var k in Dict.Keys)
{
Console.WriteLine(Dict[k]);
}
.NET Fiddle: https://dotnetfiddle.net/JZ9Eid