如何提取字符串列表中常见的所有单词?
示例:
int
我试过以下:
//Output = Bammler GOV
"Bammler Tokyo SA GOV"
"Zurich Bammler GOV"
"London Bammler 12 GOV"
"New Bammler York GOV"
我在Find a common string within a list of strings找到了这个,但这只会提取例如" Bammler"。
答案 0 :(得分:4)
你可以aggregate从所有字符串中找到单词的结果:
var result = MyStringList.Select(s => s.Split())
.Aggregate(
MyStringList[0].Split().AsEnumerable(), // init accum with words from first string
(a, words) => a.Intersect(words), // intersect with next set of words
a => a);
输出:
[
"Bammler",
"GOV"
]
答案 1 :(得分:1)
我会选择@Sergey回答,但我想添加你也可以使用哈希来获得交集:
var list = new List < string >{ "Bammler Tokyo SA GOV",
"Zurich Bammler GOV",
"London Bammler 12 GOV",
"New Bammler York GOV"};
var hash = new HashSet<string> ( list.First().Split(' ') );
for (int i = 1; i < list.Count; i++)
hash.IntersectWith(list[i].Split(' '));