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 每次迭代列表中的每2个元素_Python_List_Python 2.7 - Fatal编程技术网

Python 每次迭代列表中的每2个元素

Python 每次迭代列表中的每2个元素,python,list,python-2.7,Python,List,Python 2.7,将列表列为 l = [1,2,3,4,5,6,7,8,9,0] 如何一次迭代两个元素 我正在尝试这个 for v, w in zip(l[:-1],l[1:]): print [v, w] 获得输出就像 [1, 2] [2, 3] [3, 4] [4, 5] [5, 6] [6, 7] [7, 8] [8, 9] [9, 0] 预期产量为 [1,2] [3, 4] [5, 6] [7, 8] [9,10] 一个解决办法是 for v, w in zip(l[::2],l[1::2

将列表列为

l = [1,2,3,4,5,6,7,8,9,0]
如何一次迭代两个元素

我正在尝试这个

for v, w in zip(l[:-1],l[1:]):
    print [v, w]
获得输出就像

[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5, 6]
[6, 7]
[7, 8]
[8, 9]
[9, 0]
预期产量为

[1,2]
[3, 4]
[5, 6]
[7, 8]
[9,10]
一个解决办法是

for v, w in zip(l[::2],l[1::2]):
    print [v, w]

您可以使用iter:

>>> seq = [1,2,3,4,5,6,7,8,9,10]
>>> it = iter(seq)
>>> for x in it:
...     print (x, next(it))
...     
[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]
您还可以使用itertools中的:

>>> from itertools import izip_longest
>>> def grouper(iterable, n, fillvalue=None):
...         "Collect data into fixed-length chunks or blocks"
...         # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
...         args = [iter(iterable)] * n
...         return izip_longest(fillvalue=fillvalue, *args)
... 
>>> for x, y in grouper(seq, 2):
...     print (x, y)
...     
[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]

您可以按自己的方式进行,只需在切片中添加一个步骤部分,使两个切片都跳过一个数字:

for v, w in zip(l[::2],l[1::2]):  # No need to end at -1 because that's the default
    print [v, w]
但我喜欢帮助生成器:

def pairwise(iterable):
    i = iter(iterable)
    while True:
       yield i.next(), i.next()

for v, w in pairwise(l):
    print v, w
有什么问题吗

l = [1, 2, 3, 4, 5, 6, 7, 8]
for j in range(0, len(l), 2):
        print(l[j: j + 2])

[1, 2]
[3, 4]
[5, 6]
[7, 8]

假设列表中的元素数为偶数

对于那些来到这里希望以很长的步幅浏览列表并且不想预先使用大量内存的人,您可以这样做

Python 2.7:

import itertools

def step_indices(length, step):
    from_indices = xrange(0, length, step)
    to_indices = itertools.chain(xrange(step, length, step), [None])
    for i, j in itertools.izip(from_indices, to_indices):
        yield i, j

the_list = range(1234)
for i, j in step_indices(len(the_list), 100):
    up_to_100_values = the_list[i:j]
Python 3:

import itertools

def step_indices(length, step):
    from_indices = range(0, length, step)
    to_indices = itertools.chain(range(step, length, step), [None])
    for i, j in zip(from_indices, to_indices):
        yield i, j

the_list = list(range(1234))
for i, j in step_indices(len(the_list), 100):
    up_to_100_values = the_list[i:j]

另一个带有
map
(语法与其他答案略有不同)的解决方案。在具有奇数个元素的列表中,最后一个元素与
None
成对出现:

>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> map(None, a[::2], a[1::2])
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (11, None)]
您可以修改函数以返回列表而不是元组:

>>> map(lambda x,y:[x,y], a[::2], a[1::2])
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, None]]

>>> for e in map(lambda x,y:[x,y], a[::2], a[1::2]):
...     print e
... 
[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]
[11, None]

编辑:此解决方案仅适用于Python2.x。

我知道这是一个老问题,只是一种不同的方法

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
chunks = [l[x:x+2] for x in range(0,len(l),2)]
print(chunks)

如何使它成为列表列表而不是元组列表?l=[1,2,3,4,5,6,7,8,9,10]l2=map(无,*[iter(l)]*2)print(l2)给出map对象而不是k,v对。print(list(l2))给出TypeError:“非类型”对象不可调用
>>> map(lambda x,y:[x,y], a[::2], a[1::2])
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, None]]

>>> for e in map(lambda x,y:[x,y], a[::2], a[1::2]):
...     print e
... 
[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 10]
[11, None]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
chunks = [l[x:x+2] for x in range(0,len(l),2)]
print(chunks)