Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将包含3个元素的元组列表转换为有序字典_Python_Python 2.7_Ordereddictionary - Fatal编程技术网

Python 将包含3个元素的元组列表转换为有序字典

Python 将包含3个元素的元组列表转换为有序字典,python,python-2.7,ordereddictionary,Python,Python 2.7,Ordereddictionary,我有一个包含3个元素的元组列表。我想把它转换成保留顺序的dict >>> a = [('one', '0', '1'), ('two', '0', '0'), ('three', '1', '1')] >>> x = OrderedDict({sb[0]: sb[1:] for sb in a}) >>> x OrderedDict([('three', ('1', '1')), ('two', ('0', '0')), ('one', ('

我有一个包含3个元素的元组列表。我想把它转换成保留顺序的dict

>>> a = [('one', '0', '1'), ('two', '0', '0'), ('three', '1', '1')]
>>> x = OrderedDict({sb[0]: sb[1:] for sb in a})
>>> x
OrderedDict([('three', ('1', '1')), ('two', ('0', '0')), ('one', ('0', '1'))])
我看到订单变了,不知道为什么。有人能帮我解决这个问题吗

哎呀

哎呀


你不需要一套这样的父母;当函数(本例中为构造函数)的唯一参数是genexpr时,调用参数会加倍为genexpr参数,因此这可以正常工作:
OrderedDict((sb[0],sb[1:])对于a中的sb)
。重要的部分是删除
{}
(这使它成为一个
dict
理解),因为如果你先做一个
dict
,你已经失去了订单,所以
orderedict
无法恢复;当函数(本例中为构造函数)的唯一参数是genexpr时,调用参数会加倍为genexpr参数,因此这可以正常工作:
OrderedDict((sb[0],sb[1:])对于a中的sb)
。重要的部分是删除
{}
(这使它成为一个
dict
理解),因为如果你先做一个
dict
,你已经失去了订单,因此
orderedict
无法恢复。
...{...}..
OrderedDict(((sb[0], sb[1:]) for sb in a))