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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24643385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:55:56  来源:igfitidea点击:

numpy.float64 object is not iterable...but I'm NOT trying to

pythonnumpy

提问by plumSemPy

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(). I can do this:

我使用 np.mean() 创建了 AVERAGE_FLOW。我可以做这个:

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' object is not iterable

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! as if it is ITERATING through a string...could you please explain what is going on? thank you!

我在第一个代码中尝试了 str(np.means(temp)) ,认为可能是因为 numpy,但它实际上给了我价值的第一个数字!好像它是通过一个字符串迭代......你能解释一下发生了什么吗?谢谢你!

采纳答案by Gabriel

It looks like you're trying to extend a list with a scalar float variable. The argument to extend must be an iterable (i.e. 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 而不是扩展。

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/c a float is not iterable

这不起作用 b/ca float 不可迭代

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

this works b/c a 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 (e.g. a float)

append 确实适用于不可迭代的(例如浮点数)