我将提供下面的完整代码,但问题基本上是这样的:我创建了一个像这样的数据结构:means = {ID1:{ HOUR1:[AVERAGE_FLOW,NUMBER_OF_SAMPLES] ...}numpy.float64对象是不可迭代的...但我不想
我使用np.mean()创建了AVERAGE_FLOW。我可以这样做:
print means['716353'][0][0] #OUT : 76.6818181818
但是当我运行第二个代码时,我想:
means[row['ID']][i][0]
我得到:类型错误:“numpy.float64”对象不是可迭代
这里是代码,第一个是我生成平均数据的位置,第二个是我尝试创建列表的地方:
shunned=[]
means={} #{ #DAY: [mean, number of samples]}
hour={}
for i in range(24):
hour[i]=[]
for station in stations:
means[station]=copy.deepcopy(hour)
for station in d:
for hour in range(24):
temp=[]
for day in range(1,31):
if day in sb: #swtich between sa for all days and sb for business days
try: #no entry = no counting in the mean, list index out of range, the station has not hourly data to begin with
e = d[station][str(day)][hour][0]
if not e: # sometimes we have '' for flow which, should not be counted
next
else:
temp.append(int(e))
except IndexError:
if station not in shunned:
shunned.append([station,d[station]])
else:
next
temp=np.array(temp)
means[station][hour]=[np.mean(temp),len(temp)]
pprint.pprint(means)
print means['716353'][0][0] #OUT : 76.6818181818
headers=['ID' , 'Lat', 'Lng', 'Link ID']+range(24)
csv_list=[]
meta_f.seek(0)
i=0
for row in meta_read:
if i>100:
break
temp=[]
if row['ID'] in stations:
temp.append([row['ID'],row['Latitude'],row['Longitude'],' '])
for i in range(24):
temp.extend(means[row['ID']][i][0])
csv_list.append(temp)
i+=1
pprint.pprint(csv_list) #OUT:temp.extend(means[row['ID']][i][0]) TypeError: 'numpy.float64' object is not iterable
我在第一个代码中尝试了str(np.means(temp)),可能是因为numpy,但它实际上给了我价值的第一位!就好像它是通过字符串ITERATING一样......你能解释一下发生了什么吗?谢谢!