假设我的列表如下:
myList = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
我希望结果如下:
One, Eight, Two, Seven, Three, Six, Four, Five
最简单的方法是什么?
答案 0 :(得分:5)
from collections import deque
myList = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
def swap(items):
items = deque(items)
while items:
yield items.popleft()
try:
yield items.pop()
except IndexError:
pass
print list(swap(myList))
编辑:现在非破坏性并应对不均匀的长度列表
编辑:使用deque使其内存效率
答案 1 :(得分:1)
如果我理解的话,这会创建一个包含所需顺序的新列表。
myList = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
i = 0
new_list = []
while i < len(myList)/2:
new_list.append(myList[i])
i += 1
new_list.append(myList[-i])
print ', '.join(new_list)
答案 2 :(得分:0)
切片:
>>> result = list(myList)
>>> result[::2] = myList[:4]
>>> result[::-2] = myList[4:]
>>> result
['One', 'Eight', 'Two', 'Seven', 'Three', 'Six', 'Four', 'Five']
第一个赋值可以替换为创建正确长度列表的任何内容,然后myList的前4个元素成为结果的每个第二个元素,后4个元素是从末尾后退的每个第二个元素。 / p>
当然,如果你想要没有括号的输出:
>>> print(', '.join(result))
One, Eight, Two, Seven, Three, Six, Four, Five
如果您不介意拆分原始列表,可以一行完成:
>>> myList[::2], myList[::-2] = myList[:4], myList[4:]
>>> myList
['One', 'Eight', 'Two', 'Seven', 'Three', 'Six', 'Four', 'Five']
答案 3 :(得分:0)
myList = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
z=[x for x in myList if myList.index(x)>=len(myList)/2]
print zip(myList,reversed(z))
你可以试试这个。
答案 4 :(得分:0)
如果你想要一个单行,你可以看看:
final = [el for zipped in zip(lst[:4], reversed(lst[4:])) for el in zipped]
答案 5 :(得分:0)
以下两种方法可以解决不需要异常处理的问题,将输入列表加载到备用数据结构中只是为了销毁它。他们通过计算列表的中点并清楚地知道他们是否正在处理奇数长度列表来实现这一点:
def interleave(l):
"""
Non-destructive front-and-back iterleave of list items.
"""
midpoint, odd_length = divmod(len(l), 2)
for i in range(midpoint):
yield l[i]
yield l[-i-1]
if odd_length:
yield l[midpoint]
或者:
def interleave2(l):
"""
Non-destructive front-and-back iterleave of list items.
Uses zip and reversed generators and list slicing.
Arguably more "Pythonic," but depends on more in-flight
infrastructure and consumes more memory.
"""
midpoint, odd_length = divmod(len(l), 2)
for front, back in zip(l[:midpoint], reversed(l[midpoint:])):
yield front
yield back
if odd_length:
yield l[midpoint]
这里有一个即兴的测试工具来确认结果是否正确:
myList = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
myListAnswer = ['One', 'Eight', 'Two', 'Seven', 'Three', 'Six', 'Four', 'Five']
answers = {}
for algorithm in [interleave, interleave2]:
print()
print(algorithm.__name__, "\n", "=" * len(algorithm.__name__), "\n")
# test the simple case
assert list(algorithm(myList)) == myListAnswer
# test a number of list sizes
for j in range(11):
# run algorithm and show inputs and outputs
l = list(range(j))
print("size:", j)
print("list:", l)
answer = list(algorithm(l))
print("answer:", answer)
# check assertions
prev_answer = answers.setdefault(j, answer)
assert len(l) == len(answer)
assert l == sorted(answer)
assert answer == prev_answer
print("--")
这会产生:
interleave
==========
size: 0
list: []
answer: []
--
size: 1
list: [0]
answer: [0]
--
size: 2
list: [0, 1]
answer: [0, 1]
--
size: 3
list: [0, 1, 2]
answer: [0, 2, 1]
--
size: 4
list: [0, 1, 2, 3]
answer: [0, 3, 1, 2]
--
size: 5
list: [0, 1, 2, 3, 4]
answer: [0, 4, 1, 3, 2]
--
size: 6
list: [0, 1, 2, 3, 4, 5]
answer: [0, 5, 1, 4, 2, 3]
--
size: 7
list: [0, 1, 2, 3, 4, 5, 6]
answer: [0, 6, 1, 5, 2, 4, 3]
--
size: 8
list: [0, 1, 2, 3, 4, 5, 6, 7]
answer: [0, 7, 1, 6, 2, 5, 3, 4]
--
size: 9
list: [0, 1, 2, 3, 4, 5, 6, 7, 8]
answer: [0, 8, 1, 7, 2, 6, 3, 5, 4]
--
size: 10
list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
answer: [0, 9, 1, 8, 2, 7, 3, 6, 4, 5]
--
# ...and continues on with similar output for interleave2 ...