断言(len(内容)== 3)断言错误
assert(len(content) == 3) AssertionError
我遇到了这个错误
cluster.py", line 20, in load_data
distance, num, max_dis, min_dis = load_data(distance_file)
assert(len(content) == 3)
AssertionError
cluster.py
的代码
with open(distance_file, 'r', encoding = 'utf-8') as infile:
for line in infile:
content = line.strip().split(' ')
assert(len(content) == 3)
idx1, idx2, dis = int(content[0]), int(content[1]), float(content[2])
数据样本
1 1 0.000000
1 2 26.232388
1 3 44.486252
1 4 47.168839
1 5 37.593277
另一个文件的样本是
-82.3602 158.46
-91.0108 133.695
-125.815 148.936
-129.259 153.42
你得到一个 AssertionError
,因为断言失败了,它失败了,因为你在 1 space 上拆分,但值被 2 space 分隔。要避免这种情况,请使用不带参数的 split,它将以任意数量的 spaces:
进行拆分
content = line.strip().split()
我遇到了这个错误
cluster.py", line 20, in load_data
distance, num, max_dis, min_dis = load_data(distance_file)
assert(len(content) == 3)
AssertionError
cluster.py
的代码with open(distance_file, 'r', encoding = 'utf-8') as infile:
for line in infile:
content = line.strip().split(' ')
assert(len(content) == 3)
idx1, idx2, dis = int(content[0]), int(content[1]), float(content[2])
数据样本
1 1 0.000000
1 2 26.232388
1 3 44.486252
1 4 47.168839
1 5 37.593277
另一个文件的样本是
-82.3602 158.46
-91.0108 133.695
-125.815 148.936
-129.259 153.42
你得到一个 AssertionError
,因为断言失败了,它失败了,因为你在 1 space 上拆分,但值被 2 space 分隔。要避免这种情况,请使用不带参数的 split,它将以任意数量的 spaces:
content = line.strip().split()