【发布时间】:2015-09-29 01:10:09
【问题描述】:
我想输入List<string[]> 和
输出是一个字典,其中键是用于索引的唯一字符串,值是浮点数组,数组中的每个位置表示List<string[]> 中string[] 的键计数
到目前为止,这是我尝试过的
static class CT
{
//Counts all terms in array
public static Dictionary<string, float[]> Termfreq(List<string[]> text)
{
List<string> unique = new List<string>();
foreach (string[] s in text)
{
List<string> groups = s.Distinct().ToList();
unique.AddRange(groups);
}
string[] index = unique.Distinct().ToArray();
Dictionary<string, float[]> countset = new Dictionary<string, float[]>();
return countset;
}
}
static void Main()
{
/* local variable definition */
List<string[]> doc = new List<string[]>();
string[] a = { "That", "is", "a", "cat" };
string[] b = { "That", "bat", "flew","over","the", "cat" };
doc.Add(a);
doc.Add(b);
// Console.WriteLine(doc);
Dictionary<string, float[]> ret = CT.Termfreq(doc);
foreach (KeyValuePair<string, float[]> kvp in ret)
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
Console.ReadLine();
}
我在字典部分卡住了。实现这一点的最有效方法是什么?
【问题讨论】:
-
为什么要在这里使用
float?什么是非整数值? -
您能否提供预期输出的样本?
-
最好有一个更有用的输入,其中单词出现不止一次...
-
@JonSkeet 如果我想做任何数学运算 float 允许小数。
-
但是计数永远不会是整数以外的任何东西。如果你想将它转换为一个数组,而不是一个计数,为了清楚起见,我会亲自创建一个新数组。
标签: c# arrays dictionary