python完成数据统计

img


python实现随机产生100个[100,999]的整数并写入文件,再从文件中读取全部数据,计算平均值

import random
nums = random.choices(range(100,1000),k=100)
s = ','.join(map(str,nums))
with open(r'C:\u2d.txt','w') as f:
    f.write(s)
with open(r'C:\u2d.txt','r') as f:
    p = f.read().split(',')
avg = sum(map(int,p))/len(p)
print(f'平均值={avg:.2f}')