" TypeError:不能将序列乘以非int类型' float'"乘以两个浮点列表时

时间:2016-03-15 23:41:42

标签: python list floating-point typeerror

我得到的类型错误似乎很容易解决。我研究了类似的错误,并尝试了将浮动列表转换为数组并将乘法转换为两个for循环的解决方案。但无济于事。我想将r的每个值乘以角度中每个项目的两个值。所以 xylist将是[(0.0) math.cos(math.radians(0.0))),(0.0)(math.sin(math.radians(0.01)))]的产物。 p>

import math
import numpy as np
r = [0.0,0.01,0.0,0.35,0.98,0.001,0.0]
angles = [(0.0,0.01),(0.0,0.35),(0.98,0.001),(0.0,0.0),(0.01,0.0),(0.35,0.98),(0.001,0.0)]
angles = np.asarray(angles)
xylist = []
for i in angles:
    for x in i:
        meq = [r*(math.cos(math.radians(x))), r*(math.sin(math.radians(x)))]
        xylist.append(meq)
print xylist

TypeError:不能将序列乘以非int类型' float'

任何人都可以帮助一个女孩,也许是一些for循环的例子来迭代r的值?我很难想象看起来会是什么样子。

1 个答案:

答案 0 :(得分:0)

r 是一个列表。您不能通过浮点数复制列表。您是否打算迭代这些值?使用此添加:

    cosx = math.cos(math.radians(x))
    sinx = math.sin(math.radians(x))
    meq = [(rn*cosx, rn*sinx) for rn in r]

我们得到输出:

[[(0.0, 0.0), (0.01, 0.0), (0.0, 0.0), (0.35, 0.0), (0.98, 0.0), (0.001, 0.0), (0.0, 0.0)], [(0.0, 0.0), (0.009999999847691291, 1.7453292431333682e-06), (0.0, 0.0), (0.3499999946691952, 6.108652350966788e-05), (0.9799999850737465, 0.00017104226582707007), (0.0009999999847691292, 1.7453292431333682e-07), (0.0, 0.0)], [(0.0, 0.0), (0.01, 0.0), (0.0, 0.0), (0.35, 0.0), (0.98, 0.0), (0.001, 0.0), (0.0, 0.0)], [(0.0, 0.0), (0.009999813422410572, 6.108614390678362e-05), (0.0, 0.0), (0.34999346978436996, 0.002138015036737426), (0.9799817153962359, 0.005986442102864794), (0.0009999813422410572, 6.108614390678362e-06), (0.0, 0.0)], [(0.0, 0.0), (0.009998537262811576, 0.00017103392695130699), (0.0, 0.0), (0.3499488041984052, 0.005986187443295744), (0.9798566517555345, 0.016761324841228085), (0.0009998537262811576, 1.7103392695130698e-05), (0.0, 0.0)], [(0.0, 0.0), (0.009999999998476913, 1.7453292519057202e-07), (0.0, 0.0), (0.34999999994669195, 6.1086523816700204e-06), (0.9799999998507375, 1.7104226668676058e-05), (0.0009999999998476913, 1.7453292519057202e-08), (0.0, 0.0)], [(0.0, 0.0), (0.01, 0.0), (0.0, 0.0), (0.35, 0.0), (0.98, 0.0), (0.001, 0.0), (0.0, 0.0)], [(0.0, 0.0), (0.01, 0.0), (0.0, 0.0), (0.35, 0.0), (0.98, 0.0), (0.001, 0.0), (0.0, 0.0)], [(0.0, 0.0), (0.009999999847691291, 1.7453292431333682e-06), (0.0, 0.0), (0.3499999946691952, 6.108652350966788e-05), (0.9799999850737465, 0.00017104226582707007), (0.0009999999847691292, 1.7453292431333682e-07), (0.0, 0.0)], [(0.0, 0.0), (0.01, 0.0), (0.0, 0.0), (0.35, 0.0), (0.98, 0.0), (0.001, 0.0), (0.0, 0.0)], [(0.0, 0.0), (0.009999813422410572, 6.108614390678362e-05), (0.0, 0.0), (0.34999346978436996, 0.002138015036737426), (0.9799817153962359, 0.005986442102864794), (0.0009999813422410572, 6.108614390678362e-06), (0.0, 0.0)], [(0.0, 0.0), (0.009998537262811576, 0.00017103392695130699), (0.0, 0.0), (0.3499488041984052, 0.005986187443295744), (0.9798566517555345, 0.016761324841228085), (0.0009998537262811576, 1.7103392695130698e-05), (0.0, 0.0)], [(0.0, 0.0), (0.009999999998476913, 1.7453292519057202e-07), (0.0, 0.0), (0.34999999994669195, 6.1086523816700204e-06), (0.9799999998507375, 1.7104226668676058e-05), (0.0009999999998476913, 1.7453292519057202e-08), (0.0, 0.0)], [(0.0, 0.0), (0.01, 0.0), (0.0, 0.0), (0.35, 0.0), (0.98, 0.0), (0.001, 0.0), (0.0, 0.0)]]