Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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 &引用;OrderedDict()”的缩写;使用OrderedDict()时自动打印_Python_Python 3.x_Dictionary_Ordereddictionary - Fatal编程技术网

Python &引用;OrderedDict()”的缩写;使用OrderedDict()时自动打印

Python &引用;OrderedDict()”的缩写;使用OrderedDict()时自动打印,python,python-3.x,dictionary,ordereddictionary,Python,Python 3.x,Dictionary,Ordereddictionary,我试图使用OrderedDict打印一个有序的字典,但是当我打印它时,“OrderedDict”也会打印出来。仅供参考,这只是一段代码,而不是整个代码。我能做些什么来解决这个问题?我正在使用Python 3.2 看起来像这样: def returnAllStats(ints): choices = ["Yes","No"] dictInfo = {"Calories":ints[2], "Servings per Container":ints[0], "Amount per S

我试图使用OrderedDict打印一个有序的字典,但是当我打印它时,“OrderedDict”也会打印出来。仅供参考,这只是一段代码,而不是整个代码。我能做些什么来解决这个问题?我正在使用Python 3.2

看起来像这样:

def returnAllStats(ints):
    choices = ["Yes","No"]
    dictInfo = {"Calories":ints[2], "Servings per Container":ints[0], "Amount per Serving":ints[1], "Total Fat":(ints[3]/100)*ints[2], "Saturated Fat":(ints[4]/100)*(ints[3]/100)*ints[2], "Cholesterol":ints[5], "Fiber":ints[6], "Sugar":ints[7], "Protein":ints[8], "Sodium":ints[9], "USA":choices[ints[10]], "Caffeine":ints[11]}
    dictInfo = collections.OrderedDict(dictInfo)
    return dictInfo
我把它放在文本文件中,这是在写:

('snack', 'bananana')OrderedDict([('USA', 'No'), ('Sodium', 119), ('Calories', 479), ('Servings per Container', 7), ('Sugar', 49), ('Saturated Fat', 37.553599999999996), ('Total Fat', 234.71), ('Cholesterol', 87), ('Amount per Serving', 40), ('Fiber', 1), ('Caffeine', 7), ('Protein', 53)])

谢谢

您有几种选择

您可以使用列表理解并打印:

>>> od
OrderedDict([('one', 1), ('two', 2), ('three', 3)])
>>> [(k,v) for k,v in od.items()]
[('one', 1), ('two', 2), ('three', 3)] 
或者,知道顺序可能会改变,如果需要输出,您可以转换为dict:

>>> dict(od)
{'one': 1, 'two': 2, 'three': 3}
(对于Python3.6,常规的
dict
。使用Python3.6,顺序不会改变。将来可能会出现这种情况,但还不能保证。)

最后,您可以将
OrderDict
子类化,并用所需的格式替换
\uuuu str\uuuu
方法:

class Mydict(OrderedDict):
    def __str__(self):
        return ''.join([str((k, v)) for k,v in self.items()])

>>> md=Mydict([('one', 1), ('two', 2), ('three', 3)])   
>>> md     # repr
Mydict([('one', 1), ('two', 2), ('three', 3)])
>>> print(md)
('one', '1')('two', '2')('three', '3')
(如果希望repr的输出不同,请更改
\uuuu repr\uuu
方法…)


最后说明:

为此:

def returnAllStats(ints):
    choices = ["Yes","No"]
    dictInfo = {"Calories":ints[2], "Servings per Container":ints[0], "Amount per Serving":ints[1], "Total Fat":(ints[3]/100)*ints[2], "Saturated Fat":(ints[4]/100)*(ints[3]/100)*ints[2], "Cholesterol":ints[5], "Fiber":ints[6], "Sugar":ints[7], "Protein":ints[8], "Sodium":ints[9], "USA":choices[ints[10]], "Caffeine":ints[11]}
    dictInfo = collections.OrderedDict(dictInfo)
    return dictInfo
实际上,您得到的是一个无序的dict结果,因为您是从无序的dict文本创建OrderedDict的

您可能希望执行以下操作:

def returnAllStats(ints):
    choices = ["Yes","No"]
    return collections.OrderedDict([("Calories",ints[2]), ("Servings per Container",ints[0]), ("Amount per Serving",ints[1]), ("Total Fat",(ints[3]/100)*ints[2]), ("Saturated Fat",(ints[4]/100)*(ints[3]/100)*ints[2]), ("Cholesterol",ints[5]), ("Fiber",ints[6]), ("Sugar",ints[7]), ("Protein",ints[8]), ("Sodium",ints[9]), ("USA",choices[ints[10]]), ("Caffeine",ints[11])]}
    return dictInfo

你有几个选择

您可以使用列表理解并打印:

>>> od
OrderedDict([('one', 1), ('two', 2), ('three', 3)])
>>> [(k,v) for k,v in od.items()]
[('one', 1), ('two', 2), ('three', 3)] 
或者,知道顺序可能会改变,如果需要输出,您可以转换为dict:

>>> dict(od)
{'one': 1, 'two': 2, 'three': 3}
(对于Python3.6,常规的
dict
。使用Python3.6,顺序不会改变。将来可能会出现这种情况,但还不能保证。)

最后,您可以将
OrderDict
子类化,并用所需的格式替换
\uuuu str\uuuu
方法:

class Mydict(OrderedDict):
    def __str__(self):
        return ''.join([str((k, v)) for k,v in self.items()])

>>> md=Mydict([('one', 1), ('two', 2), ('three', 3)])   
>>> md     # repr
Mydict([('one', 1), ('two', 2), ('three', 3)])
>>> print(md)
('one', '1')('two', '2')('three', '3')
(如果希望repr的输出不同,请更改
\uuuu repr\uuu
方法…)


最后说明:

为此:

def returnAllStats(ints):
    choices = ["Yes","No"]
    dictInfo = {"Calories":ints[2], "Servings per Container":ints[0], "Amount per Serving":ints[1], "Total Fat":(ints[3]/100)*ints[2], "Saturated Fat":(ints[4]/100)*(ints[3]/100)*ints[2], "Cholesterol":ints[5], "Fiber":ints[6], "Sugar":ints[7], "Protein":ints[8], "Sodium":ints[9], "USA":choices[ints[10]], "Caffeine":ints[11]}
    dictInfo = collections.OrderedDict(dictInfo)
    return dictInfo
实际上,您得到的是一个无序的dict结果,因为您是从无序的dict文本创建OrderedDict的

您可能希望执行以下操作:

def returnAllStats(ints):
    choices = ["Yes","No"]
    return collections.OrderedDict([("Calories",ints[2]), ("Servings per Container",ints[0]), ("Amount per Serving",ints[1]), ("Total Fat",(ints[3]/100)*ints[2]), ("Saturated Fat",(ints[4]/100)*(ints[3]/100)*ints[2]), ("Cholesterol",ints[5]), ("Fiber",ints[6]), ("Sugar",ints[7]), ("Protein",ints[8]), ("Sodium",ints[9]), ("USA",choices[ints[10]]), ("Caffeine",ints[11])]}
    return dictInfo

如果您不关心订单,只需打印
dict(您的Ordereddict)
,如果您关心订单,您可以:

for key, value in yourOrderedDict.items():
    print(key, value)

希望它能有所帮助

如果您不关心订单,只需打印
dict(您的订单信息)
,如果您关心订单,您可以:

for key, value in yourOrderedDict.items():
    print(key, value)

希望对您有所帮助

您知道,在您的示例中,
dictInfo
的输出顺序是无序的,因为您首先创建了一个常规dict,然后才创建了一个
OrderedDict
,对吗?您知道在您的示例中,
dictInfo
的输出顺序是无序的,因为您首先创建了一个常规dict,然后再从中创建一个
OrderedDict
,对吗?