简体   繁体   English

TypeError:当将 integer 与整数列表相乘时,不能将序列乘以非整数类型的“浮点”

[英]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.

相关问题 “当两个浮点列表相乘时,TypeError:不能将序列乘以&#39;浮点&#39;类型的非int” - “TypeError: can't multiply sequence by non-int of type 'float'” when multiplying two lists of floats TypeError:不能通过乘法将序列乘以“浮点”类型的非整数 - TypeError: can't multiply sequence by non-int of type 'float' with multiplying 将值乘以两列:TypeError:无法将序列乘以&#39;float&#39;类型的非整数 - Multiplying the values in two columns: TypeError: can't multiply sequence by non-int of type 'float' TypeError:无法将序列乘以float类型的非整数 - TypeError:can't multiply sequence by non-int of type float TypeError:不能将序列乘以“float”类型的非整数? - TypeError: can't multiply sequence by non-int of type 'float'? typeerror:不能将序列乘以“int”类型的非int - typeerror: can't multiply sequence by non-int of type "float 类型错误:不能将序列乘以浮点类型的非整数 - TypeError: Can't multiply sequence by non-int of type float TypeError:无法将序列乘以&#39;float&#39;类型的非整数 - TypeError: can't multiply sequence by non-int of type 'float' TypeError:无法将序列乘以&#39;list&#39;类型的非整数 - TypeError: can't multiply sequence by non-int of type 'list' 将浮点数乘以 Python 中的 Function 并得到不能将序列乘以“浮点”类型的非整数 - Multiplying a float Number to a Function in Python and getting can't multiply sequence by non-int of type 'float'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM