Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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
List 将列表转换为保持序列完整的词典_List_Python 2.7_Dictionary_Type Conversion - Fatal编程技术网

List 将列表转换为保持序列完整的词典

List 将列表转换为保持序列完整的词典,list,python-2.7,dictionary,type-conversion,List,Python 2.7,Dictionary,Type Conversion,我有一个巨大的列表,想把它转换成这样一本字典。 样本清单:['a','b','c','d','e','f','g','h'] 输出字典:{'a':'b','c':'d','e':'f','g':'h'} 我希望序列保持完整。我读了另一篇类似的帖子,它使用了itertools的izip。我尝试将其用作: from itertools import izip i = iter(list_name) dic = dict(izip(i, i)) 但它给了我一本全序列混乱的字典。 此外,列表中的元素数

我有一个巨大的列表,想把它转换成这样一本字典。 样本清单:['a','b','c','d','e','f','g','h'] 输出字典:{'a':'b','c':'d','e':'f','g':'h'} 我希望序列保持完整。我读了另一篇类似的帖子,它使用了itertools的izip。我尝试将其用作:

from itertools import izip
i = iter(list_name)
dic = dict(izip(i, i))
但它给了我一本全序列混乱的字典。 此外,列表中的元素数为偶数。

目录无序。您可以使用来保持插入顺序:

from collections import OrderedDict

from itertools import izip
i = iter(list_name)
dic = OrderedDict(izip(i, i))
输出:

In [3]: list_name =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']    
In [4]: i = iter(list_name)   
In [5]: dic = OrderedDict(izip(i, i))   
In [6]: dic
Out[6]: OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]