简体   繁体   English

numpy.float64对象不是可迭代的...但是我不是想

[英]numpy.float64 object is not iterable…but I'm NOT trying to

I will provide the full code below, but the problem basically is this: I created a data structure like this: means = {ID1 : { HOUR1 : [AVERAGE_FLOW, NUMBER_OF_SAMPLES] ...} 我将在下面提供完整的代码,但是问题基本上是这样的:我创建了一个像这样的数据结构:Means = {ID1:{HOUR1:[AVERAGE_FLOW,NUMBER_OF_SAMPLES] ...}

I created AVERAGE_FLOW using np.mean(). 我使用np.mean()创建了AVERAGE_FLOW。 I can do this: 我可以做这个:

print means['716353'][0][0] #OUT : 76.6818181818 

but when I run the second code when I want to : 但是当我想运行第二个代码时:

means[row['ID']][i][0] 

I get: TypeError: 'numpy.float64' object is not iterable 我得到:TypeError:“ numpy.float64”对象不可迭代

Here are the codes, the first one is where I produce the means data, and the second where I am trying to create a list: 这是代码,第一个是我生成均值数据的地方,第二个是我试图创建列表的地方:

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

I tried str(np.means(temp)) in the first code thinking maybe it is because of numpy, but it actually gave me the first digit of my value! 我在第一个代码中尝试过str(np.means(temp))可能是因为numpy,但实际上它给了我价值的第一位数! as if it is ITERATING through a string...could you please explain what is going on? 好像它正在通过字符串进行迭代...您能解释发生了什么吗? thank you! 谢谢!

It looks like you're trying to extend a list with a scalar float variable. 似乎您正在尝试使用标量float变量扩展列表。 The argument to extend must be an iterable (ie not a float). 扩展的参数必须是可迭代的(即不是浮点数)。 From your first bit of code it looks like means[i][j][k] returns a float, 在您的第一段代码中,它似乎means[i][j][k]返回一个浮点数,

print means['716353'][0][0] #OUT : 76.6818181818

The problem is here, 问题在这里,

temp.extend(means[row['ID']][i][0])

If you expect that means[i][j][k] will always be a single value and not a list you can use append instead of extend. 如果您希望means[i][j][k]始终是单个值而不是列表,则可以使用append而不是extend。

temp.append( means[row['ID']][i][0] )

An example to show the difference, 一个显示差异的例子,

l = [i for i in range(10)]
l.extend( 99.0 )
TypeError: 'float' object is not iterable

this doesn't work b/ca float is not iterable 这是行不通的b / ca浮动是不可迭代的

l.extend( [99.0] )
print l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 99.0]

this works b/ca list is iterable (even a one element list) 这项工作b / ca列表是可迭代的(甚至是一个元素列表)

l.append( 101.0 )
print l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 99.0, 101.0]

append does work with a non-iterable (eg a float) append确实适用于不可迭代(例如float)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM