2016-03-04 52 views
7

我如何通過我的2所列出循環,這樣我可以使用採取從第一個列表中第一個項目,最後一個項目,從第二個列表中創建新列表

a=[1,2,3,8,12] 
b=[2,6,4,5,6] 

得到

[1,6,2,5,3,8,6,12,2] 

OR使用

d=[a,b,c,d] 
e=[w,x,y,z] 

得到

[a,z,b,y,c,x,d,w] 

(從第一列表中第一元件,從第二列表中最後一個元素)
(從第一列表中第二元件,第二從第二列表中最後一個元素)

+0

哪一個是你有困難的部分? –

回答

11
[value for pair in zip(a, b[::-1]) for value in pair] 
5

可以與第二個的反向拉鍊的第一列表(使用itertools.izip_longest),然後加入使用itertools.chain列:

>>> d=['a','b','c','d'] 
>>> e=['w','x','y','z'] 
>>> 
>>> from itertools import chain, zip_longest # in python 2 use izip_longest 
>>> 
>>> list(chain(*izip_longest(d, e[::-1]))) 
['a', 'z', 'b', 'y', 'c', 'x', 'd', 'w'] 

使用zip_longest()的優點是,它需要一個fillvalue參數,它會傳遞給填滿省略項目時,你的列表的長度是不相等的。

如果您確定列表的長度相等,您最好使用內置函數zip()

>>> d=['a','b'] 
>>> e=['w','x','y','z'] 
>>> list(chain(*izip_longest(d, e[::-1], fillvalue=''))) 
['a', 'z', 'b', 'y', '', 'x', '', 'w'] 

更多的@喬恩建議克萊門茨Python的方式:

list(chain.from_iterable(zip_longest(d, reversed(e)))) 
+0

使用'chain.from_iterable(iterable)'而不是'chain(* iterable)'來考慮更好的形式,如果你想避免創建一個新的列表,而不是'e [:: - 1]'你可以使用'reverse(e)'總之:'list(chain。from_iterable(zip_longest(d,reverse(e))))' –

+0

也許我使用的方法是採用'roundrobin' itertools配方,並提供'd'和'reverse(e)'作爲輸入 - (不用擔心fillvalues),並且可以擴展到任意數量的iterables作爲輸入 –

+0

@JonClements是的,使用'reverse()'是一個更好的主意,但我認爲使用'roundrobin'(雖然是pythonic方式)在這種情況下是矯枉過正的。 – Kasramvd

0

嗯,我已經取得了一些試驗python2:

import time 
from operator import itemgetter 
from itertools import chain, izip_longest 

a = [1, 2, 3, 8, 12] 
b = [2, 6, 4, 5, 6] 

print "Using value and zip" 
starttime = time.time() 
c = [value for pair in zip(a, b[::-1]) for value in pair] 
elapsed = time.time() - starttime 
print c 
print elapsed 

print "Using chain and izip" 
starttime = time.time() 
c = list(chain(*izip_longest(a, b[::-1]))) 
elapsed = time.time() - starttime 
print c 
print elapsed 

print "Using itemgetter" 
c = [] 
starttime = time.time() 
for i in xrange(0, len(a)): 
    c.append(itemgetter(i)(a)) 
    c.append(itemgetter(len(b)-i-1)(b)) 
elapsed = time.time() - starttime 
print c 
print elapsed 

輸出:

Using value and zip 
[1, 6, 2, 5, 3, 4, 8, 6, 12, 2] 
1.59740447998e-05 
Using chain and izip 
[1, 6, 2, 5, 3, 4, 8, 6, 12, 2] 
3.2901763916e-05 
Using itemgetter 
[1, 6, 2, 5, 3, 4, 8, 6, 12, 2] 
1.4066696167e-05 

有時第一種方法更快,有時是第三種。

這些是列出了結果lenght = 1000:

Using value and zip 
0.000767946243286 
Using chain and izip 
0.000431060791016 
Using itemgetter 
0.00203609466553 

,你可以看到第二個方法獲得更長的名單更好。

0

如何:

a=[1,2,3,8,12] 
b=[2,6,4,5,6] 
>>> a1 = list(map(lambda x: a1.extend([x,0]), a)) 
[None, None, None, None, None] 
>>> a1 
[1, 0, 2, 0, 3, 0, 8, 0, 12, 0] 
>>> b1 = list(map(lambda x: b1.extend([0,x]), b[::-1])) 
[None, None, None, None, None] 
>>> b1 
[0, 6, 0, 5, 0, 4, 0, 6, 0, 2] 
>>> c = [x+y for x,y in zip(a1,b1)] 
>>> c 
[1, 6, 2, 5, 3, 4, 8, 6, 12, 2] 

如果A和B是具有不同的長度,則:

>>> c = [x+y for x,y in izip_longest(a1,b1)] #you choose your fillvalue.