Python-删除列表项

时间:2015-03-16 01:46:06

标签: python list

很抱歉很多问题,这是我的代码到目前为止,我不确定如何删除更大的值。

计算列表中值的平均值,并删除大于给定列表中的平均值的所有值

n = int(input("Number of values in list: "))
numbers = []
average=0
for i in range(n):
    lists = float(input("Enter a floating number: "))
    numbers.append(lists)
    average=average+ lists/n

print (numbers)
if lists>average
value = numbers.pop
谢谢你:)

4 个答案:

答案 0 :(得分:1)

response = raw_input('Enter floats separated by spaces')
float_list = map(float, response.split())

答案 1 :(得分:0)

b= []
c= [4.5,3.4,3.141592654]
def func(a):
        for numbers in a:
            b.append(numbers)
func(c)

不确定这是否是您要找的。

答案 2 :(得分:0)

我想出了如何使用这种方法,感谢您的帮助

n = int(input("Number of values in list: "))
numbers = []
average=0
for i in range(n):
    lists = float(input("Enter an float: "))
    numbers.append(lists)
    average=average+ lists/n
print(numbers)

答案 3 :(得分:0)

根据我的理解,你只需要一个数字列表,然后删除所有大于平均值的数字?你可以过滤不大于平均值的数字。

n = int(input("Number of values in list: "))
numbers = []
average=0
for i in range(n):
    lists = float(input("Enter a floating number: "))
    numbers.append(lists)
    average=average+ lists/n

print (numbers)

# filter numbers not greater than average, and get a new list
numbers = [n for n in numbers if n <= average]
print (numbers)