确定。把头发拉出来。我整天写这样的代码,不能为我的生活看到为什么这样做。也许我需要睡觉。
我的代码:
gsamp = 0 # good samples
zsamp = 0 # zero samples
nsamp = 0 # null samples
rtotal = 0
rtotalltms = 0
pctltms = 0.0
peakh = 0
peakl = 0
for sample in self.rawdata:
if "data" not in sample['data']:
nsamp += 1
continue
if not bool(sample['data']['data']):
nsamp += 1
continue
rtotal += sample['data']['value']
gsamp += 1
# If I print gsamp here it shows it being correctly incremented
for entry in sample['data']['data']:
if int(entry['key']) <= thresh:
rtotalltms += entry['value']
if gsamp == 1:
print "DEBUG gsamp=%d" % gsamp
peakh = int(entry['key'])
peakl = int(entry['key'])
continue
if int(entry['key'] > peakh):
peakh = int(entry['key'])
continue
if int(entry['key'] < peakl):
peakl = int(entry['key'])
continue
我应该只看到调试行打印一次。但由于某些原因,gsamp是本地范围的东西。
当我使用30个样本的数据集运行此代码时,我看到调试行打印了30次,gsamp = 1。
感谢您的帮助。
答案 0 :(得分:0)
问题是,当您循环浏览gsamp
时,您在顶部循环中递增sample.rawdata
,但是您点击了第二个循环sample['data']['data']
。这是打印调试语句的地方。
如果删除其余代码,您可以看到正在进行的操作:
[...]
gsamp += 1
# If I print gsamp here it shows it being correctly incremented
for entry in sample['data']['data']:
if int(entry['key']) <= thresh:
rtotalltms += entry['value']
if gsamp == 1:
print "DEBUG gsamp=%d" % gsamp
[...]