用于重新排列列表的Python标准库函数



我想知道Python中是否有一个标准库函数可以重新排列列表中的元素,如下所示:

a = [1,2,3,4,5,6,7]
function(a)
print a
a = [1,7,2,6,3,5,4]

它应该从原始列表的开头获取一个元素,然后从末尾获取一个,然后从开头获取第二个,依此类推。然后重新排列列表。

问候,

您可以使用itertools构建一个快速、内存高效的生成器,它可以实现您想要的功能:

from itertools import chain, izip
def reorder(a):
    gen = chain.from_iterable(izip(a, reversed(a)))
    for _ in a:
        yield next(gen)
>>> list(reorder(a))
<<< [1, 7, 2, 6, 3, 5, 4]

您会发现itertools有一个很好的构建块集合,用于创建您自己的高效迭代器。一个稍微简洁一点的解决方案:

>>> list(chain.from_iterable(izip(a, reversed(a))))[:len(a)]
<<< [1, 7, 2, 6, 3, 5, 4]

列表理解是构建列表的另一种非常简洁的方法:

>>> [x for t in zip(a, reversed(a)) for x in t][:len(a)]
<<< [1, 7, 2, 6, 3, 5, 4]

最后,这里有一句简短的俏皮话:

>>> sum(zip(a, a[::-1]), ())[:len(a)]
<<< (1, 7, 2, 6, 3, 5, 4)
>>> ((a+a[:0:-1])*len(a))[::len(a)][:len(a)]
[1, 7, 2, 6, 3, 5, 4]
for a in ([1,2,3,4,5,6,7,8,9],
          [1,2,3,4,5,6,7,8],
          [1,2,3,4],
          [1,2,3],
          [1,2,],
          [1],
          []):
    print a
    [ a.insert(i,a.pop()) for i in xrange(1,len(a)+1,2)]
    print a,'n'

结果

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 9, 2, 8, 3, 7, 4, 6, 5] 
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 8, 2, 7, 3, 6, 4, 5] 
[1, 2, 3, 4]
[1, 4, 2, 3] 
[1, 2, 3]
[1, 3, 2] 
[1, 2]
[1, 2] 
[1]
[1] 
[]
[] 

更新1

与zeekay的代码相比:

from time import clock

n = 100000

te = clock()
for i in xrange(n):
    a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
    [ a.insert(i,a.pop()) for i in xrange(1,len(a)+1,2)]
print clock()-te

from itertools import chain, izip
def reorder(a):
    gen = chain(*izip(a, reversed(a)))
    for _ in a:
        yield next(gen)
te = clock()
for i in xrange(n):
    a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
    a = list(reorder(a)) 
print clock()-te

结果

2.36667984339
5.00051766356

我的方法更改了a位置

当然,在Python中,做事只有一种方法;-(:

def function(a):
    ret = []
    this_end, other_end = 0, -1
    while a:
        ret.append(a.pop(this_end))
        this_end, other_end = other_end, this_end
    return ret
a = [1,2,3,4,5,6,7]
print function(a)

时间安排:

% python -m timeit 'def function(a):
quote>     ret = []
quote>     this_end, other_end = 0, -1
quote>     while a:
quote>         ret.append(a.pop(this_end))
quote>         this_end, other_end = other_end, this_end
quote>     return ret
quote>
quote> a = [1,2,3,4,5,6,7]
quote>
quote> print function(a)
quote> ' | tail
[1, 7, 2, 6, 3, 5, 4]
[1, 7, 2, 6, 3, 5, 4]
[1, 7, 2, 6, 3, 5, 4]
[1, 7, 2, 6, 3, 5, 4]
[1, 7, 2, 6, 3, 5, 4]
[1, 7, 2, 6, 3, 5, 4]
[1, 7, 2, 6, 3, 5, 4]
[1, 7, 2, 6, 3, 5, 4]
[1, 7, 2, 6, 3, 5, 4]
100000 loops, best of 3: 10.5 usec per loop

谢谢大家,我已经编写了自己的函数:

def shake(list):
    """Gets a list and reorders the items,
       one from beginning, one from end"""
    #print "original list is: ", list
    new_list = []
    x = len(list) - 1
    y = len(list)/2
    for i in xrange(y):
        if list[i] not in new_list:
            new_list.append(list[i])
        if list[i+x] not in new_list:
            new_list.append(list[i+x])
        x -= 2
    if len(list)%2 == 1:
        new_list.append(list[y])
    #print "new list is: ", new_list 
    return new_list

最新更新