|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
下面的代码是处理了一个文件,
有没有哪位大佬能在下面的代码中找个函数定义并运用一下,
任何函数都行,急需,有点不太会定义函数,
大家路过的请帮下小弟,万分感激🙏
import numpy as np
import csv
filename = 'iris.data'
with open(filename, 'r') as csvfile:
dataSet = list(csv.reader(csvfile))
num = 1
for line in dataSet:
print(num, line)
num += 1
sum = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
for i in range(3):
for j in range(50):
for k in range(4):
sum[i][k] += float(dataSet[50*i+j][k])
average = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
for i in range(3):
for j in range(4):
average[i][j] = np.around(sum[i][j]/50, 2)
print('Iris plant: sep len, sep wid, pet len, pet wid')
for i in range(3):
print(dataSet[i*50][4]+', 50 samples: ', end='')
for j in range(4):
if j == 3:
print(str(average[i][j]))
else:
print(str(average[i][j])+', ', end='')
本帖最后由 Twilight6 于 2020-6-6 13:40 编辑
import numpy as np
import csv
def get_average(filename):
with open(filename, 'r') as csvfile:
dataSet = list(csv.reader(csvfile))
num = 1
for line in dataSet:
print(num, line)
num += 1
sum = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
for i in range(3):
for j in range(50):
for k in range(4):
sum[i][k] += float(dataSet[50*i+j][k])
average = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
for i in range(3):
for j in range(4):
average[i][j] = np.around(sum[i][j]/50, 2)
print('Iris plant: sep len, sep wid, pet len, pet wid')
for i in range(3):
print(dataSet[i*50][4]+', 50 samples: ', end='')
for j in range(4):
if j == 3:
print(str(average[i][j]))
else:
print(str(average[i][j])+', ', end='')
filename = 'iris.data'
get_average(filename)
|
|