Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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_Dictionary - Fatal编程技术网

Python 词典中的词典

Python 词典中的词典,python,dictionary,Python,Dictionary,我试图在字典中创建一个字典,但字典总是以相同的细节结束 代码如下: Class DailyStatement(object): def __init__(self): self.masterdict={} self.subdict={} self.list_of_subdicts=['yesterday','today'] self.keys=['high','low','open','close'] self._create_master

我试图在字典中创建一个字典,但字典总是以相同的细节结束

代码如下:

Class DailyStatement(object):
  def __init__(self):
     self.masterdict={}
     self.subdict={}
     self.list_of_subdicts=['yesterday','today'] 
     self.keys=['high','low','open','close']
     self._create_masterdict()

  def _create_masterdict(self):
     for subdict in self.list_of_subdicts:
       self.masterdict[subdict]={}
       self.masterdict[subdict]=self.create_subdict(subdict)
       print self.masterdict[subdict]

  def create_subdict(self,subdict):
     for item in self.keys:
       subdict[item]=self.get_value(item,subdict)
     return self.subdict

  def get_value(self,item,subdict):
     {code to find the value}
     return value
当我跑步时:

ds=DailyStatement()
print ds.masterdict
它打印得很好,似乎工作得很好,在控制台上显示:

 {'high':2,'low':1,'open':1.5,'close':1.6}
 {'high':2.2,'low':2.1,'open':2.5,'close':2.6}
但当我跑步时:

ds=DailyStatement()
print ds.masterdict
我得到:

{'today':{'high':2,'low':1,'open':1.5,'close':1.6},
 'yesterday':{'high':2,'low':1,'open':1.5,'close':1.6}}
我不明白这是为什么。在初始化类时,似乎可以很好地构建masterdict,但是在代码运行之后,字典是否会自己重写呢

在过去的几个小时里,我一直在研究这个问题,似乎无法解决它

非常感谢您的帮助。

您正在为这两个子词典使用self.subct。您应该在每次需要子字典时创建一个新的子字典:

import random

class DailyStatement(object):  # changed from C to c
  def __init__(self):  # Added self
     self.masterdict={}
     # Removed self.subdict
     self.list_of_subdicts=['yesterday', 'today'] 
     self.keys=['high', 'low', 'open', 'close']
     self._create_masterdict()

  def _create_masterdict(self):
     for subdict in self.list_of_subdicts:
       self.masterdict[subdict] = {}
       self.masterdict[subdict] = self.create_subdict(subdict)  # added self
       print self.masterdict[subdict]

  def create_subdict(self, subdict):
     sub_dict = {}
     for item in self.keys:
        sub_dict[item] = self.get_value(item, subdict)  # added self
     return sub_dict  

  def get_value(self, item, subdict):  # added self
     return random.randrange(1,4)

if __name__ == "__main__":
  ds=DailyStatement()
  print ds.masterdict
我已经对其他的变化发表了评论

以下是python repl中的一个简单示例:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> y = {}
>>> x = y
>>> y["NOEL"] = "ROCKS"
>>> x
{'NOEL': 'ROCKS'}
>>>

正如您所看到的,x从未直接设置过,但仍然包含添加到y的键和值。

请更加小心地发布您的代码。你的uuu init uuuuuuuuuuu方法的名称有一个输入错误,你在调用u create u masterdict时错过了自我。还有,这些dict是从哪里来的?昨天,今天,高点,低点,开盘和收盘都没有定义。很抱歉。我已经改正了这些错误。