import random
def average():
infile = open("pa8_numbers.py")
global mylist
mylist = []
num = infile.readline()
while num != "":
mylist.append(eval(num))
num = infile.readline()
Sum = 0
for x in mylist:
Sum = x + Sum
global avg
avg = Sum/10000
print("Average: ", end=''), print(format(avg, '.2f'))
这些我不知道,我知道很难从def中获取“ avg”和“ mylist”,但是我真的对面向对象编程不满意。有解决方法吗?
def above(mylist, avg):
acount = 0
abv = avg + 10
for a in mylist:
if a in range(eval(avg, abv)):
count = count + 1
print(acount)
def below(mylist, avg):
bcount = 0
blw = avg - 10
for a in mylist:
if a in range(avg, blw):
count = count + 1
print(bcount)
def main():
outfile = open("pa8_numbers.py","w")
for i in range(10000):
data = random.randint(1,100)
outfile.write(str(data)+"\n")
outfile.close()
print("Statistics")
print("---------------------")
average()
above(mylist, avg)
below(mylist, avg)
main()
目标是列出100个随机数字,将这些数字相加并得出平均值。将随机数列表与avg + 10和avg-10进行比较。
然后计算列表中有多少个随机数落在avg + 10和avg-10类别中。
请帮助,谢谢
答案 0 :(得分:0)
尝试一下
import random
data = random.sample(range(1, 200), 100)
print(data)
avg = sum(data) / len(data)
print(avg)
print([i for i in data if (i >= avg) and (i <= avg + 10)])
print([i for i in data if (i < avg) and (i >= avg - 10)])
样本输出
[8, 54, 13, 60, 156, 152, 128, 89, 28, 142, 197, 37, 109, 133, 44, 45, 57, 194, 106, 95, 39, 85, 82, 27, 25, 64, 102, 143, 99, 61, 43, 118, 182, 141, 117, 15, 78, 6, 164, 132, 86, 19, 77, 186, 97, 119, 154, 63, 48, 122, 14, 53, 7, 83, 90, 136, 163, 76, 148, 196, 190, 150, 4, 66, 180, 165, 38, 130, 32, 94, 170, 140, 36, 96, 176, 62, 167, 179, 172, 127, 79, 17, 16, 139, 116, 34, 30, 9, 40, 195, 111, 115, 146, 80, 193, 58, 51, 125, 93, 98]
96.26
[106, 102, 99, 97, 98]
[89, 95, 90, 94, 96, 93]
答案 1 :(得分:0)
随机导入
def average(): infile = open(“ pa8_numbers.py”)
mylist = []
num = infile.readline()
while num != "":
mylist.append(eval(num))
num = infile.readline()
Sum = 0
for x in mylist:
Sum = x + Sum
avg = Sum/10000
print("Average: ", end=''), print(format(avg, '.2f'))
return avg
def above(): 数量= 0
infile = open("pa8_numbers.py")
mylist = []
num = infile.readline()
while num != "":
mylist.append(eval(num))
num = infile.readline()
Sum = 0
for x in mylist:
Sum = x + Sum
avg = Sum//10000
abv = avg + 10
for a in mylist:
if a in range(avg, abv):
acount += 1
print("Above10: ", acount)
def below(): bcount = 0
infile = open("pa8_numbers.py")
mylist = []
num = infile.readline()
while num != "":
mylist.append(eval(num))
num = infile.readline()
Sum = 0
for x in mylist:
Sum = x + Sum
avg = Sum//10000
blw = avg - 10
for a in mylist:
if a in range(blw, avg):
bcount += 1
print("Below10: ", bcount)
def main():
outfile = open("pa8_numbers.py","w")
for i in range(10000):
data = random.randint(1,100)
outfile.write(str(data)+"\n")
outfile.close()
print("Statistics")
print("---------------------")
average()
above()
below()
main()
我没有尝试访问平均功能中先前定义的avg,而是复制并粘贴了它的代码并将其插入。我知道它的丑陋且不美观。但这有效。
答案 2 :(得分:0)
您可以尝试以下操作:
import numpy as np
num = 25 #Choose the number of random numbers
arr = np.random.uniform(low=0, high=100, size=(num,)) #Numpy array of random numbers
lst = list(arr) #Convert array to list
print lst
>>> [54.5319505485215, 4.245083725270494, 78.678755233104, 13.350267097985968, 0.053831517325253486, 41.35195665558887, 32.247355069096336, 49.18231116527674, 94.7134275426869, 82.70659951321971, 69.20538155428206, 47.5556647970394, 70.51752770999894, 34.769182788859574, 35.32276275010964, 4.087762606868672, 83.63591325576411, 64.02551476912747, 33.82182179701143, 79.41086468884642, 83.315665673576, 38.115224599910604, 21.318686272968367, 37.495943736007376, 19.8111480759137]
arr_mean = np.mean(arr) #Mean of the array/list of random numbers
print arr_mean
>>> 46.93882412577438
''' Array (arr_new) of random numbers with (arr_mean-10, arr_mean+10) '''
arr_new = arr[ (np.where( (arr<arr_mean+10) & (arr>arr_mean-10) )[0] ) ]
print arr_new
>>> [54.53195055 41.35195666 49.18231117 47.5556648 38.1152246 37.49594374]
''' Print mean and length of new array. '''
print "Length of new array is: ", len(arr_new)
print "Mean of new array is: ", np.mean(arr_new)
>>> Length of new array is: 6
>>> Mean of new array is: 44.70550858372409