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

时间:2011-08-21 22:28:24

标签: python list function standards

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

a = [1,2,3,4,5,6,7]

function(a)

print a

a = [1,7,2,6,3,5,4]

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

此致

5 个答案:

答案 0 :(得分:9)

您可以使用generator构建快速,节省内存的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]

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

>>> [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)

答案 1 :(得分:3)

>>> ((a+a[:0:-1])*len(a))[::len(a)][:len(a)]
[1, 7, 2, 6, 3, 5, 4]

答案 2 :(得分:2)

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

答案 3 :(得分:2)

当然在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

答案 4 :(得分:1)

谢谢大家,我写了自己的功能:

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