小编Flo*_*mer的帖子

如何根据索引模3进行列表元素的置换?

我想根据它们的索引模3来置换列表的元素,例如列表:

[0,1,2,3,4,5,6,7,8]
Run Code Online (Sandbox Code Playgroud)

应重新订购:

[0,3,6,1,4,7,2,5,8]
Run Code Online (Sandbox Code Playgroud)

一般来说:

[A0, A1, A2, A3, A4, A5, A6, A7, A8]
Run Code Online (Sandbox Code Playgroud)

应成为:

[A0, A3, A6, A1, A4, A7, A2, A5, A8]
Run Code Online (Sandbox Code Playgroud)

我试过使用以下代码:

def arr_sort(arr, algo):
    arrtmp = arr

    arrlen = len(arr)

    if algo == 1:
        return arr

    if algo == 2:
        count = 0
        while count < (arrlen - 1):
            for index, val in enumerate(arr):
                if index % 3 == 0:
                    arrtmp[count] = val
                    count += 1
            for index, val in enumerate(arr):
                if index % 3 == …
Run Code Online (Sandbox Code Playgroud)

python sorting permutation

0
推荐指数
1
解决办法
503
查看次数

标签 统计

permutation ×1

python ×1

sorting ×1