简体   繁体   中英

“TypeError: can't multiply sequence by non-int of type 'float'” when multiplying two lists of floats

I'm getting a type error that seems pretty simple to solve. I've researched similar errors and tried solutions like converting both float lists into arrays and breaking up the multiplication into two for loops. But to no avail.I would like to multiply each value of r, to both values in each item in of angles. So that xylist would be the product of [(0.0) math.cos(math.radians(0.0))),(0.0) (math.sin(math.radians(0.01)))]

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: can't multiply sequence by non-int of type 'float'

Can anyone help a girl out, maybe some examples of a for loop for iterating over the values of r? I'm having a hard time visualizing what that would look like.

r is a list. You can't multiple a list by a floating-point number. Did you perhaps intend to iterate through those values? Using this addition:

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

we get the output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM