Python 不能将序列乘以“列表”类型的非整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14791119/
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
can't multiply sequence by non-int of type 'list'
提问by user2057593
def evalPolynomial(coeffs,x):
return sum([n for n in coeffs] * [x**(m-1)for m in range(len(coeffs),0,-1)])
TypeError: can't multiply sequence by non-int of type 'list'
类型错误:不能将序列乘以“列表”类型的非整数
Not sure what's causing the error? When I print each of the statements separately, they each give me a list, but when I try to multiply them it doesn't work.
不确定是什么导致了错误?当我分别打印每个语句时,它们每个都给了我一个列表,但是当我尝试将它们相乘时它不起作用。
回答by Matt
You are trying to multiple two lists together. This is not a valid operation in python.
您正在尝试将两个列表放在一起。这在 python 中不是有效的操作。
If you want to multiply each corresponding element in the two lists you can use something like this:
如果要将两个列表中的每个相应元素相乘,可以使用以下方法:
def evalPolynomial(coeffs,x):
return sum(x * y for x, y in zip(coeffs, (x**(m-1)for m in range(len(coeffs),0,-1))))
回答by Bakuriu
Python lists can only be multiplied by an integer, in which case the elements of the listare repeated:
Python lists 只能乘以一个整数,在这种情况下, 的元素list会重复:
>>> [1,2,3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
If you want vectorial operations use numpy.ndarrayinstead:
如果您想要矢量操作,请numpy.ndarray改用:
>>> import numpy as np
>>> ar = np.array([1,2,3])
>>> ar * 3
array([3, 6, 9])
In particular there is a numpy function for convolution(i.e. polynomial multiplication):
特别是卷积有一个 numpy 函数(即多项式乘法):
>>> a = np.array([1,2,3]) # 1 + 2x + 3x^2
>>> b = np.array([4,5,6]) # 4 + 5x + 6x^2
>>> np.convolve(a, b) # (1 + 2x + 3x^2) * (4 + 5x + 6x^2)
array([ 4, 13, 28, 27, 18]) # 4 + 13x + 28x^2 + 27x^3 + 18x^4
If you want to evaluate a polynomial there is the numpy.polyvalfunction which does this.
如果您想评估多项式,则numpy.polyval有执行此操作的函数。
Keep in mind that using numpy limits the size of the integers, so you might obtain wrong results if the coefficients are so big that they overflow.
请记住,使用 numpy 会限制整数的大小,因此如果系数大到溢出,您可能会得到错误的结果。
回答by Dave
The expression [n for n in coeffs]is a list, of integers.
Lists do support multiplication by an integer, but this means "make a list that
is n copies of the starting list"; this is not what you want in this mathematical context.
表达式[n for n in coeffs]是一个list, 整数。
列表确实支持乘以整数,但这意味着“制作一个列表,它是起始列表的 n 个副本”;在这个数学背景下,这不是你想要的。
I would recommend that you look at the numpy(or scipywhich is largely a superset of numpy) package to help with this. It has a function polyvalfor evaluating exactly what you want, and also provides a class based representation polynomial. In general, for doing numeric computation in Python, you should look at these packages.
我建议您查看numpy(或scipy主要是 的超集numpy)包以帮助解决此问题。它具有polyval准确评估您想要的内容的功能,还提供了基于类的表示polynomial。一般来说,要在 Python 中进行数值计算,您应该查看这些包。
But if you want to roll your own, you'll need to do the math inside of the list comprehension, one way to do it is:
但是如果你想自己动手,你需要在列表理解中进行数学计算,一种方法是:
return sum( [ n*x**(i-1) for (n,i) in zip( coeffs, xrange(len(coeffs),0,-1)) ] )

