简体   繁体   English

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

[英]Can't Multiply Sequence By Non-Int of Type Float? Python

Module OneDMaps: 模块OneDMaps:

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

Actual Program: 实际计划:

# 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')  

The program is supposed to call a function from a module OneDMaps.py and then plot it against it's iterates. 该程序应该从模块OneDMaps.py调用一个函数,然后根据它的迭代绘制它。 I get the error "Can't multiply sequence by non-int of type float" and i've tried using LogisticMap(float(a)...) but that didn't work. 我得到错误“不能将类型浮点数的非int乘以序列”并且我尝试使用LogisticMap(float(a)...)但是这不起作用。 Also I want the function name to show up in the title of the plot but i get "at r=0.98 instead of it saying LogisticMap at r= 0.98. 此外,我希望函数名称显示在图表的标题中,但我得到“在r = 0.98,而不是在r = 0.98时说LogisticMap”。

You set up a list like this: 你设置了这样一个list

x = [0.1]

You then multipy it by a float : 然后你用float乘以它:

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

You can't do that. 你不能这样做。 Perhaps you wanted x to be an array instead of a list ? 也许你想让x成为一个array而不是一个list

x = array([0.1])

(That would do the multiplication elementwise) (那会元素乘法)


Note that adding lists concatenates: 请注意,添加列表连接:

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

Multiplication by an integer is the same as concatenating that many times: 乘以整数与连续多次相同:

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

But that makes no sense for floats, 但这对花车毫无意义,

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

(what should you get if you multiply by 3.5 for example?) (例如,如果乘以3.5,你应该得到什么?)

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

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