通过嵌套的dict循环只返回最后一个值,除非我将它分配给另一个dict

时间:2015-11-09 03:01:31

标签: python python-2.7 loops dictionary

我有以下结构的字母products

{'Cheese': {'Cheese': {'comment': 'Markets quiet this week', 'indicator': 'Flat', 'pricelow': '3385', 'priceaverage': '3385', 'pricehigh': '3385'}}, 'MPC': {'MPC70': {'comment': 'Large buyers', 'indicator': 'Flat', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}, 'MPI:': {'comment': 'Large buyers', 'indicator': 'Flat', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}, 'MPC85': {'comment': 'Large buyers', 'indicator': 'Flat', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}}}

我想解决嵌套结构,为此我想取产品名称“Cheese”,“MPC70”,“MPI”,“MPC85”,将其添加到内部字典中作为“名称”并将其全部放入它在列表中。 (类别(“奶酪”和“MPC”不再重要,将会丢失)有类似的东西:

[{'indicator': 'Flat', 
 'comment': 'Markets quiet this week', 
 'name': 'Cheese', 
 'pricelow': '3385', 
 'priceaverage': '3385', 
 'pricehigh': '3385'}, 
{'indicator': 'Flat', 
 'comment': 'Large buyers.High Ground.', 
 'name': 'MPC70', 
 'pricelow': '4189', 
 'priceaverage': '4409', 
 'pricehigh': '5070'}, 
{'indicator': 'Flat', 
 'comment': 'Large buyers.High Ground.', 
 'name': 'MPI:', 
 'pricelow': '8598', 
 'priceaverage': '8929', 
 'pricehigh': '9039'}, 
{'indicator': 'Flat', 
 'comment': 'Large buyers.High Ground.', 
 'name': 'MPC85', 
 'pricelow': '8377', 
 'priceaverage': '8488', 
 'pricehigh': '8818'}
]

到目前为止一直很好但是当我在“MPC”类别的所有产品的字典中循环时,名称是“MPC85” 我的循环看起来像这样:

for item in products:
  for subitem in products[item]:
    products[item][subitem]['name'] = subitem
    productlist.append(products[item][subitem])
    #print products[item][subitem]
    #print products[item][subitem]['name']
    #print  item + ' ' + subitem
print productlist

对于任何评论的打印报表,输出完全符合我的要求,但是如果我打印整个列表,每个“MPC”产品的“名称”键是“MPC85”\

经过一些试验和错误以及@spectras确认循环实际工作后,我想如果我将dict分配给另一个变量

products2 = products

并将循环更改为循环遍历products2而不是products整个过程都有效。

所以问题是:为什么python似乎有问题循环一个dict,而当我将dict分配给另一个变量时它会按预期执行?

编辑: 完全转储我所做的和我得到的:

In [3]: print products
{'Cheese': {'Cheese': {'comment': 'Markets quiet this week', 'indicator': 'Flat', 'pricelow': '3385', 'priceaverage': '3385', 'pricehigh': '3385'}}, 'MPC': {'MPC70': {'comment': 'Large buyers.High Ground.', 'indicator': 'Flat', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}, 'MPI:': {'comment': 'Large buyers.High Ground.', 'indicator': 'Flat', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}, 'MPC85': {'comment': 'Large buyers.High Ground.', 'indicator': 'Flat', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}}, 'Whey Powder': {'Whey Powder': {'comment': 'Dry Whey futures drop', 'indicator': 'Down', 'pricelow': '1124', 'priceaverage': '1124', 'pricehigh': '1124'}}, 'Casein': {'Casein': {'comment': 'No changes.', 'indicator': 'Down', 'pricelow': '7165', 'priceaverage': '7605', 'pricehigh': '8157'}}, 'Lactose': {'Lactose': {'comment': 'Lactose prices remain unchanged', 'indicator': 'Down', 'pricelow': '683', 'priceaverage': '683', 'pricehigh': '683'}}, 'Powders': {'NFDM': {'comment': 'Export volumes dropped.', 'indicator': 'Up', 'pricelow': '2408', 'priceaverage': '2408', 'pricehigh': '2408'}}, 'Caseinate': {'Caseinate': {'comment': 'actively selling.requested pricing.', 'indicator': 'Down', 'pricelow': '7716', 'priceaverage': '8598', 'pricehigh': '9479'}}, 'Cream': {'Butter': {'comment': 'Butter stocks increased', 'indicator': 'Down', 'pricelow': '3847', 'priceaverage': '3847', 'pricehigh': '3847'}}, 'WPC': {'WPC80': {'comment': 'WPC 80 \xe2\x80\x93 Supply continues.', 'indicator': 'Down', 'pricelow': '5300', 'priceaverage': '7165', 'pricehigh': '8267'}, 'IWPC80': {'comment': 'WPC 80 \xe2\x80\x93 Supply continues.', 'indicator': 'Down', 'pricelow': '5300', 'priceaverage': '7165', 'pricehigh': '8267'}}}

In [4]: %paste
        for item in products:
                for subitem in products[item]:
                        products[item][subitem]['name'] = subitem
                        #print products[item][subitem]
                        #print products[item][subitem]['name']
                        productlist.append(products[item][subitem])
                        #print  item + ' ' + subitem
                        #print products[item][subitem]['name']
                        #print products[item]
                        #print products[item][subitem]
        print productlist

## -- End pasted text --
[{'comment': 'Markets quiet this week', 'indicator': 'Flat', 'name': 'Cheese', 'pricelow': '3385', 'priceaverage': '3385', 'pricehigh': '3385'}, {'comment': 'Large buyers.High Ground.', 'indicator': 'Flat', 'name': 'MPC85', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}, {'comment': 'Large buyers.High Ground.', 'indicator': 'Flat', 'name': 'MPC85', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}, {'comment': 'Large buyers.High Ground.', 'indicator': 'Flat', 'name': 'MPC85', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}, {'comment': 'Dry Whey futures drop', 'indicator': 'Down', 'name': 'Whey Powder', 'pricelow': '1124', 'priceaverage': '1124', 'pricehigh': '1124'}, {'comment': 'No changes.', 'indicator': 'Down', 'name': 'Casein', 'pricelow': '7165', 'priceaverage': '7605', 'pricehigh': '8157'}, {'comment': 'Lactose prices remain unchanged', 'indicator': 'Down', 'name': 'Lactose', 'pricelow': '683', 'priceaverage': '683', 'pricehigh': '683'}, {'comment': 'Export volumes dropped.', 'indicator': 'Up', 'name': 'NFDM', 'pricelow': '2408', 'priceaverage': '2408', 'pricehigh': '2408'}, {'comment': 'actively selling.requested pricing.', 'indicator': 'Down', 'name': 'Caseinate', 'pricelow': '7716', 'priceaverage': '8598', 'pricehigh': '9479'}, {'comment': 'Butter stocks increased', 'indicator': 'Down', 'name': 'Butter', 'pricelow': '3847', 'priceaverage': '3847', 'pricehigh': '3847'}, {'comment': 'WPC 80 \xe2\x80\x93 Supply continues.', 'indicator': 'Down', 'name': 'IWPC80', 'pricelow': '5300', 'priceaverage': '7165', 'pricehigh': '8267'}, {'comment': 'WPC 80 \xe2\x80\x93 Supply continues.', 'indicator': 'Down', 'name': 'IWPC80', 'pricelow': '5300', 'priceaverage': '7165', 'pricehigh': '8267'}]

In [5]: type(products)
Out[5]: dict

In [6]: type(products['Cheese']
   ...: )
Out[6]: dict

EDIT2: Ran建议剪掉评论,输出:

    In [3]: print products
{'Cheese': {'Cheese': {'comment': 'Markets quiet this week', 'indicator': 'Flat', 'pricelow': '3385', 'priceaverage': '3385', 'pricehigh': '3385'}}, 'MPC': {'MPC70': {'comment': 'Large buyers.High Ground.', 'indicator': 'Flat', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}, 'MPI:': {'comment': 'Large buyers.High Ground.', 'indicator': 'Flat', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}, 'MPC85': {'comment': 'Large buyers.High Ground.', 'indicator': 'Flat', 'pricelow': '8598', 'priceaverage': '8929', 'pricehigh': '9039'}}, 'Whey Powder': {'Whey Powder': {'comment': 'Dry Whey futures drop', 'indicator': 'Down', 'pricelow': '1124', 'priceaverage': '1124', 'pricehigh': '1124'}}, 'Casein': {'Casein': {'comment': 'No changes.', 'indicator': 'Down', 'pricelow': '7165', 'priceaverage': '7605', 'pricehigh': '8157'}}, 'Lactose': {'Lactose': {'comment': 'Lactose prices remain unchanged', 'indicator': 'Down', 'pricelow': '683', 'priceaverage': '683', 'pricehigh': '683'}}, 'Powders': {'NFDM': {'comment': 'Export volumes dropped.', 'indicator': 'Up', 'pricelow': '2408', 'priceaverage': '2408', 'pricehigh': '2408'}}, 'Caseinate': {'Caseinate': {'comment': 'actively selling.requested pricing.', 'indicator': 'Down', 'pricelow': '7716', 'priceaverage': '8598', 'pricehigh': '9479'}}, 'Cream': {'Butter': {'comment': 'Butter stocks increased', 'indicator': 'Down', 'pricelow': '3847', 'priceaverage': '3847', 'pricehigh': '3847'}}, 'WPC': {'WPC80': {'comment': 'WPC 80 \xe2\x80\x93 Supply continues.', 'indicator': 'Down', 'pricelow': '5300', 'priceaverage': '7165', 'pricehigh': '8267'}, 'IWPC80': {'comment': 'WPC 80 \xe2\x80\x93 Supply continues.', 'indicator': 'Down', 'pricelow': '5300', 'priceaverage': '7165', 'pricehigh': '8267'}}}

In [4]: %paste
from pprint import pprint; from itertools import chain; pprint([(key, type(val).__name__, id(val)) for key, val in chain.from_iterable(x.items() for x in products.values())])

## -- End pasted text --
[('Cheese', 'dict', 64885352L),
 ('MPC70', 'dict', 64887800L),
 ('MPI:', 'dict', 64887800L),
 ('MPC85', 'dict', 64887800L),
 ('Whey Powder', 'dict', 65061480L),
 ('Casein', 'dict', 65061752L),
 ('Lactose', 'dict', 65062024L),
 ('NFDM', 'dict', 65062296L),
 ('Caseinate', 'dict', 65062568L),
 ('Butter', 'dict', 65063112L),
 ('WPC80', 'dict', 65062840L),
 ('IWPC80', 'dict', 65062840L)]

1 个答案:

答案 0 :(得分:1)

好的,现在问题很明显了。

我复制粘贴的示例行说明:

name

这将通过每个2级词典并打印:

  1. 用于访问它的密钥。
  2. 字典的类型(以确保它实际上是字典)
  3. 字典的唯一标识符。
  4. 因此,鉴于这些结果,有一些重要的事情需要注意:

    name

    →您的某些产品共享相同的字典。了解MPC70,MPI和MPC85如何具有相同的唯一标识符?

    结果,您不是添加三个不同的dicts,每个都添加了for item in products: for subitem in products[item]: product = products[item][subitem].copy() product['name'] = subitem productlist.append(product) 到产品列表中,而是最终添加相同的dict三次,每次循环迭代都会覆盖for item in products.values(): for subkey, subitem in item.items(): product = subitem.copy() product['name'] = subkey productlist.append(product)

    这也解释了为什么复制粘贴改变了这个问题。在复制粘贴时,您可以创建三个不同的词组,它们恰好相同。

    我们现在可以修复您的代码,知道它必须明确地复制产品而不是简单地重复使用它们:

    tell application "Mail"
        set newRule to make new rule at end of rules with properties {name:"test rule", Run applescript:"test.scpt"}
        tell newRule
           make new rule condition at end of rule conditions with properties {rule type:message content, qualifier:does contain value, expression:"test"}
        end tell
    end tell
    

    作为奖励,同样的循环写得更有效率:

      if ($("#GateCodes_GateResponsibleParty").val().split("~")[0] == "Other") { 
        ....  
      }