Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过从第一个列表中获取第一项,从第二个列表中获取最后一项来创建新列表_Python_List_Python 3.x - Fatal编程技术网

Python 通过从第一个列表中获取第一项,从第二个列表中获取最后一项来创建新列表

Python 通过从第一个列表中获取第一项,从第二个列表中获取最后一项来创建新列表,python,list,python-3.x,Python,List,Python 3.x,如何循环浏览我的2个列表,以便使用 a=[1,2,3,8,12] b=[2,6,4,5,6] 得到 [1,6,2,5,3,8,6,12,2] 或使用 d=[a,b,c,d] e=[w,x,y,z] 得到 [a,z,b,y,c,x,d,w] (第一个列表中的第一个元素,第二个列表中的最后一个元素) (第一个列表中的第二个元素,第二个列表中的第二个到最后一个元素)您可以使用第二个列表的反面压缩第一个列表(使用itertools.izip\u longest),然后使用itertools.ch

如何循环浏览我的2个列表,以便使用

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

[1,6,2,5,3,8,6,12,2]
或使用

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

[a,z,b,y,c,x,d,w]
(第一个列表中的第一个元素,第二个列表中的最后一个元素)

(第一个列表中的第二个元素,第二个列表中的第二个到最后一个元素)

您可以使用第二个列表的反面压缩第一个列表(使用
itertools.izip\u 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()

@Jon Clements建议的更具蟒蛇风格的方式:

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

好的,我已经对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
有时第一种方法更快,有时第三种方法更快

以下是列表长度=1000的结果:

Using value and zip
0.000767946243286
Using chain and izip
0.000431060791016
Using itemgetter
0.00203609466553
如您所见,第二种方法对于较长的列表会更好。

这个方法如何:

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.

您对哪一部分有困难?使用
链被认为是更好的形式。从
链(*iterable)
改为
链(*iterable)
。。。如果你想避免创建一个新的列表,而不是
e[::-1]
,你可以使用
reversed(e)
,所以总而言之:
list(chain.from_iterable(zip_iterable(d,reversed(e))
也许我会使用的方法是采用
roundrobin
itertools配方,并提供
d
reversed(e)
作为输入-这将很好地处理不同长度的列表(无需担心FillValue),并可根据需要扩展到任意数量的iTerableinput@JonClements是的,使用
reverse()。
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]
>>> c = [x+y for x,y in izip_longest(a1,b1)] #you choose your fillvalue.