C# ienumerable<;字符串>;到字典<;字符串,int>;

C# ienumerable<;字符串>;到字典<;字符串,int>;,c#,linq,c#-4.0,C#,Linq,C# 4.0,我使用以下代码将字符串数组拆分为列表 private List<string> GenerateTerms(string[] docs) { return docs.SelectMany(doc => ProcessDocument(doc)).Distinct().ToList(); } private IEnumerable<string> ProcessDocument(string doc) {

我使用以下代码将字符串数组拆分为列表

private List<string> GenerateTerms(string[] docs)
    {
        return docs.SelectMany(doc => ProcessDocument(doc)).Distinct().ToList();
    }

    private IEnumerable<string> ProcessDocument(string doc)
    {
        return doc.Split(' ')
                  .GroupBy(word => word)
                  .OrderByDescending(g => g.Count())
                  .Select(g => g.Key)
                  .Take(1000);
    }
私有列表生成器(字符串[]文档)
{
return docs.SelectMany(doc=>ProcessDocument(doc)).Distinct().ToList();
}
私有IEnumerable进程文档(字符串文档)
{
退货单拆分(“”)
.GroupBy(word=>word)
.OrderByDescending(g=>g.Count())
.选择(g=>g.Key)
.取(1000);
}
我要做的是将返回的列表替换为

Dictionary <string, int>
字典
i、 e.我想返回字典,而不是返回列表


有人能帮忙吗??提前谢谢

在没有任何额外杂质的情况下,以下各项应能正常工作

return doc.Split(' ')
          .GroupBy(word => word)
          .ToDictionary(g => g.Key, g => g.Count());

根据您的情况,通过
Take
OrderBy
等方式进行定制。

尝试以下方法:

    var keys = new List<string>();
    var values = new List<string>();
    var dictionary = keys.ToDictionary(x => x, x => values[keys.IndexOf(x)]);
var keys=new List();
var值=新列表();
var dictionary=keys.ToDictionary(x=>x,x=>value[keys.IndexOf(x)];
编辑:

var result = docs.SelectMany(
        doc => doc.Split()
            .GroupBy(word => word)
            .OrderByDescending(g => g.Count())
            .Take(1000))
    .Select(g => new {Word = g.Key, Cnt = g.Count()})
    .GroupBy(t => t.Word)
    .ToDictionary(g => g.Key, g => g.Sum(t => t.Cnt));
我相信您希望从字符串数组中获得最终的字典,它以单词作为键,以它们的最终计数作为值。因为字典不能包含重复的值,所以不需要使用
Distict
。 您必须将方法重新编写为:

private Dictionary<string,int> GenerateTerms(string[] docs)
{
    List<Dictionary<string, int>> combinedDictionaryList = new List<Dictionary<string, int>>();
    foreach (string str in docs)
    {
        //Add returned dictionaries to a list
        combinedDictionaryList.Add(ProcessDocument(str));
    }
    //return a single dictionary from list od dictionaries
    return combinedDictionaryList
            .SelectMany(dict=> dict)
            .ToLookup(pair => pair.Key, pair => pair.Value)
            .ToDictionary(group => group.Key, group => group.Sum(value => value));
}

private Dictionary<string,int> ProcessDocument(string doc)
{
    return doc.Split(' ')
            .GroupBy(word => word)
            .OrderByDescending(g => g.Count())
            .Take(1000)
            .ToDictionary(r => r.Key, r => r.Count());
}
专用字典生成器(字符串[]文档)
{
List combinedDictionaryList=新列表();
foreach(文档中的字符串str)
{
//将返回的词典添加到列表中
combinedDictionalList.Add(ProcessDocument(str));
}
//从词典列表中返回单个词典
返回组合字典列表
.SelectMany(dict=>dict)
.ToLookup(pair=>pair.Key,pair=>pair.Value)
.ToDictionary(group=>group.Key,group=>group.Sum(value=>value));
}
私有字典处理文档(字符串文档)
{
退货单拆分(“”)
.GroupBy(word=>word)
.OrderByDescending(g=>g.Count())
.取(1000)
.ToDictionary(r=>r.Key,r=>r.Count());
}
然后你可以这样称呼它:

string[] docs = new[] 
    {
        "This is a test sentence with some words with some words repeating like: is a test",
        "This is a test sentence with some words with some words repeating like: is a test",
        "This is a test sentence with some words",
        "This is a test sentence with some words",
    };

Dictionary<string, int> finalDictionary = GenerateTerms(docs);
string[]docs=new[]
{
“这是一个测试句子,其中一些单词重复出现,如:is a test”,
“这是一个测试句子,其中一些单词重复出现,如:is a test”,
“这是一个带有单词的测试句子”,
“这是一个带有单词的测试句子”,
};
字典finalDictionary=生成器(文档);
试试这个:

string[] docs = {"aaa bbb", "aaa ccc", "sss, ccc"};        

var result = docs.SelectMany(doc => doc.Split())
                 .GroupBy(word => word)
                 .OrderByDescending(g => g.Count())
                 .ToDictionary(g => g.Key, g => g.Count())
                 .Take(1000);
编辑:

var result = docs.SelectMany(
        doc => doc.Split()
            .GroupBy(word => word)
            .OrderByDescending(g => g.Count())
            .Take(1000))
    .Select(g => new {Word = g.Key, Cnt = g.Count()})
    .GroupBy(t => t.Word)
    .ToDictionary(g => g.Key, g => g.Sum(t => t.Cnt));

但是我有很多医生。这段代码是否符合我在原始帖子中的要求???@Qaesar,是否希望您的方法
GenerateTerms
返回字符串数组中所有元素的组合字典?@Habib。。。1-对每个文档进行拆分、分组、排序并获取(1000),然后将所有文档合并到一个没有重复元素(不同)的字典@Qaesar,因此对于合并的字典,您希望根据单个关键字将每个单词的计数加在一起吗?@Qaesar,如果您正在查找,请检查编辑的答案for@Qaesar,“取(1000)对于每一个医生”-对不起我的错误@凯萨哇,我有负面反馈!答案已更新。你觉得这个怎么样?