简体   繁体   English

Python 错误:TypeError:float() 参数必须是字符串或实数,而不是“列表”

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

Hello I am new to programming and taking online courses at the moment.您好,我目前是编程和在线课程的新手。 I am stuck on this specific exercise:我坚持这个具体的练习:

I have a sample.txt that contains a bunch of text with numbers all throughout the text.我有一个 sample.txt,其中包含一堆文本,整个文本都带有数字。 My goal is to parse the lines for the numbers only and then find the sum.我的目标是仅解析数字的行,然后找到总和。

Here is the sample.txt:这是sample.txt:

Why should you learn to write programs? 7746
12 1929 8827
Writing programs (or programming) is a very creative 
7 and rewarding activity.  You can write programs for 
many reasons, ranging from making your living to solving
8837 a difficult data analysis problem to having fun to helping 128
someone else solve a problem.  This book assumes that 
everyone needs to know how to program ...

Here is my code:这是我的代码:

import re
name = input('Enter File: ')
if len(name) < 1 :
    name = 'sample.txt'
handle = open(name)
numlist = list()
for line in handle :
    line = line.rstrip()
    items = re.findall('[0-9]+', line)
    if len(items) > 0 :
        #print(items)
        num = float(items[0: ])
        numlist.append(num)
print(sum(numlist))

You are trying to convert a list to a float.您正在尝试将列表转换为浮点数。

num = float(items[0:])
# note, the [0:] is redundant, it will just return the whole list

You can either convert the whole list at once using map您可以使用 map 一次转换整个列表

nums = list(map(float, items))

Or you can convert them one at a time like this或者您可以像这样一次转换一个

nums = []
for item in items:
  nums.append(float(item))

I think the better way is to convert the whole list at once using the map function https://www.geeksforgeeks.org/python-map-function/我认为更好的方法是使用 map function https://www.geeksforgeeks.org/python-map一次转换整个列表

暂无
暂无

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

相关问题 Python-TypeError:float()参数必须是字符串或数字,而不是&#39;list - Python - TypeError: float() argument must be a string or a number, not 'list 错误:“TypeError: float() argument must be a string or a number, not 'NoneType'”,但是没有'NoneType'? - Error: "TypeError: float() argument must be a string or a number, not 'NoneType'", but there is no 'NoneType'? 类型错误:float() 参数必须是字符串或数字,而不是“模块” - TypeError: float() argument must be a string or a number, not 'module' TypeError:float() 参数必须是字符串或数字,而不是“PolyCollection” - TypeError: float() argument must be a string or a number, not 'PolyCollection' “TypeError:float参数必须是字符串或数字,而不是列表。”将字符串列表转换为浮点列表 - “TypeError: float argument must be a string or a number, not a list.” converting a list of strings to a list of floats TypeError: int() 参数必须是字符串、类似字节的对象或实数,而不是“列表” - TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list' 类型错误:float() 参数必须是字符串或数字,而不是“datetime.timedelta” - TypeError: float() argument must be a string or a number, not 'datetime.timedelta' 类型错误:必须是实数,而不是列表 - Typeerror: must be real number, not list python 错误:参数必须是字符串或数字,而不是 function - python error: argument must be a string or a number, not function sklearn knn预测错误:float()参数必须是字符串或数字,而不是&#39;dict&#39; - sklearn knn prediction error: float() argument must be a string or a number, not 'dict'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM