Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 输出顺序不正确_Python_Python 3.x_Python 2.7_Dictionary - Fatal编程技术网

Python 输出顺序不正确

Python 输出顺序不正确,python,python-3.x,python-2.7,dictionary,Python,Python 3.x,Python 2.7,Dictionary,我在python2.7中有一段代码,它获取一行文本作为其列表元素,并将其作为json返回。代码如下: import itertools lines=["Hamlet" ,"William Shakespeare", "Edited Barbara B Mowat Paul Werstine" ,"Michael Poston Rebecca Niles"] LinesMap=dict() for line in lines: list=[l for l in line

我在python2.7中有一段代码,它获取一行文本作为其列表元素,并将其作为json返回。代码如下:

import itertools

lines=["Hamlet"
   ,"William Shakespeare",
   "Edited Barbara B Mowat Paul Werstine"
   ,"Michael Poston Rebecca Niles"]
LinesMap=dict()
for line in lines:
    list=[l for l in line.split(' ')]
    d = dict(itertools.izip_longest(*[iter(list)] * 2, fillvalue=None))
    LinesMap.update(d)

print(LinesMap)
输出为:

{'William': 'Shakespeare', 'Edited': 'Barbara', 'B': 'Mowat',    

'Michael':'Poston', 'Paul': 'Werstine', 'Rebecca': 'Niles', 'Hamlet': None}
鉴于它应该是:

{'Hamlet': None, 'William': 'Shakespeare', 'Edited': 'Barbara', 

'B':'Mowat', 'Paul': 'Werstine', 'Michael': 'Poston', 'Rebecca': 'Niles'}
如果我把名单再长一点,情况会更糟!为什么顺序不正确?但是当我在python3.6中运行相同的代码时,当然使用python3语法,顺序是正确的。python3.6代码是:

import itertools

lines=["Hamlet"
   ,"William Shakespeare",
   "Edited Barbara B Mowat Paul Werstine"
   ,"Michael Poston Rebecca Niles"]
LinesMap=dict()
for line in lines:
   list=[l for l in line.split(' ')]
   d = dict(itertools.zip_longest(*[iter(list)] * 2, fillvalue=None))
   LinesMap = {**LinesMap, **d}

print(LinesMap) 
这是一个问题。另一个是,对于短列表,它可以正常运行。但是,当列表较长且元素过多时,输出不会显示任何内容,并且似乎已中断。这在windows和linux中都不会中断。有什么问题


ps:出于某些原因,我不得不在Python2.7中运行它

Python
dict
对象没有内在的顺序,除非是在Python的最新版本中,所以您不能假设您将以与放入对象相同的顺序取出对象

如果您需要该功能,可以使用一个
orderedict
对象

import random
from collections import OrderedDict

# Normal dict behavior:
numbers = {}
random_nums = (random.random() for _ in range(1000))
for i in random_nums:
    numbers[i] = random.random()
assert list(numbers.keys()) != list(random_nums)

# Ordered dict behavior:
numbers = OrderedDict()
for i in random_nums:
    numbers[i] = random.random()
assert list(numbers.keys()) == list(random_nums)

Python
dict
对象没有内在的顺序,除了在Python的最新版本中,因此您不能假设您将以与放入对象相同的顺序取出对象

如果您需要该功能,可以使用一个
orderedict
对象

import random
from collections import OrderedDict

# Normal dict behavior:
numbers = {}
random_nums = (random.random() for _ in range(1000))
for i in random_nums:
    numbers[i] = random.random()
assert list(numbers.keys()) != list(random_nums)

# Ordered dict behavior:
numbers = OrderedDict()
for i in random_nums:
    numbers[i] = random.random()
assert list(numbers.keys()) == list(random_nums)

Python 3.6之前没有对字典进行排序-这意味着在早期版本中不能依赖任何特定的顺序。如果限制为2.7,您可能希望尝试从集合导入排序dict,也可以在“Hamlet”中添加一个空格,以便添加“None”条目。看起来您希望它们按输入顺序。请尝试使用
从集合导入顺序dict
,而不是使用
dict
。Python 3.6之前没有对词典进行排序-这意味着您不能依赖早期版本中的任何特定顺序。如果限制为2.7,您可能希望尝试使用
从集合导入顺序dict
,也可以在“Hamlet”中添加空格以便添加“无”条目。看起来您希望它们按输入顺序排列。尝试从集合导入排序dict中使用它,而不是使用
dict
。是的,尽管您的
assert
不能证明任何东西,但常规
dict
也会在Python 2中传递此断言(编辑:因为整数大部分都是散列的-回答了您下面的问题),我们开始了,使用随机数而不是证明数据结构是有序的是不可能的,从一些例子来看,你应该只链接到文档或语言规范,使用OrderedDict而不是dict,输出是:OrderedDict([('Hamlet',None),('William','Shakespeare'),('Edit','Barbara'),('B','Mowat'),('Paul','Werstine'),('Michael','Poston'),('Rebecca','Niles')]),因此顺序是正确的。但是我如何才能获得这种格式的所需输出:{'Hamlet':无,'William':'Shaspeare','Edited':'Barbara','B':'Mowat','Paul':'Werstine','Michael':'Poston','Rebecca':'Niles'}为什么要这样格式化?这种格式化表示所表示的对象是一个普通的旧Dict,OrderedDict不是。是的,尽管您的
assert
不能证明任何东西,但常规
Dict
也会在Python 2中传递此断言(编辑:因为整数大多散列到自身-回答您下面的问题)好了,用随机数代替证明数据结构是有序的,从几个例子来看是不可能的,相反,你应该链接到文档或语言规范,使用OrderedDict而不是dict,输出是:OrderedDict([('Hamlet',None),('William','Shakespeare'),('Edited'、'Barbara')、('B'、'Mowat')、('Paul'、'Werstine')、('Michael'、'Poston')、('Rebecca'、'Niles'))的顺序是正确的。但我如何才能获得这种格式的所需输出:{'Hamlet':无,'William':'Shaspeare','Edited':'Barbara','B':'Mowat','Paul':'Werstine','Michael':'Poston','Rebecca':'Niles'}为什么要这样格式化?这种格式表示所表示的对象是一个普通的旧格言,而有序的格言则不是。