目的是输入一个列表x后,如果x中全为数字,则输出大于其平均值的数的平方,如果不全是数字就输出列表中长度大于2的元素
class mf:
def myaver(x):
y =[]
aver = sum(x)/len(x)
for i in x:
if i >= aver:
y.append(i)
x = y
def is_number(x):
try:
float(x)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError,ValueError):
pass
return False
def af(x):
'''
x is a list
'''
flag = True
for i in x:
if mf.is_number(i) == True:
pass
else:
flag = False
if flag == True:
y = list(filter(mf.myaver,x))
z = list(map(y**2,y))
print(z)
else:
print(list(filter(lambda i:len(i)>=2,x)))
aver = sum(x)/len(x)
TypeError: 'int' object is not iterable
class mf:
def myaver(x):
def is_number(x):
你开头定义了类建议加上@staticmethod静态方法装饰器。
然后你 myaver(x)
这个函数传入值 x 是一个 int 类型的,sum()只能接受一个可迭代对象,而 int 不属于可迭代对象,所以你要看看你代码逻辑哪里有问题~