代码如下:
#coding=utf-8
# 从文件中加载数据
def load_numbers(file):
numbers = []
with open(file, 'r') as f:
for line in f.readlines():
numbers.append(int(line))
return numbers
if __name__ == '__main__':
# 从文件 data.txt 中加载数字
numbers = load_numbers('data.txt')
# 区间下限
min = 10
# 区间上限
max = 30
# 统计区间数字个数
cnt = 0
for val in numbers:
# 如果不要包含上下限,去掉 = 号
if val >= min and val <= max:
cnt = cnt + 1
# 将结果保存到文件 result.txt 中
with open('result.txt', 'w') as f:
f.write('[%d,%d], %d' % (min, max, cnt))
print('done.')