简体   繁体   English

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

[英]adding two consecutive numbers in a list

How to add two consecutive number in the list. 如何在列表中添加两个连续的数字。

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

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

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

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

I can easily achieve it using simple for loop. 我可以使用简单的for循环轻松实现它。 But How can I achieve it using more pythonic way. 但是我如何使用更多的pythonic方式实现它。

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

returns awaited values for both odd and even numbers: 返回奇数和偶数的等待值:

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]

UPDATE: if the original list is really large, you can replace the simple slices with islice : 更新:如果原始列表非常大,您可以使用islice替换简单切片:

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

UPDATE 2: even a shorter and more universal version (without itertools) comes here: 更新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]

You can use iterators to avoid intermediate lists: 您可以使用迭代器来避免中间列表:

>>> 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]

It will also work with [1,2,3,4,5,6,7,8,9] because next will return zero on StopIteration . 它也适用于[1,2,3,4,5,6,7,8,9]因为next将在StopIteration上返回零。

Best way! 最好的办法!

I want to change my answer to this now. 我现在想改变我对此的回答。 I like it more than the itertools solutions; 我比itertools解决方案更喜欢它; I think it's the least code (if you count importing 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]

I know some people don't like map, but I like it :) I remember reading somewhere that it's faster than list iterations. 我知道有些人不喜欢地图,但我喜欢它:)我记得在某处读它比列表迭代更快。


My original answer: 我的原始答案:

>>> 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]

But doesn't give your answer for oddly numbered lists: 但是没有给出奇怪编号列表的答案:

>>> 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: kludge修复:

>>> 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]

:) :)

A Pythonic and efficient way is this, as it only ever iterates over the list once: 这是一个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]

See: How does zip(*[iter(s)]*n) work in Python? 请参阅: zip(* [iter(s)] * n)如何在Python中工作? for explanations as to that strange "2- iter over the same list " syntax. 关于那个奇怪的“同一list 2- iter ”语法的解释。


Timing: 定时:

% 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

Write nsplit to split a list ( n items a group): 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]
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'

produces 产生

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

But I prefer the JBernardo - glglgl 's solution 但我更喜欢JBernardo - glglgl的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM