创建从字符串列表中的所有不同单词的字典C#
问题描述:
我正在查看有关数据挖掘算法的教程,无法从教程中复制一行代码以生成我自己的“词汇表”变量(如教程叫它)。创建从字符串列表中的所有不同单词的字典C#
基本上教程页面上的代码是:
List<string> x = textBox1.Text.Split(',').ToList();
var vocabulary = x.SelectMany(GetWords).Distinct().OrderBy(word => word).ToList();
但是,当我把它复制到Visual Studio中,我得到以下错误:
The name 'GetWords' does not exist in the current context.
相信我,我并不缺什么从教程。我所寻找的是要实现以下的方法:
考虑这一点,并产生这样的:
(忽略教程图片上的数字)
我试过下面的代码,但它们从字符串中返回整个元素:
//var vocabulary = x.OrderBy(q => q).Distinct().ToList();
//var vocabulary = (from w in x
// select w).Distinct().ToList();
// IEnumerable<Word> vocabulary =
//(from w in x.Distinct()
// select new Word { Text = w.ToString() }).ToList();
任何帮助将得到高度赞赏。
答
如果输入看起来像CSV文件,然后再考虑:
var test = "Sunny,1\r\nSunny,2\r\nBobb,1";
var lines = test.Split('\r', '\n');
var vocabulary = lines.Select(z => z.Split(',')[0])
.Where(z => !string.IsNullOrEmpty(z))
.Distinct()
.OrderBy(word => word)
.ToList();
这将让你的文字,每行的逗号前,然后删除重复。
GetWords是一个仿函数(获取您的收集项类型的参数的方法)。 – KamikyIT
它看起来像没有变量或方法命名'GetWords'范围 –
没有,我只是想达到与教程 –