あなたはこれを行うことができますLINQを使用して分離された辞書のリストに追加します。
List<Dictionary<Blopp,double>> listOfDict =
Enumerable.Range(0,data.First().Value.Count)
.Select(i => data.ToDictionary(x => x.Key, x => x.Value[i]))
.ToList();
EDIT:
までのアプローチの両方が使い捨ての辞書の多くを作成します。もちろん、これらを変更する必要がなく、参照のみを実行する必要がある場合は、これは効率的で無駄です。免責事項
foreach (var partial in dict.AsSeparatedDictionaries())
{
// use partial as a normal IDictionary<Blopp,double>
// if you need the current "i" use partial.Index
}
:
static class MultiDictionaryExtension
{
#region Extension
public static IEnumerable<IDictionary<TK, TV>> AsSeparatedDictionaries<TK, TV>(this IDictionary<TK, List<TV>> multiDict)
{
int numDictionaries = multiDict.First().Value.Count;
for (int i = 0; i < numDictionaries; i++)
yield return new SingleDictionaryWrap<TK, TV>(multiDict, i);
}
#endregion
#region Helper classes
public class SingleDictionaryWrap<TK, TV> : IDictionary<TK, TV>
{
private class ValueCollection : ICollection<TV>
{
private readonly SingleDictionaryWrap<TK, TV> dict;
public ValueCollection(SingleDictionaryWrap<TK, TV> dict)
{
this.dict = dict;
}
public int Count { get { return this.dict.Count; } }
public bool IsReadOnly { get { return false; } }
public void Add(TV item) { throw new NotSupportedException("This dictionary is readonly"); }
public void Clear() { throw new NotSupportedException("This dictionary is readonly"); }
public bool Contains(TV item) { return this.dict.Select(x => x.Value).Contains(item); }
public void CopyTo(TV[] array, int arrayIndex) { foreach (var item in this) array[arrayIndex++] = item; }
public IEnumerator<TV> GetEnumerator() { return this.dict.Select(x => x.Value).GetEnumerator(); }
public bool Remove(TV item) { throw new NotSupportedException("This dictionary is readonly"); }
IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); }
}
private readonly IDictionary<TK, List<TV>> multiDict;
public int Index { get; private set; }
public SingleDictionaryWrap(IDictionary<TK, List<TV>> multiDict, int index)
{
this.Index = index;
this.multiDict = multiDict;
}
public ICollection<TK> Keys { get { return this.multiDict.Keys; } }
public ICollection<TV> Values { get { return new ValueCollection(this); } }
public int Count { get { return this.multiDict.Count; } }
public bool IsReadOnly { get { return true; } }
public TV this[TK key]
{
get { return this.multiDict[key][this.Index]; }
set { throw new NotSupportedException("This dictionary is readonly"); }
}
public bool ContainsKey(TK key) { return this.multiDict.ContainsKey(key); }
public void Add(TK key, TV value) { throw new NotSupportedException("This dictionary is readonly"); }
public bool Remove(TK key) { throw new NotSupportedException("This dictionary is readonly"); }
public bool TryGetValue(TK key, out TV value)
{
value = default(TV);
List<TV> values;
if (this.multiDict.TryGetValue(key, out values))
{
value = values[this.Index];
return true;
}
return false;
}
public void Add(KeyValuePair<TK, TV> item) { throw new NotSupportedException("This dictionary is readonly"); }
public void Clear() { throw new NotSupportedException("This dictionary is readonly"); }
public bool Contains(KeyValuePair<TK, TV> item)
{
TV value;
if (this.TryGetValue(item.Key, out value))
{
return Object.Equals(value, item.Value);
}
return false;
}
public void CopyTo(KeyValuePair<TK, TV>[] array, int arrayIndex) { foreach (var kvp in this) array[arrayIndex++] = kvp; }
public bool Remove(KeyValuePair<TK, TV> item) { throw new NotSupportedException("This dictionary is readonly"); }
public IEnumerator<KeyValuePair<TK, TV>> GetEnumerator() { return this.multiDict.Select(kvp => new KeyValuePair<TK, TV>(kvp.Key, kvp.Value[this.Index])).GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); }
}
#endregion
}
かなり長い拡張クラスを、それはすべてが簡単になる方法を見て:だから、私はいくつかのラッパークラスを利用し、拡張メソッドのために行くだろうすべての前のコードは、すべての内部リストがまったく同じ長さであることを前提としています。
キーリストペアを含む辞書が1つあります。辞書リストはありません – user3185569
@ user3185569はい、わかっています。しかし、**私は辞書のリストを持っているかのように見なすことができますが、お互いに課しています。 * Value *のリストの各要素は、他のKey-Valueペアの要素と、同じリスト内のバディの要素との関連性が高くなります。 –
@ KonradViltersten:A:3 | B:4 | C:5とA:2 | B:9 | C:7の2つの辞書を考えてみましょう。もう1つはあなたが得られるでしょう:A:3,2 | B:4,9 | C:5,7 ...それはあなたが意味することですか? – digEmAll