【问题标题】:Python arranging list with for loopPython用for循环排列列表
【发布时间】:2021-05-03 17:03:33
【问题描述】:

问题连同解决方案一起发布。但我不明白解决方案,尤其是在 for 循环中。有人可以为我详细说明代码吗?

[1]:https://i.stack.imgur.com/p1Lkp.jpg![enter此处的图片描述](https://i.stack.imgur.com/dMYgZ.jpg)

【问题讨论】:

  • 你不明白哪一部分?请输入你的代码,而不是image的问题。

标签: arrays list loops for-loop alternating


【解决方案1】:

在这里,我尝试对您发布的问题进行一些解释。
实际上,如果您可以通过平台运行它来查看程序在运行中会更容易 - 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2014-02-22
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多