不能通过Non-Int类型浮点乘以序列?蟒蛇

时间:2012-12-09 03:59:16

标签: python plot

模块OneDMaps:

def LogisticMap(a,nIts,x):
    for n in xrange(0,nIts):
        return 4.*a*x*(1.-x)

实际计划:

# Import plotting routines
from pylab import *
import OneDMaps 

def OneDMap(a,N,x,f):
    return x.append(f(a,N,x))

# Simulation parameters
# Control parameter of the map: A period-3 cycle
a = 0.98
# Set up an array of iterates and set the initital condition
x = [0.1]
# The number of iterations to generate
N = 100

#function name in OneDMaps module
func = LogisticMap

# Setup the plot
xlabel('Time step n') # set x-axis label
ylabel('x(n)') # set y-axis label
title(str(func) + ' at r= ' + str(a)) # set plot title

# Plot the time series: once with circles, once with lines
plot(OneDMap(a,N,x,func), 'ro', OneDMap(a,N,x,func) , 'b')  

该程序应该从模块OneDMaps.py调用一个函数,然后根据它的迭代绘制它。我得到错误“不能将类型浮点数的非int乘以序列”并且我尝试使用LogisticMap(float(a)...)但是这不起作用。此外,我希望函数名称显示在图表的标题中,但我得到“在r = 0.98,而不是在r = 0.98时说LogisticMap。

1 个答案:

答案 0 :(得分:4)

您设置了list,如下所示:

x = [0.1]

然后将其乘以float

return 4.*a*x*(1.-x)

你做不到。也许您希望x成为array而不是list

x = array([0.1])

(那将按元素进行乘法)


请注意,添加列表连接:

[0] + [1] == [0, 1]

乘以整数与连接多次相同:

[0] * 3 == [0, 0, 0]

但这对花车毫无意义,

[0] * 3.0 #raises TypeError as you've seen

(例如,如果乘以3.5,你会得到什么?)