我只是想知道如何遍历嵌套的Leftsequence?
我用以下代码进行了左移:
sz = sz * 2
输出= def leftSeq(s):
ls = "leer"
for i in range(len(s), 0, -1):
ls = (l[i - 1], ls)
return ls
l = [1,2,3]
我如何遍历它?
答案 0 :(得分:2)
您可以为此类左序列进行迭代:
def left_iterator(lseq):
while isinstance(lseq,tuple) and len(lseq) == 2:
first, lseq = lseq
yield first
yield lseq
#test:
x = (1, (2, (3, 'leer')))
for i in left_iterator(x):
print(i)
输出:
1
2
3
leer
答案 1 :(得分:0)
您可以像这样使用while循环,并嵌套到ls
:
x = (1, (2, (3, 'leer'))) #=leftSeq(l)
while len(x) > 1:
print(x)
x = x[1]
(1, (2, (3, 'leer')))
(2, (3, 'leer'))
(3, 'leer')
leer