一次迭代列表两项 [英] Iterate through list two items at a time

查看:91
本文介绍了一次迭代列表两项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在寻找一种在

时间内遍历列表,两个(或更多)项目的方法。基本上......


myList = [1,2,3,4,5,6]


我想成为能够一次拉出两件物品 - 简单的例子是

be:

创建此输出:

1 2

3 4

5 6


创建此列表:

[(1,2),(3,4) ),(5,6)]


我希望使用以下语法,但遗憾的是它没有:

for my,x:y in myList:

打印x,y


我可以在tcl中使用简单的foreach语句来完成此操作,如果它很容易在
$ b $中b tcl在Python中可能不太难。


谢谢,

Dave

Hi all,
I''m looking for a way to iterate through a list, two (or more) items at a
time. Basically...

myList = [1,2,3,4,5,6]

I''d like to be able to pull out two items at a time - simple examples would
be:
Create this output:
1 2
3 4
5 6

Create this list:
[(1,2), (3,4), (5,6)]

I want the following syntax to work, but sadly it does not:
for x,y in myList:
print x, y

I can do this with a simple foreach statement in tcl, and if it''s easy in
tcl it''s probably not too hard in Python.

Thanks,
Dave

推荐答案

1月2日,下午7:57,Dave Dean < dave.d ... @ xilinx.comwrote:
On Jan 2, 7:57 pm, "Dave Dean" <dave.d...@xilinx.comwrote:

大家好,

我正在寻找迭代的方法通过列表,两个(或更多)项目在

时间。基本上......


myList = [1,2,3,4,5,6]


我想成为能够一次拉出两个项目...
Hi all,
I''m looking for a way to iterate through a list, two (or more) items at a
time. Basically...

myList = [1,2,3,4,5,6]

I''d like to be able to pull out two items at a time...



def pair_list(list_):

返回[list_ [i:i + 2] for x in xrange(0,len(list_),2)]

def pair_list(list_):
return[list_[i:i+2] for i in xrange(0, len(list_), 2)]


很少有替代解决方案(其他可能),我通常使用变体

的第一个版本,在分区函数内,第二个变种

当你没有一个方便的partition()函数并且你
时更短
不想导入模块,第四个需要更少的内存当

数据很长时间:


来自itertools import izip,islice


data = [1,2,3,4,5,6,7]

for x1,x2 in(数据[i:i + 2] for x in xrange(0,len(data)/ 2 * 2,2)):

print x1,x2


为x1,x2为zip(data [:: 2],data [1 :: 2]):

print x1,x2


for izip中的x1,x2(data [:: 2],data [1 :: 2]):

print x1,x2

for x1,x2 in izip(islice(data,0,None,2),islice(data,1,None,2) ):

打印x1,x2

再见,

bearophile

Few alternative solutions (other are possible), I usually use a variant
of the first version, inside a partition function, the second variant
is shorter when you don''t have a handy partition() function and you
don''t want to import modules, and the forth one needs less memory when
the data is very long:

from itertools import izip, islice

data = [1,2,3,4,5,6,7]

for x1, x2 in (data[i:i+2] for i in xrange(0, len(data)/2*2, 2)):
print x1, x2

for x1, x2 in zip(data[::2], data[1::2]):
print x1, x2

for x1, x2 in izip(data[::2], data[1::2]):
print x1, x2

for x1, x2 in izip(islice(data,0,None,2), islice(data,1,None,2)):
print x1, x2

Bye,
bearophile

< br>


>我正在寻找一种方法来遍历列表,两个(或更多)项目
一次。基本上......

myList = [1,2,3,4,5,6]

我希望能够在时间...
>I''m looking for a way to iterate through a list, two (or more) items
at a time. Basically...

myList = [1,2,3,4,5,6]

I''d like to be able to pull out two items at a time...



Dandef pair_list(list_):

Dan返回[list_ [i:i + 2] for我在xrange(0,len(list_),2)]


这是'另一种方式(对我来说似乎有点清楚,但每个人都有自己的

查看事物的方式):

Dandef pair_list(list_):
Dan return[list_[i:i+2] for i in xrange(0, len(list_), 2)]

Here''s another way (seems a bit clearer to me, but each person has their own
way of seeing things):


>> import string
string.letters
>>import string
string.letters



''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ''

''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ''


>> zip(string.letters [:: 2],string.letters [1 :: 2])
>>zip(string.letters[::2], string.letters[1::2])



[(''a'',''b''),(''c'',''d''),...,( ''W'',''X' '),(''Y'',''Z'')]


它可以扩展到更长的分组:

[(''a'', ''b''), (''c'', ''d''), ..., (''W'', ''X''), (''Y'', ''Z'')]

It extends readily to longer groupings:


>> zip(string.letters [:: 3],string.letters [1 :: 3],string.letters [2 :: 3])
>>zip(string.letters[::3], string.letters[1::3], string.letters[2::3])



[(''a'',''b'',''c''),(' 'd'','e'',''f''),(''''',h,我),...


显然,如果你的名单很长,你可以用itertools.izip代替

zip。使用

itertools.groupby可能有一些简单的方法可以达到相同的效果,但我不在我的经验中......


跳过

[(''a'', ''b'', ''c''), (''d'', ''e'', ''f''), (''g'', ''h'', ''i''), ...

Obviously, if your lists are long, you can substitute itertools.izip for
zip. There''s probably some easy way to achieve the same result with
itertools.groupby, but I''m out of my experience there...

Skip


这篇关于一次迭代列表两项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆