Python用for循环排列列表



问题与解决方案一起发布。但是我不明白这个解决方案,尤其是在for循环里面。谁能给我详细解释一下密码吗?

[1]: https://i.stack.imgur.com/p1Lkp.jpg ![输入图片描述](https://i.stack.imgur.com/dMYgZ.jpg)

在这里我试着对你发布的问题进行一些解释。
实际上,如果您可以通过平台运行它来查看程序的运行情况,则会更容易- http://pythontutor.com/下次。

# interleave a list items:
#
array = [3, 6, 1, 2, 'a', 'c', 'b', 'z']
#        0  1  2  3   4    5    6   7
N = len(array)    # the length of the array a
out =  []         # to stort the output
c = mid = N //2   # mid-point of the array
print(f' c: {c} ')  
for item in array:
if c != N:
print(array[c])

out += [item] + [array[c]]   # get 4th item - 'a', append it; then continue
c += 1   # moving this mid-point
print(f' c is {c} ')
print(out)

最新更新