假设我有以下元组:
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)
我怎样才能做到这一点(就像毕多尼亚的那种方式)?
谢谢..。
发布于 2018-08-02 18:23:59
但是,通过将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)
发布于 2018-08-02 18:17:38
直接、简单、易读的答案(丙酮?):
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)
发布于 2018-08-02 18:27:38
这是一个基于生成器的解决方案。这样做的好处是,该解决方案不要求输入是可切片的,因此可以将其应用于像zip
和map
这样的函数的输出。
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]
https://stackoverflow.com/questions/51659879
复制相似问题