【发布时间】:2018-11-19 13:51:43
【问题描述】:
我需要编写执行以下操作的 Python 方法的主体:
1) 获取一个列表,其中 list[0] 是一个字符串,而 list[1] 是一个看起来相同的列表或 None
2) 打印列表中的每一个字符串
我必须使用 while 循环,而不是使用列表推导或展平。
def pick_cherries_iter(field):
""""e.g.
>>> cherry_field = ['cherry1', ['cherry2', ['cherry3', ['cherry4', ['Yay!!!', None]]]]]
>>> pick_cherries_iter(cherry_field)
cherry1
cherry2
cherry3
cherry4
Yay!!!"""
_______________________
_______________________
_______________________
_______________________
while _________________:
_______________________
_______________________
_______________________
我知道,对于上面的示例,如果我为cherry_field[1][0] 打印cherry_field[0] 或为cherry_field[1][0] 打印cherry1 或为cherry_filed[1][1][0] 打印cherry2 等,我可以打印cheery1,但我不是确定如何使用 while 循环遍历这些元素。
【问题讨论】:
-
这是给你的作业吗?
-
是的,这是一个作业。
-
不是,我不应该用flatten。
-
在分配中是否保证每个列表最多包含 2 个元素?