简体   繁体   English

IGrouping <int, Keypair> 到字典

[英]IGrouping<int, Keypair> to Dictionary

I seperated my main dictonary into a group of multiple dictonaries so that I can shoot threads off with the smaller set of dictonaries. 我将主要词典分为多个词典,这样我就可以用较小的词典来删除主题。

Here is the code that seperates my main Dictonary into multiple smaller ones: 这是将我的主字典分为多个较小的代码的代码:

int numberOfGroups = 10;
int counter = 0;
var result = offenderWorkload.GroupBy(x => counter++ % numberOfGroups);

Now I am having trouble to obtain one dictonary from the set of 10 within the result. 现在,我很难从结果中的10个集合中获取一个字典。

I need to seperate result into 10 different dictonaries and shoot off threads so something like... 我需要将结果分成10个不同的字典,然后打断线程,例如...

foreach(var something in result)
{
    Dictionary<String, int> workLoad = (Dictionary<String, int>)something.ToDictionary();
    Console.WriteLine("workload: " + something.Key + " has " + workLoad.Keys.Count);
}

So that something is keypair that gets casted to a dictonary and ignoring the int in the Igroup. 这样,密钥对中的某些内容将被转换为字典,而忽略了Igroup中的int。

Dictionary<String, int> workLoad = something.ToDictionary(x => x.Key, x => x.Value);

因为在进行分组之后,每个分组成员都不是字典,而是KeyValuePair的集合。

This will give you a Group of dictionaries. 这将为您提供一组词典。 each dictionary have n elements. 每个字典有n元素。

int n = (int)Math.Ceiling((double)offenderWorkload.Count/numberOfGroups);

IEnumerable<Dictionary<string, int>> result =
    offenderWorkload.GroupBy(x => counter++/n)
        .Select(x => x.ToDictionary(d => d.Key, d => d.Value));

Note that if your dictionary have 12 keys and you want to group them in to 5 dictionaries you will get 4 dictionaries instead, each one have 3 keys. 请注意,如果您的词典有12个键,并且要将它们分组为5个词典,则您将获得4个词典,而每个词典都有3个键。

Because 12/5 = 2.4 . 因为12/5 = 2.4 you can have 5 dictionaries with 2 keys plus one dictionary with two keys 2*5+0.4*5 . 您可以拥有2个键的5个字典,以及两个键2*5+0.4*5一个字典。 In total 6 dictionaries which is more than maximum number of groups. 总共6个词典,超过了最多组数。 Or better way, by taking its ceiling you will get maximum possible (or less) than amount of specified groups. 或者更好的方法是,将其上限设为最大(或小于)指定组的数量。 Here [2.4] = 3 . 这里[2.4] = 3 in total 4 dictionaries each have 3 keys. 共有4个词典,每个词典有3个键。

Another example if you want to group 12 into 7 dictionaries you will get 5 dictionaries instead each one have 2 keys. 另一个示例,如果要将12个字典分组为7个字典,则将获得5个字典,而每个字典有2个键。 If you group 13 into 3 you will get 3 dictionaries, Two first dictionaries will have 5 keys and Last one have 3 keys. 如果将13分组为3,则将获得3个字典,前两个字典将具有5个键,后一个字典将具有3个键。

In order to get maximum numberOfGroups that you have specified choose something that offenderWorkload.Count can be divided to. 为了获得您指定的最大numberOfGroups ,请选择可以将offenderWorkload.Count划分为一些对象。 for example If you group 12 into 6 you will get 6 dictionaries each one have 2 keys ( 12 / 6 = 2 ). 例如,如果将12分组为6,则将得到6个字典,每个字典有2个键( 12 / 6 = 2 )。

int numberOfGroups = 10;
int counter = 0;
int n = (int)Math.Ceiling((double)offenderWorkload.Count/numberOfGroups);

var result =
    offenderWorkload.GroupBy(x => counter++/n)
        .Select(x => x.ToDictionary(d => d.Key, d => d.Value));

int i = 0;
foreach (var workLoad in result)
{
    Console.WriteLine("workload: " + i++ + " has " + workLoad.Keys.Count);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM