简体   繁体   English

TypeError:float()参数必须是python的字符串或数字

[英]TypeError: float() argument must be a string or a number for python

I am carrying out some calculation where i am dividing two values. 我正在进行一些除以两个值的计算。 The values i am dividing are two different list. 我正在划分的值是两个不同的列表。 So i know i can't be dividing a list with a list. 所以我知道我不能将列表除以列表。 I am not sure of how i can do this. 我不知道我该怎么做。 Currently my code is as follows : 目前我的代码如下:

def files(currentd, previousd):

    with open(currentd, 'r') as current_data_file,\
            open(previousd, 'r') as pre_data_file:

        # load both data
        data_current = [json.loads(line) for line in current_data_file]
        data_previous = [json.loads(line) for line in pre_data_file]


        # store the previous names for  lookup
        pre_names = set([data["File Name"] for data in data_previous])
        pre_sizes = set([data["File Size"] for data in data_previous])
        cur_sizes = set(data["File Size"] for data in data_current)
        cur_names = set(data["File Name"] for data in data_current)



        # loop through all current data for matching names

        print("Looping through \n")

        for data in data_current:

           if data["File Name"] in pre_names :


               if pre_sizes is None and cur_sizes is None:
                  return "both missing"


               size_ratio = float(cur_sizes) / pre_sizes

You could store data from your files as dictionaries instead of sets and then look for files that are both in previous and current data to add their ratio to dict that we're going to return. 您可以将文件中的数据存储为字典而不是集合,然后查找先前数据和当前数据中的文件,以增加它们的比率以决定我们将要返回的数据。

def files(currentd, previousd):
    # created example data
    data_current = [{'File Name': 'file1', 'File Size': 100}, {'File Name': 'file2', 'File Size': 600}]
    data_previous = [{'File Name': 'file2', 'File Size': 300}]

    # store the previous names for  lookup
    current = {i["File Name"]: i["File Size"] for i in data_current}
    previous = {i["File Name"]: i["File Size"] for i in data_previous}
    ratios = {}

    # loop through all current data for matching names
    for data in current.keys():
        if data in previous.keys():
            ratios[data] = float(current[data]) / previous[data]

    return ratios

print(files(0, 0)) # -> {'file2': 2.0}

暂无
暂无

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

相关问题 类型错误:float() 参数必须是字符串或数字,而不是“列表”python - TypeError: float() argument must be a string or a number, not 'list' python 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' Python-TypeError:float()参数必须是字符串或数字,而不是'list - Python - TypeError: float() argument must be a string or a number, not 'list 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' TypeError:float() 参数必须是字符串或数字,而不是“SingleBlockManager” - TypeError: float() argument must be a string or a number, not 'SingleBlockManager' 类型错误:float() 参数必须是字符串或数字,而不是“Day” - TypeError: float() argument must be a string or a number, not 'Day'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM