Python反转元组的某些部分

时间:2018-08-02 18:12:12

标签: python python-3.x python-2.7

假设我有以下元组为:

a = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)

我想做的是反转元组的某些部分。

例如4个元素保持不变,然后4个元素将颠倒。

我想得到以下结果:

a = (1,2,3,4,8,7,6,5,9,10,11,12,16,15,14,13,17,18,19,20) 

我该如何实现(以pythonian的方式)?

谢谢...

5 个答案:

答案 0 :(得分:1)

直接,简单,易读的答案(pythonic?):

a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

for n in range(0, len(a), 8):
    a[n+4:n+8] = a[n+7:n+3:-1]

print(a)

答案 1 :(得分:1)

音色是不变的,但是,通过将a转换为list,可以执行就地分配:

a = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
new_a = list(a)
for c, i in enumerate(range(0, len(a), 4)):
  if c%2: 
     new_a[i:i+4] = new_a[i:i+4][::-1]

print(tuple(new_a))

输出:

(1, 2, 3, 4, 8, 7, 6, 5, 9, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19, 20)

答案 2 :(得分:1)

这是一个基于生成器的解决方案。这样做的好处是该解决方案不需要输入是可切片的,因此您可以将其应用于zipmap之类的函数的输出。

from itertools import zip_longest as zipl
from itertools import cycle, chain

_marker = object()

def cycle_map(iterable, func_iterable):
    funcs = cycle(func_iterable)
    for func, item in zip(funcs, iterable):
        if func:
            yield func(item)
        else:
            yield item

def reverse_filter(iterable, remove=_marker):
    t = tuple(i for i in iterable if i is not remove)
    return reversed(t)

def reverse_alternating(iterable, length=4):
    chunks = zipl(*[iter(iterable)]*length, fillvalue=_marker)
    funcs = (None, reverse_filter) 
    return chain.from_iterable(cycle_map(chunks, funcs))

a = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
list(reverse_alternating(a))
# [1, 2, 3, 4, 8, 7, 6, 5, 9, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19, 20]

答案 3 :(得分:0)

使用列表切片。

例如:

from itertools import chain
a = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
res = [a[v:v+4] if i%2 == 0 else list(reversed(a[v:v+4])) for i, v in enumerate(range(0, len(a), 4))]
print(tuple(chain.from_iterable(res)))    #Flatten list

输出:

(1, 2, 3, 4, 8, 7, 6, 5, 9, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19, 20)

答案 4 :(得分:0)

这是使用列表理解解决问题的另一种方法(可能不是最快的方法)。想要共享逻辑,假设len(a)4整除:

a = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
lst = [a[j:j+4] if i%2 == 0 else a[j:j+4][::-1] for i, j in 
enumerate(range(0,len(a), 4))]
result = tuple([j for i in lst for j in i])
print (result)

输出

(1, 2, 3, 4, 8, 7, 6, 5, 9, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19, 20)