我只是在学习python,并试图使用for
循环定义函数。代码如下-
def chk(hilist):
``` The function returns the output of the enumerate function as (x1,y1) (x2,y2)...
```
for item in enumerate(hilist):
return item
我为输入“字符串”运行上述功能,如下所示:
abc = chk('string')
abc
输出结果为(0,s)。
如果我运行常规for
函数,则输出将如下所示-(0,'s')(1,'t')(2,'r')(3,'i')(4,'n')( 5,“ g”)
有人可以帮我了解我在做什么错吗?提前致谢。
Return将立即中断功能。因此,您必须将结果保存在列表中并返回:
def chk(hilist):
""" The function returns the output of the enumerate function as (x1,y1) (x2,y2)...
"""
ret_list=list()
for item in enumerate(hilist):
ret_list.append(item)
return ret_list
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句