【问题标题】:python dictionary of classes, values disappearancepython类字典,值消失
【发布时间】: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


【解决方案1】:

并非所有对象都保持用零填充。

我把最后一个循环改成

    for cle,val in cliStat.items():
        if val.nb_users:
            print val

并得到六个客户端的输出,全部填充非零数据。

最好先用少量数据测试代码。

【讨论】:

    猜你喜欢
    • 2022-12-11
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多