【发布时间】:2013-02-08 09:06:02
【问题描述】:
我正在尝试将类字典指定为值,将字符串指定为键。 在主循环中打印它们时我有正确的值, 但是一旦我离开主循环,我的“cliStat”变量就会出现刚刚初始化 具有“O”值..
这是我进行分配和打印的代码部分。 我的问题是,为什么 cliStat 仍然是“0”值?
cliStat = {}
for org in client:
cliStat[ org ] = StatEntry( org, "0", "0", "0", "0", "0", "0", "0" )
self.getFilteredStat( client, date_debut, date_fin )
for statEntry in self.tabStatEntry.values():
cliStat[ statEntry.client ].nb_users += statEntry.nb_users
cliStat[ statEntry.client ].nb_pages = ( statEntry.nb_pages + cliStat[ statEntry.client ].nb_pages ) / 2
cliStat[ statEntry.client ].perf_rate = ( statEntry.perf_rate + cliStat[ statEntry.client ].perf_rate ) / 2
cliStat[ statEntry.client ].response_time = ( statEntry.response_time + cliStat[ statEntry.client ].response_time ) / 2
cliStat[ statEntry.client ].nb_errors = ( statEntry.nb_errors + cliStat[ statEntry.client ].nb_errors ) / 2
cliStat[ statEntry.client ].perf_globale = ( statEntry.perf_globale + cliStat[ statEntry.client ].perf_globale ) / 2
cliStat[ statEntry.client ].perf_server = ( statEntry.perf_server + cliStat[ statEntry.client ].perf_server ) / 2
cliStat[ statEntry.client ].perf_network = ( statEntry.perf_network + cliStat[ statEntry.client ].perf_network ) / 2
cliStat[ statEntry.client ].perf_redirect = ( statEntry.perf_redirect + cliStat[ statEntry.client ].perf_redirect ) / 2
for cle,val in cliStat.items():
print val
如果需要,这里是完整的代码; http://dpaste.com/914508/
如果需要运行代码,这里是数据文件http://rapidshare.com/files/2185155450/meteo.7z
只需用您的文件夹更改 logDir 变量即可。
感谢观看!
【问题讨论】:
-
注意:你不需要像
(int)那样用()包围类型来进行转换。只需int(object)。 -
您确定要添加非零值吗?
(0 + 0) / 2 == 0 -
是的,我想过,但即使 nb_users 也保持在 0,并且在主循环中打印它们会给出非零值
-
你能做一个简化的可运行示例吗? dpaste 中的代码依赖于一些数据文件。
-
@Janne Karila:这是一些数据文件:rapidshare.com/files/2185155450/meteo.7z 只需将 logDir 变量更改为新文件夹即可。您将拥有可运行的代码.. 感谢您研究它!
标签: python class dictionary