【问题标题】:TypeError: can't multiply sequence by non-int of type 'float'. Python 3TypeError:不能将序列乘以“浮点”类型的非整数。蟒蛇 3
【发布时间】:2015-04-05 11:04:09
【问题描述】:
first = int(list1[0] + list1[1]) 

a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23 = map(float, list1)

if first == 11:
            c = con.execute("SELECT Centroid FROM Centroid WHERE ItemID = 1")
            centroid = c.fetchone()
            AA = (min(a1, a2) * centroid) + (pow(a3,2))
            BB = (min(a1, a2) + a3)
            WA = AA/BB
            print (WA)

以上是我的代码。我现在的问题是我收到一个错误:

TypeError: 不能将序列乘以“float”类型的非整数

我使用的变量 (a1,a2,a3,centriod) 是一种浮点数。

【问题讨论】:

  • map 在 Python 3 中是否返回生成器?这将导致min(<generator>, None) * 1.4,这将解释错误。此外,无论如何,列表推导似乎比map 更受青睐。尝试用[float(x) for x in list1]替换map(...)
  • 您确定类型吗?你检查过了吗?特别检查centroid。当您从 SQL 查询中获取它时,为什么您期望它是 float

标签: python list python-3.x


【解决方案1】:

您需要从元组中获取质心的第一个元素以用作值,因为type(centroid) is tupleTrue。使用:

centroid = c.fetchone()[0]

【讨论】:

    猜你喜欢
    • 2018-12-19
    • 2013-09-11
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 2012-09-17
    相关资源
    最近更新 更多