【发布时间】:2026-01-26 04:45:02
【问题描述】:
我正在执行一些计算,我将两个值相除。我划分的值是两个不同的列表。所以我知道我不能将列表与列表分开。我不确定我该怎么做。目前我的代码如下:
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
【问题讨论】:
-
将您的列表转换为 numpy 数组,然后您可以简单地将两者一分为二,除法将在元素方面进行。准确地说,
size_ratio = np.array([float(i) for i in cur_sizes]) / np.array(pre_sizes) -
@Bazingaa 然后我得到错误:如果 size_ratio >= 1 + tolerance: ValueError: 具有多个元素的数组的真值是不明确的。使用 a.any() 或 a.all()
-
你的尺寸比例是多少?它必须是一个标量。您如何期望从两个列表的除法中得到一个数字?由于您要划分两个列表(数组),因此您的大小比率也是一个数组,因此当您检查大小比率 >=1+tolerance 时,您检查的不是单个数字,而是一个完整的数组。为了比较一个完整的数组,您必须使用 a.any() 或 a.all() 其中
a是您的数组名称 -
@Bazingaa 我的尺寸比例是:如果 size_ratio >= 1 + 容差:返回 "not ok %d%% large" % round(((size_ratio - 1) * 100), 0)跨度>
-
再仔细阅读我的评论。
标签: python json python-2.7 list file