参数必须是python的字符串或数字

参数必须是python的字符串或数字

我正在进行一些除以两个值的计算。我正在划分的值是两个不同的列表。所以我知道我不能将列表除以列表。我不知道我该怎么做。目前我的代码如下:

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

最佳答案

您可以将文件中的数据存储为字典而不是集合,然后查找先前数据和当前数据中的文件,以增加它们的比率以决定我们将要返回的数据。

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}

关于python - TypeError:float()参数必须是python的字符串或数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53763583/

10-12 16:23