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
列表withloop中字典的Python打印键(OrderedDict)_Python_Python 3.x_Ordereddictionary - Fatal编程技术网

列表withloop中字典的Python打印键(OrderedDict)

列表withloop中字典的Python打印键(OrderedDict),python,python-3.x,ordereddictionary,Python,Python 3.x,Ordereddictionary,我正在试图找出如何打印一个列表,storeItems,其中包含3个orderedict(),并且能够为每个orderedict打印键和值。我不知道如何解决这个问题,这就是我尝试的,它给了我一个类型错误:字符串索引必须是整数 storeItems = [{}, {}, {}] storeItems[0] = OrderedDict() storeItems[1] = OrderedDict() storeItems[2] = OrderedDict() storeItems[0]['Name']

我正在试图找出如何打印一个列表,
storeItems
,其中包含3个
orderedict()
,并且能够为每个
orderedict
打印
键和
值。我不知道如何解决这个问题,这就是我尝试的,它给了我一个
类型错误:字符串索引必须是整数

storeItems = [{}, {}, {}]
storeItems[0] = OrderedDict()
storeItems[1] = OrderedDict()
storeItems[2] = OrderedDict()

storeItems[0]['Name'] = 'Auto-Enter'
storeItems[0]['Price'] = 30
storeItems[0]['Max'] = 100

storeItems[1]['Name'] = 'Multiplier'
storeItems[1]['Price'] = 100
storeItems[1]['Max'] = 5

storeItems[2]['Name'] = 'Factory'
storeItems[2]['Price'] = 100
storeItems[2]['Max'] = 3

#the original layout of the storeItems

#storeItems = [{
#        "Name": "Auto-Enter",
#        "Price": 30,
#        "Max": 100
#    }, {
#        "Name": "Multiplier",
#        "Price": 100,
#        "Max": 5
#    }, {
#        "Name": "Factory",
#        "Price": 200,
#        "Max": 3
#    }]
i = 0
for value in storeItems:
    for key in value:
        for i in range(0, 2):
            print(storeItems[i][key], storeItems[i][key][value])
注意事项 我发现对storeItems中的值执行
:打印(值)
将为每行打印
OrderedDict([('Name':'Auto-Enter',…)])
<代码>打印(storeItems[0]['Name'])
输出
自动输入
。我似乎很难把这一点纳入循环


我更希望它尽可能接近原始格式的
storeItems

storeItems
中的每个元素都是一个字典(
orderedict
只是一个跟踪插入顺序的子类),所以只需循环:

for odict in storeItems:
    for key, value in odict.items():
        print(key, value)