我有一个脚本,它接受文件中的列并将它们放在一个列表中,找到该列表中的最小值和最大值并减去它们,这是执行此操作的代码的一部分:
for line in f:
new_n = float(line)
temp.append(new_n)
min_ = min(temp)
max_ = max(temp)
tot = (max_ - min_)
某些文件包含全0的列表,我收到错误:ValueError: min() arg is an empty sequence
如何处理这些空集,如果整个列表为0则返回0。
我猜是这样的:
for num in temp:
if num == 0:
tot = 0
else:
min_ = min(temp)
max_ = max(temp)
tot = (max_ - min_)
答案 0 :(得分:0)
全零的文件没有给出错误...它的文件完全没有任何数据令人不安。
发生此错误
ValueError: min() arg is an empty sequence
当min func的参数是空列表(或元组)时。 <{1}}执行后,您的临时列表为空。