简体   繁体   English

类型错误:float() 参数必须是字符串或数字,而不是“列表”python

[英]TypeError: float() argument must be a string or a number, not 'list' python

I have a problem with Python.我对 Python 有问题。 This is my code:这是我的代码:

def calcola():
            a = input()
            b = float(a[0].split("*"))
            c = float(a[0].split("/")) 
            d = float(a[0].split("-"))
            e = float(a[0].split("+"))
            j = float(a[1].split("*"))
            k = float(a[1].split("/")) 
            l = float(a[1].split("-")) 
            m = float(a[1].split("+")) 
            b = b[0]
            c = b[1]
            d = c[0]
            e = c[1]
            f = d[0]
            g = d[1]
            h = e[0]
            i = e[1]
            somma1 = b+c
            somma2 = d+e
            somma3 = f+g
            somma4 = h+i
            print(somma1)
            print(somma2)
            print(somma3)
            print(somma4)


calcola()

I've recieved some errors:我收到了一些错误:

Traceback (most recent call last): File "file.py", line 29, in calcola() File "file.py", line 3, in calcola b = float(a[0].split("*")) TypeError: float() argument must be a string or a number, not 'list'回溯(最近一次调用):文件“file.py”,第 29 行,在 calcola() 文件“file.py”,第 3 行,在 calcola b = float(a[0].split("*"))类型错误:float() 参数必须是字符串或数字,而不是“列表”

How can I transform the number in the list?如何转换列表中的数字?

You can't call float on a list directly.您不能直接在列表上调用float You can use map to call float on each item in the list.您可以使用map对列表中的每个项目调用float Like so:像这样:

b = map(float, a[0].split("*"))

In python 3.x在 python 3.x 中

b = list(map(float, a[0].split("*")))

Or for more readability, use a list comprehension.或者为了提高可读性,请使用列表理解。 Works for both python2 and python3:适用于 python2 和 python3:

b = [float(s) for s in a[0].split("*")]

But be sure the items after splitting are floatable但要确保拆分后的项目是可浮动的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-TypeError:float()参数必须是字符串或数字,而不是'list - Python - TypeError: float() argument must be a string or a number, not 'list 类型错误:float() 参数必须是字符串或数字,而不是“列表” - TypeError: float() argument must be a string or a number, not 'list' TypeError:float()参数必须是python的字符串或数字 - TypeError: float() argument must be a string or a number for python TypeError:float()参数必须是字符串或具有列表总和的数字 - TypeError: float() argument must be a string or a number with list sum Python/Pandas:类型错误:float() 参数必须是字符串或数字,而不是“函数” - Python/Pandas: TypeError: float() argument must be a string or a number, not 'function' TypeError:float()参数必须是字符串或数字,而不是'function' - Python / Sklearn - TypeError: float() argument must be a string or a number, not 'function' – Python/Sklearn Python TypeError:float()参数必须是字符串或数字,而不是“ SingleBlockManager” - Python TypeError: float() argument must be a string or a number, not 'SingleBlockManager' Pandas:TypeError:float() 参数必须是字符串或数字 - Pandas : TypeError: float() argument must be a string or a number 类型错误:float() 参数必须是字符串或数字,而不是“时间戳” - TypeError: float() argument must be a string or a number, not 'Timestamp'' TypeError:float() 参数必须是字符串或数字,而不是“NAType” - TypeError: float() argument must be a string or a number, not 'NAType'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM