在列表中添加两个连续的数字

时间:2012-02-06 14:20:57

标签: python

如何在列表中添加两个连续的数字。

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

result = [3,7,11,15,9]

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

result = [3,7,11,15,19]

我可以使用简单的for循环轻松实现它。但 我怎样才能用更多的pythonic方式实现它。

6 个答案:

答案 0 :(得分:13)

import itertools as it    
[sum(r) for r in it.izip_longest(l[::2], l[1::2], fillvalue=0)]

返回奇数和偶数的等待值:

l = [1,2,3,4,5,6,7,8,9]    # [3, 7, 11, 15, 9]
l = [1,2,3,4,5,6,7,8,9,10] # [3, 7, 11, 15, 19]

更新:如果原始列表非常大,您可以使用islice替换简单切片:

[sum(r) for r in it.izip_longest(it.islice(l,0,None,2), it.islice(l,1,None,2), fillvalue=0)]

更新2:即使是更短,更通用的版本(没有itertools)也来到这里:

l = [1,2,3,4,5,6,7,8,9,10]
n = 3

[sum(l[i:i+n]) for i in xrange(0, len(l), n)]
# returns: [6, 15, 24, 10]

答案 1 :(得分:6)

您可以使用迭代器来避免中间列表:

>>> it = iter([1,2,3,4,5,6,7,8,9,10])
>>> [i + next(it, 0) for i in it]
[3, 7, 11, 15, 19]

它也适用于[1,2,3,4,5,6,7,8,9],因为next将在StopIteration上返回零。

答案 2 :(得分:3)

最好的方式!

我现在想改变我对此的回答。我比itertools解决方案更喜欢它;我认为这是最少的代码(如果你计算导入itertools):

>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> if len(x) % 2: x.append(0)
... 
>>> map(sum, zip(x[::2], x[1::2]))
[3, 7, 11, 15, 9]

我知道有些人不喜欢地图,但我喜欢它:)我记得reading somewhere它比列表迭代更快。


我原来的回答:

>>> x=[1,2,3,4,5,6,7,8,9,10]
>>> [a+b for a,b in zip(x[::2], x[1::2])]
[3, 7, 11, 15, 19]

但是没有给出奇数编号列表的答案:

>>> x=[1,2,3,4,5,6,7,8,9]
>>> [a+b for a,b in zip(x[::2], x[1::2])]
[3, 7, 11, 15]

kludge fix:

>>> x   
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> result = [a+b for a,b in zip(x[::2], x[1::2])]
>>> if len(x) % 2: result.append(x[-1])
... 
>>> print result
[3, 7, 11, 15, 9]

:)

答案 3 :(得分:1)

这是一种Pythonic高效的方式,因为它只会迭代list一次:

In [1]: l = [1,2,3,4,5,6,7,8,9]
In [2]: from itertools import izip_longest
In [3]: [sum (t) for t in izip_longest(* 2 * [iter(l)], fillvalue=0)]
Out[3]: [3, 7, 11, 15, 9]

请参阅:How does zip(*[iter(s)]*n) work in Python?,了解有关奇怪的“2 - iter相同list”语法的解释。


定时:

% python -m timeit -c 'l = [1,2,3,4,5,6,7,8,9]
from itertools import izip_longest
[sum (t) for t in izip_longest(* 2 * [iter(l)], fillvalue=0)]
'
100000 loops, best of 3: 9.42 usec per loop

答案 4 :(得分:0)

编写nsplit以拆分列表(n项目组):

>>> ls = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> nsplit = lambda s, n: [s[i:i+n] for i in range(0, len(s), n)]

# [1+2, 3+4, 5+6, 7+8, 9]
>>> [sum(x) for x in nsplit(ls, 2)]
[3, 7, 11, 15, 9]

# [1+2+3, 4+5+6, 7+8+9]
>>> [sum(x) for x in nsplit(ls, 3)]
[6, 15, 24]

# [1+2+3+4, 5+6+7+8, 9]
>>> [sum(x) for x in nsplit(ls, 4)]
[10, 26, 9]

答案 5 :(得分:0)

from itertools import chain

l = [1,2,3,4,5,6,7,8,9]
it = chain(l,[0])
result = list(x + next(it) for x in it)
print l,'\n',result,'\n'


l = [1,2,3,4,5,6,7,8,9,10]
it = chain(l,[0])
result = list(x + next(it) for x in it)
print l,'\n',result,'\n'



l = [1,2,3,4,5,6,7,8,9]
it = chain(l,[0,0])
result = list(x + next(it) + next(it) for x in it)
print l,'\n',result,'\n'

l = [1,2,3,4,5,6,7,8,9,10]
it = chain(l,[0,0])
result = list(x + next(it)+ next(it) for x in it)
print l,'\n',result,'\n'

产生

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

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
[3, 7, 11, 15, 19] 

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

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

但我更喜欢JBernardo - glglgl的解决方案