[英]TypeError: can't multiply sequence by non-int of type 'float' when multiplying an integer with a list of integers
My code:我的代码:
import numpy as np
import sympy as sym
df = [0.99833714, 0.96852 , 0.95101663, 0.92618755]
mat = list(np.arange(1,5)/2)
def task2(df,mat,comp):
"""
inputs:
df - list with discount factors
mat - list with respective maturities
comp - either a positive integer for compounding frequency per year or 0 for continuous compounding
returns: numpy array with spot rates (in decimals p.a.) in the same order as inputs
"""
if comp > 0:
rate = comp*([x for x in df]**(-1/comp*[y for y in mat])-1)
else:
rate = -(1/[y for y in mat])*np.log([x for x in df])
rate.asarray()
return rate
This is the error I get when playing task2(), I don't understand why this yields an error and I couldn't find anything related to it on StackOverflow or elsewhere:这是我在玩 task2() 时遇到的错误,我不明白为什么这会产生错误,而且我在 StackOverflow 或其他地方找不到与它相关的任何内容:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-102-430bc9fc5d7f> in <module>
----> 1 print(task2(df,mat,2))
2 print(task2(df,mat,0))
<ipython-input-100-5b721054deb4> in task2(df, mat, comp)
16
17 if comp > 0:
---> 18 rate = comp*([x for x in df]**(-1/comp*[y for y in mat])-1)
19 else:
20 rate = -(1/[y for y in mat])*np.log([x for x in df])
TypeError: can't multiply sequence by non-int of type 'float'
Can someone tell me, why I get this error, if `comp is surely an integer?有人可以告诉我,如果`comp 肯定是 integer,为什么我会收到此错误?
In the below section of the code, -1/comp
will result in a float unless comp
is 1 or if you are using Python 2.在下面的代码部分中, -1/comp
将导致浮点数,除非comp
为 1 或者您使用的是 Python 2。
(-1/comp*[y for y in mat])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.