Python 两两统一列表项

Python 两两统一列表项,python,list-comprehension,Python,List Comprehension,给你一张单子 [1,2,3,4,5,6] 具有2n个元素 我怎样才能得到这份名单 [1+2,3+4,5+6] 有n个元素?来自: 然后致电: for paired in grouper(2, inputlist): # paired is a tuple of two elements from the inputlist at a time. grouper返回一个迭代器;如果必须有一个列表,只需将iterable消费到一个新列表中: newlist = list(grouper

给你一张单子

[1,2,3,4,5,6]
具有2n个元素

我怎样才能得到这份名单

[1+2,3+4,5+6]
有n个元素?

来自:

然后致电:

for paired in grouper(2, inputlist):
    # paired is a tuple of two elements from the inputlist at a time.
grouper返回一个迭代器;如果必须有一个列表,只需将iterable消费到一个新列表中:

newlist = list(grouper(2, inputlist))
从:

然后致电:

for paired in grouper(2, inputlist):
    # paired is a tuple of two elements from the inputlist at a time.
grouper返回一个迭代器;如果必须有一个列表,只需将iterable消费到一个新列表中:

newlist = list(grouper(2, inputlist))
给予

给予


如果izip_longest不是python2定义的,我真的不能使用py3呢?它是在python2中定义的;python2.6及以上版本,看看如果izip_longest没有定义python2,我真的不能使用py3呢?它是在python2中定义的;python 2.6及更高版本,请参见
li = [1,2,10,20,100,200,2000,3000,2,2,3,3,5,5]
print li

it = iter(li)
if len(li)%2==0:
    print [x+it.next() for x in it]
[1, 2, 10, 20, 100, 200, 2000, 3000, 2, 2, 3, 3, 5, 5]
[3, 30, 300, 5000, 4, 6, 10]