我想遍历python中的列表列表。我先打印所有值,然后再打印后续迭代,然后删除最后一个值,例如:
mylists=[["near", "belle", "round", "about"],[" vue"," bus"," stop"],["sammy"],["mombasa","road"]]
在上面的列表中,我打印:
"near belle round about"
"near belle round"
"near belle"
"near"
并继续其他所有列表。
请帮助我以最好的方式做到这一点,我有以下代码并没有给我我想要的东西。
for list in sentence:
while len(list) >0:
print list.pop()
您正在打印来自的退货pop
,但听起来像您想要结束后还剩下什么pop
。试试这个:
for alist in mylists: # Use alist, not list, to avoid shadowing list built-in
while alist: # Faster equivalent to while len(alist) > 0:
print(' '.join(alist)) # Join and print current value
alist.pop() # Remove last, finished when emptied
您的问题标题要求递归执行此操作,但是您的尝试不是递归的,并且您打算递归执行的步骤尚不清楚。该问题根本不需要递归。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句