我目前正在尝试为 python 中的内置 min-max 函数编写等效代码,并且我的代码返回了一个我完全不理解的非常奇怪的异常:
TypeError: 'generator' object is not subscriptable, min, 7, , 9
当我尝试时:
min(abs(i) for i in range(-10, 10))
这是我的代码:
def min(*args, **kwargs):
key = kwargs.get("key", None)
argv=0
for i in args:
argv+=1
if argv == 1 and (type(args) is list or type(args) is tuple or type(args) is str):
min=args[0][0]
for i in args[0]:
if key != None:
if key(i) < key(min):
min = i
else:
if i < min:
min = i
return min
else:
min=args[0]
for i in args:
if key != None:
if key(i) < key(min):
min = i
else:
if i < min:
min = i
return min
根据文档,我应该能够迭代生成器......