如何获取固定的python代码以删除ValueError

我正在学习嵌套字典。以下是我拥有的python代码。我的任务是找到每个特定区域中的所有支出,然后找到该区域中支出的最小和最大值,然后计算每个箱中有多少数据点。对于bin,(n_bins)是随机数,但是bin的宽度是通过[max-min] // n_bins计算的。

from header import read_data, VALID_AREAS
from collections import defaultdict 
from collections import Counter

def funding_dist(data, n_bins, area):
    #expenses = []

    for k,v in data.items():
        area = v["area"]
        expend = v["expenditure"]
        storage = defaultdict(list)

        if (area is not None) and (expend is not None):
            if (area in VALID_AREAS):
                {storage[d['area']].append(d['expenditure']) for _, d in data.items() if d['area']}

        results = dict(storage)      

        # max/min expenditure in that area

        for k1,v1 in results.items():
            lists = []
            if v1 in results.values():
                for val in v1:
                    lists.append(val)
                for i in range(0,len(lists)):
                    lists[i] = int(lists[i])

            max_expend = max(lists)
            min_expend = min(lists)
            bin_width = (max_expend - min_expend)//n_bins

            bin_values = Counter()
            for i in range(0, len(lists)):
                bin_values[int(((int(lists[i]) - min_expend-1) / (max_expend - min_expend))*n_bins)] +=1

            bin_values = list(bin_values)
            return bin_values


# to test your function with another CSV file,
# change the value of this variable
test_file = 'cleaned_data.csv'

# you don't need to modify the code below
if __name__ == '__main__':
    data_cleaned = read_data(test_file)

在行list [i] = int(lists [i])处存在ValueError。我怎样才能解决这个问题? 另外,表示每个仓中有多少个数据点的数字不在实际计数范围之内。如何修复代码?

评论