2

我有一本包含值列表的字典

列表是在运行时动态添加的。在 c# 中,我如何压缩字典中的所有列表?

样本:

Dictionary<string, List<string>> MyDictionary = new Dictionary<string, List<string>>();

List<int> firstlist= new List<string>();

firstlist.Add("one");

firstlist.Add("two");

firstlist.Add("three");

firstlist.Add("four");

List<int> secondlist= new List<int>();

secondlist.Add(1);

secondlist.Add(2);

secondlist.Add(3);

secondlist.Add(4);

MyDictionary.Add("Words", firstlist);
MyDictionary.Add("Number", secondlist);

我想压缩 mydictionary 中的所有列表,结果将是:

one       1
two       2
three     3
four      4
4

3 回答 3

5

Dictionary给定一个Lists:

var d = new Dictionary<string, List<string>>()
{
    {"first",  new List<string>() {"one", "two", "three"}},
    {"second", new List<string>() {"1",   "2",   "3"}}, 
    {"third",  new List<string>() {"A",   "B",   "C"}}
};

你可以使用这个通用方法:

IEnumerable<TResult> ZipIt<TSource, TResult>(IEnumerable<IEnumerable<TSource>> collection, 
                                            Func<IEnumerable<TSource>, TResult> resultSelector)
{
    var enumerators = collection.Select(c => c.GetEnumerator()).ToList();
    while (enumerators.All(e => e.MoveNext()))
    {
        yield return resultSelector(enumerators.Select(e => (TSource)e.Current).ToList());
    }
}

压缩此字典中的所有列表,例如:

var result = ZipIt(d.Values, xs => String.Join(", ", xs)).ToList();

result就是现在

在此处输入图像描述

请注意,此方法允许您选择如何组合值;在我的示例中,我只是创建了一个,-separated 字符串。你也可以只使用其他东西。

于 2013-01-10T09:11:43.680 回答
0

尝试:

var dictionary = firstlist.Zip(secondlist, (value, key) => new { value, key })
                         .ToDictionary(x => x.key, x => x.value);

看看文档中的这个例子:

 int[] numbers = { 1, 2, 3, 4 };
 string[] words = { "one", "two", "three" };

 var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);

 foreach (var item in numbersAndWords)
     Console.WriteLine(item);

 // This code produces the following output: 
 // 1 one 
 // 2 two 
 // 3 three

编辑:

要访问字典中的列表,请使用:

(List<string>)MyDictionary["words"];
(List<int>)MyDictionary["numbers"];

例如:

List<string> words = (List<string>)MyDictionary["words"];
List<int> numbers = (List<int>)MyDictionary["numbers"];
for (int i = 0; i < words.Count; i++) // Loop through List with for
{
    Console.WriteLine(words[i] + numbers [i]);
}
于 2013-01-10T08:59:59.910 回答
0

让我们从文档示例向后工作:

    int[] numbers = { 1, 2, 3, 4 };
    string[] words = { "one", "two", "three" };

    var numbersAndWords = numbers.Zip(words, (first, second) => first +
         " " + second);

    foreach (var item in numbersAndWords)
        Console.WriteLine(item);

    // This code produces the following output: 

    // 1 one 
    // 2 two 
    // 3 three

那么从上面使用你的字典将是:

    var numbersAndWords = MyDictionary["Words"]
        .Zip(MyDictionary["Number"], (first, second) => first + "\t" + second);

    foreach (var item in numbersAndWords)
        Console.WriteLine(item);

    // This code produces the following output: 

    // one       1
    // two       2
    // three     3
    // four      4
于 2013-01-10T09:07:59.280 回答