【问题标题】:"TypeError: can't multiply sequence by non-int of type 'float'" when multiplying two lists of floats将两个浮点数列表相乘时,“TypeError:不能将序列乘以‘float’类型的非整数”
【发布时间】:2016-03-16 07:55:02
【问题描述】:

我遇到了一个似乎很容易解决的类型错误。我研究了类似的错误并尝试了解决方案,例如将两个浮点列表转换为数组并将乘法分解为两个 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: 不能将序列乘以“float”类型的非整数

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

【问题讨论】:

  • r 是一个列表,您正在尝试将一个列表乘以一个浮点数。您可能想要遍历 r(使用第三个循环)
  • 这里的逻辑似乎很混乱。错误是您试图将list 乘以float,但尚不清楚您要做什么,因此很难修复。此外,您可能希望使用 numpy 的一些功能对 np.array 类型的对象执行操作。

标签: python list floating-point typeerror


【解决方案1】:

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)]]

【讨论】:

    猜你喜欢
    • 2017-12-09
    • 1970-01-01
    • 2010-12-30
    • 2012-10-09
    • 2018-12-19
    • 2021-08-07
    • 1970-01-01
    • 2013-09-11
    • 2015-06-27
    相关资源
    最近更新 更多