相关疑难解决方法(0)

使用其构造函数初始化OrderedDict以便保留初始数据顺序的正确方法?

初始化有序字典(OD)以保持初始数据顺序的正确方法是什么?

from collections import OrderedDict

# Obviously wrong because regular dict loses order
d = OrderedDict({'b':2, 'a':1}) 

# An OD is represented by a list of tuples, so would this work?
d = OrderedDict([('b',2), ('a', 1)])

# What about using a list comprehension, will 'd' preserve the order of 'l'
l = ['b', 'a', 'c', 'aa']
d = OrderedDict([(i,i) for i in l])
Run Code Online (Sandbox Code Playgroud)

题:

  • 将一个OrderedDict保存的元组的列表的顺序,或元组或列表或列表等的列表的元组的元组通过在初始化的时间(2楼和3上面的例子)?

  • 如何验证是否OrderedDict实际维护订单?由于a dict具有不可预测的顺序,如果我的测试向量幸运地具有与dict的不可预测顺序相同的初始顺序,该怎么办?例如,如果不是d = OrderedDict({'b':2, 'a':1})我写d = OrderedDict({'a':1, …

python sorting dictionary ordereddictionary

109
推荐指数
2
解决办法
7万
查看次数

标签 统计

dictionary ×1

ordereddictionary ×1

python ×1

sorting ×1