python 如何用循环遍历多重列表

2024-11-29 11:45:50
推荐回答(2个)
回答1:

>>> def my_print(s):
....... if type(s) == str:
... ..........print s
... .....else:
... ..........for i in s:
... ..........。。。my_print(i)
...

>>> d = ['li', ['mark', 'jim', 'lily', ['lucy', 'gao']]]
>>> my_print(d)
li
mark
jim
lily
lucy
gao
>>>

回答2:

自定义函数递归吧:
def myprint(a):
____for i in a:
________if isinstance(i,list):
____________myprint(i)
________else:
____________print i

a = ["li",["mark","jim","lily",["lucy","gao"]],"master"]
myprint(a)
>>>
li
mark
jim
lily
lucy
gao
master