现在我知道这个问题听起来很奇怪,但我发现自己的处境可能需要避免在列表上循环,除非我设法找出问题的根源
问题是:
def SomeFunction(args):
someList = [here, are, list, elements]
print (someList) # works normal
for elem in someList:
print (elem) # side effects observed here (and not sure why at this stage), only the first element is printed
有没有其他方法可以像for循环那样访问列表元素
通常,这可以使用递归实现
下面是我使用递归处理列表的算法:-
函数进程\列表(将列表的第一个元素作为参数)
如果列表已结束: 返回(结束函数执行)
处理当前元素
调用函数process_list(将其传递给列表中的下一个元素)
您可以使用while循环迭代列表:
可以使用递归或切片
相关问题 更多 >
编程相关推荐