平均值-Python

平均值-Python,python,list,file,average,Python,List,File,Average,我正在尝试查找范围内数字的平均值(即查找范围1-1000内所有数字的平均值)。我编写了以下代码来实现这一点,但是由于if语句,在运行时,代码会生成多个数字。然后我尝试了一个while循环,但是当我输入一个break语句时,它产生了与所产生的if语句相同的list。有没有办法使用if语句得到一个平均数?谢谢大家! mylist =[] def ave_data(x, y): for line in filename: for number in line.split

我正在尝试查找范围内数字的平均值(即查找范围1-1000内所有数字的平均值)。我编写了以下代码来实现这一点,但是由于
if语句
,在运行时,代码会生成多个数字。然后我尝试了一个
while循环
,但是当我输入一个
break
语句时,它产生了与所产生的
if语句相同的
list
。有没有办法使用
if语句
得到一个平均数?谢谢大家!

mylist =[]
def ave_data(x, y):
    for line in filename:    
        for number in line.split():
            if int(number) > x and int(number) < y:
                mylist.append(int(number)) 
                print(sum(mylist)/len(mylist))
mylist=[]
def ave_数据(x,y):
对于文件名中的行:
对于第.split()行中的数字:
如果int(number)>x且int(number)
这不是由于if语句造成的。您只需将
打印(..)
放入
if
for
循环中的
。通过将其移出外部
,它将打印整个文件的平均

def ave_data(x, y):
    mylist = []  # move inside
    for line in filename:    
        for number in line.split():
            if int(number) > x and int(number) < y:
                mylist.append(int(number)) 
    print(sum(mylist)/len(mylist))  # for the entire file.
这不是由于if语句造成的。您只需将
打印(..)
放入
if
for
循环中的
。通过将其移出外部
,它将打印整个文件的平均

def ave_data(x, y):
    mylist = []  # move inside
    for line in filename:    
        for number in line.split():
            if int(number) > x and int(number) < y:
                mylist.append(int(number)) 
    print(sum(mylist)/len(mylist))  # for the entire file.

您还可以使用
列表压缩
而不是
进行循环
,如下所示:

mylist = [int(num) for line in filename for num in line.split() if y > int(num) > x]
print sum(mylist) / len(mylist)
您还可以添加一个条件来检查
列表是否为空:

print sum(mylist) / len(mylist) if mylist else 'No numbers in that range'

您还可以使用
列表压缩
而不是
进行循环
,如下所示:

mylist = [int(num) for line in filename for num in line.split() if y > int(num) > x]
print sum(mylist) / len(mylist)
您还可以添加一个条件来检查
列表是否为空:

print sum(mylist) / len(mylist) if mylist else 'No numbers in that range'

您看到多个数字,因为您的打印语句在for循环中。看看这个伪代码:

create an empty list
open the file
for each line in the file
    for each number in the line
        if the number is within the range
            add the number to the list
            compute the average of the list of numbers
你看,每次你找到一个在要求范围内的数字时,你都在计算平均值。相反,您要做的是首先收集所有数字(在for循环内),然后(在for循环外)计算平均值:

create an empty list
open the file
for each line in the file
    for each number in the line
        if the number is within the range
            add the number to the list
compute the average of the list of numbers
这是通过以下代码实现的:

def ave_data(x, y):
    nums = []
    for line in filename:    
        for number in line.split():
            if int(number) > x and int(number) < y:
                nums.append(int(number)) 
    print(sum(mylist)/len(mylist))
def ave_数据(x,y):
nums=[]
对于文件名中的行:
对于第.split()行中的数字:
如果int(number)>x且int(number)
这里有一个更好的方法来做同样的事情:

def ave_data(x,y):
    total = 0
    nums = 0
    with open('path/to/file') as infile:
        for line in infile:
            for num in line.split():
                num = int(num)
                if not (x < num < y): continue
                total += num
                nums += 1

    if not nums:
        print("There were no numbers within the required range")
        return

    print("The average is", total/nums)
def ave_数据(x,y):
总数=0
nums=0
以open('path/to/file')作为填充:
对于填充中的线:
对于第行中的num.split():
num=int(num)
如果不是(x
您看到多个数字,因为您的打印语句在for循环中。看看这个伪代码:

create an empty list
open the file
for each line in the file
    for each number in the line
        if the number is within the range
            add the number to the list
            compute the average of the list of numbers
你看,每次你找到一个在要求范围内的数字时,你都在计算平均值。相反,您要做的是首先收集所有数字(在for循环内),然后(在for循环外)计算平均值:

create an empty list
open the file
for each line in the file
    for each number in the line
        if the number is within the range
            add the number to the list
compute the average of the list of numbers
这是通过以下代码实现的:

def ave_data(x, y):
    nums = []
    for line in filename:    
        for number in line.split():
            if int(number) > x and int(number) < y:
                nums.append(int(number)) 
    print(sum(mylist)/len(mylist))
def ave_数据(x,y):
nums=[]
对于文件名中的行:
对于第.split()行中的数字:
如果int(number)>x且int(number)
这里有一个更好的方法来做同样的事情:

def ave_data(x,y):
    total = 0
    nums = 0
    with open('path/to/file') as infile:
        for line in infile:
            for num in line.split():
                num = int(num)
                if not (x < num < y): continue
                total += num
                nums += 1

    if not nums:
        print("There were no numbers within the required range")
        return

    print("The average is", total/nums)
def ave_数据(x,y):
总数=0
nums=0
以open('path/to/file')作为填充:
对于填充中的线:
对于第行中的num.split():
num=int(num)
如果不是(x
只需缩进打印3个LVL,您的
print
语句就在循环中,因此每次都会通过。只需将其移出for循环,以便在填充
mylist
后完成一次。可能重复:@Ev.Kounis谢谢您的帮助@谢谢你的帮助!只需将print 3 LVL缩进,您的
print
语句就在循环中,因此每次都会通过。只需将其移出for循环,以便在填充
mylist
后完成一次。可能重复:@Ev.Kounis谢谢您的帮助@谢谢你的帮助!哇,我真不敢相信我错过了。非常感谢。哇,我真不敢相信我错过了。非常感谢。谢谢你的帮助!很高兴帮助:-)谢谢你的帮助!很乐意帮忙:-)