列表中没有索引的所有项目对? [英] all pairs of items in a list without indexing?

查看:77
本文介绍了列表中没有索引的所有项目对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要做类似的事情:


for i in range(len(l)):

for j in range(i + 1 ,len(l)):

#用(l [i],l [j])做某事


我得到所有项目对一个列表(我想的是对,

作为集合,而不是元组,所以顺序没关系)。这里的解决方案确实没有什么问题,但是由于Python的每个

构造都很好,我通常会尽量避免范围( len(..))类型

调用和列表索引当我可以...有没有办法做到这一点

没有索引,例如:


for item1 in ...:

for item2 in ...:

#做某事(item1,item2)


我可以这样做:


for i,item1 in enumerate(l):

for j in range(i +1,len(l)):

(item1,l [j])


但这只能让我到达那里...我还以为类似于:


for i,item1 in enumerate(l):

for item2 in l [i + 1:]:

(第1项,第2项)


但这似乎是很多浪费的列表切片...


提前谢谢,


史蒂夫

-

如果你只是动词,你可以用任何语法表达。

- Bucky卡特,得到模糊

解决方案

Steven Bethard写道:

所以我需要做类似的事情:

for in in range(len(l)):
for j in range(i + 1,len(l)):
#做某事(l [i],l [j])

在哪里我得到列表中的所有项目对(我想的是对,
作为集合,而不是元组,所以顺序无所谓)。这里的解决方案确实没有任何问题,但是由于Python的每个
构造都很好,我通常会尝试避免范围(len(..))类型
调用和列表索引,当我可以...有没有办法做到这一点
没有索引




你是否想要配对每个项目一个列表与其他每个

项目恰好一次?也许这段代码可以做你想要的:


而len(L)> 0:

item1 = L.pop()
$ b L中item2的$ b:

print(item1,item2)

pop()从列表中删除一个项目。内部for循环与每个剩余项目匹配

项目。在外循环结束时,

列表L为空;你可能想要在列表的副本上运行这个循环

如果你想稍后用列表做其他事情。


HTH,

Jim


2004年9月28日星期二09:47:32 +0000,Jim Sizelove写道:

Steven Bethard写道:

所以我需要做类似的事情:

我在范围内(len(l)):
for j in range (i + 1,len(l)):
#做某事(l [i],l [j])

我在列表中得到所有项目对(其中)我正在考虑成对,而不是元组,所以顺序并不重要)。这里的解决方案确实没有任何问题,但是由于Python的每个
构造都很好,我通常会尝试避免范围(len(..))类型
调用和列表索引当我可以...有没有办法做到这一点
没有索引
你是否试图将列表中的每个项目与其他
项目完全配对?也许这段代码可以做你想要的:



而len(L)> 0:
item1 = L.pop()
对于L中的item2: br /> print(item1,item2)




这看起来不错,只要item1将来的事实

向后退出的列表是可以的。


我会写''而L:''而不是你更复杂的测试。


def all_pairs(L) :

而L:

i = L.pop()

for L in L:yield i,j

list(all_pairs(range(5)))



[(4,0),(4,1),(4,2),(4,3),(3,0),(3,1),(3,2),(2,0), (2,1),(1,0)]


杰夫


-----开始PGP签名-----

版本:GnuPG v1.2.5(GNU / Linux)

iD8DBQFBWd8LJd01MZaTXX0RAtSuAJ9b1FjtfXQF / uAruhDukWlcA9ftCwCeJfm +

9r / rzibh0ssDsVjfVA7d51s =

= e1EA

----- END PGP SIGNATURE -----


< jepler< ;在> unpythonic.net>写道:

def all_pairs(L):
而L:
i = L.pop()
对于L中的j:yield i,j



有趣。我没有想到这个 - 除了

需要列表副本(因为我需要维护原始列表)之外,它还不错。


谢谢,


史蒂夫


So I need to do something like:

for i in range(len(l)):
for j in range(i+1, len(l)):
# do something with (l[i], l[j])

where I get all pairs of items in a list (where I''m thinking of pairs
as sets, not tuples, so order doesn''t matter). There isn''t really
anything wrong with the solution here, but since Python''s for-each
construction is so nice, I usually try to avoid range(len(..)) type
calls and list indexing when I can... Is there any way to do this
without indexing, e.g.:

for item1 in ...:
for item2 in ...:
# do something with (item1, item2)

I could do something like:

for i, item1 in enumerate(l):
for j in range(i+1, len(l)):
(item1, l[j])

but that only gets me halfway there... I also thought of something like:

for i, item1 in enumerate(l):
for item2 in l[i+1:]:
(item1, item2)

but that seems like a lot of wasteful list slicing...

Thanks in advance,

Steve
--
You can wordify anything if you just verb it.
- Bucky Katt, Get Fuzzy

解决方案

Steven Bethard wrote:

So I need to do something like:

for i in range(len(l)):
for j in range(i+1, len(l)):
# do something with (l[i], l[j])

where I get all pairs of items in a list (where I''m thinking of pairs
as sets, not tuples, so order doesn''t matter). There isn''t really
anything wrong with the solution here, but since Python''s for-each
construction is so nice, I usually try to avoid range(len(..)) type
calls and list indexing when I can... Is there any way to do this
without indexing



Are you trying to pair each item in a list with every other
item exactly once? Maybe this code does what you want:

while len(L)>0:
item1 = L.pop()
for item2 in L:
print (item1, item2)

pop() removes one item from the list. The inner for loop matches that
item with each of the remaining items. At the end of the outer loop the
list L is empty; you may want to run this loop over a copy of the list
if you want to do other things with the list later.

HTH,

Jim


On Tue, Sep 28, 2004 at 09:47:32PM +0000, Jim Sizelove wrote:

Steven Bethard wrote:

So I need to do something like:

for i in range(len(l)):
for j in range(i+1, len(l)):
# do something with (l[i], l[j])

where I get all pairs of items in a list (where I''m thinking of pairs
as sets, not tuples, so order doesn''t matter). There isn''t really
anything wrong with the solution here, but since Python''s for-each
construction is so nice, I usually try to avoid range(len(..)) type
calls and list indexing when I can... Is there any way to do this
without indexing
Are you trying to pair each item in a list with every other
item exactly once? Maybe this code does what you want:



while len(L)>0:
item1 = L.pop()
for item2 in L:
print (item1, item2)



This looks good, as long as the fact that the "item1"s will come
out of the list backwards is OK.

I''d write ''while L:'' instead of your more complicated test, though.

def all_pairs(L):
while L:
i = L.pop()
for j in L: yield i, j

list(all_pairs(range(5)))


[(4, 0), (4, 1), (4, 2), (4, 3), (3, 0), (3, 1), (3, 2), (2, 0), (2, 1), (1, 0)]

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBWd8LJd01MZaTXX0RAtSuAJ9b1FjtfXQF/uAruhDukWlcA9ftCwCeJfm+
9r/rzibh0ssDsVjfVA7d51s=
=e1EA
-----END PGP SIGNATURE-----


<jepler <at> unpythonic.net> writes:

def all_pairs(L):
while L:
i = L.pop()
for j in L: yield i, j



Interesting. I hadn''t thought of this one -- it''s not bad other than
requiring the list copy (since I need to maintain the original list).

Thanks,

Steve


这篇关于列表中没有索引的所有项目对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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