[英]Best way to convert Dictionary to two lists
如何将Dictionary<DateTime, double>
类型的Dictionary<DateTime, double>
转换为Tuple<List<DateTime>, List<double>>
?
谢谢
编辑:以下是否保证两个列表中的项目顺序相同? var abc = new Tuple<List<DateTime>, List<double>>(_data.Keys.ToList(), _data.Values.ToList());
简单(订单有保证):
Tuple<List<DateTime>, List<double>> tuple
= Tuple.Create(dict.Keys.ToList(), dict.Values.ToList());
Dictionary.ValueCollection中的值的顺序是未指定的,但它与Keys属性返回的Dictionary.KeyCollection中的关联键的顺序相同。
资料来源: MSDN
订单保证示例:
订单即使在更新,删除和添加后也能得到保证。
Dictionary<int, string> dict = new Dictionary<int, string>();
dict[2] = "2";
dict[1] = "0";
dict[3] = "3";
dict[1] = "1";
dict[1] = "1";
dict.Remove(3);
var tuple = Tuple.Create(dict.Keys.ToList(), dict.Values.ToList());
// 2 1
// "2" "1"
var result = Tuple.Create(dict.Keys.ToList(), dict.Values.ToList());
根据你评论中的订单问题:
你得到的订单无法保证,因为字典不是有序集合。 但是它保证你在Keys
得到相同的 - 和Values
集合,如果你使用foreach(KeyValuepair<DateTime,double> kv in dict)
。
MSDN:密钥 :
Dictionary(Of TKey,TValue).KeyCollection中键的顺序是未指定的,但它与Values属性中返回的Dictionary(Of TKey,TValue).ValueCollection中的相关值的顺序相同。
MSDN:值 :
Dictionary(Of TKey,TValue).ValueCollection中的值的顺序是未指定的,但它与Keys属性返回的Dictionary(Of TKey,TValue).KeyCollection中的关联键的顺序相同。
你问过:
那么,例如,
dict[result.Item1[5]] == result.Item2[5]
成立?
是的,根据上面的文档证明了这一点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.