比较两个列表并返回“位置”。 [英] comparing two lists and returning "position"

查看:65
本文介绍了比较两个列表并返回“位置”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有2个名单..为了简单起见,让我们说:


l1 = [''abc''''ghi''''mno' ']


l2 = [''abc''''def'''''ghi''''jkl''mno''''pqr'']


我需要做的是将l1与l2进行比较并返回位置

l1中的每个对象在l2中的位置


ie:pos = 0,2,4

提前致谢,-h

解决方案

< blockquote>在2007-06-22,hiro< Nu **** @ gmail.comwrote:


你好,我有2个列表..对于简单清酒可以说是:


l1 = [''abc''''ghi''''mno'']


l2 = [''abc''''def''''ghi''''jkl''mno''''pqr'']


我需要做的就是比较l1对l2并返回position

l1中的每个对象在l2中的位置


ie:pos = 0,2,4


提前致谢,-h



快来吧!你可以更努力地尝试。


-

Neil Cerutti


hiro< Nu **** @ gmail.com写:


我需要做的是将l1与l2进行比较并返回position

l1中的每个对象在l2中的位置


ie:pos = 0,2,4



是否已经是9月来自itertools导入izip的


pos = map(dict(izip(l2,count())).__ getitem __,l1)


Heh heh heh。


Paul Rubin写道:


>

来自itertools import izip

pos = map(dict(izip(l2,count())).__ getitem __,l1)



或可能效率较低......


>> l1 = [ ''abc'',''ghi'',''mno'']
l2 = [''abc'',''def'',''ghi'',''jkl'',''mno'',''pqr'']
pos = [l2。索引(i)for i in l1]
print pos



[0,2,4]


Charles


Hi there, I have a 2 lists.. for simplicities sake lets say the are:

l1 = [ ''abc'' ''ghi'' ''mno'' ]

l2 = [ ''abc'' ''def'' ''ghi'' ''jkl ''mno'' ''pqr'']

what I need to do is compare l1 against l2 and return the "position"
of where each object in l1 is in l2

ie: pos = 0, 2, 4
Thanks in advance, -h

解决方案

On 2007-06-22, hiro <Nu****@gmail.comwrote:

Hi there, I have a 2 lists.. for simplicities sake lets say the are:

l1 = [ ''abc'' ''ghi'' ''mno'' ]

l2 = [ ''abc'' ''def'' ''ghi'' ''jkl ''mno'' ''pqr'']

what I need to do is compare l1 against l2 and return the "position"
of where each object in l1 is in l2

ie: pos = 0, 2, 4

Thanks in advance, -h

Come, come! You can try harder than that.

--
Neil Cerutti


hiro <Nu****@gmail.comwrites:

what I need to do is compare l1 against l2 and return the "position"
of where each object in l1 is in l2

ie: pos = 0, 2, 4

Is it September already?

from itertools import izip
pos = map(dict(izip(l2, count())).__getitem__, l1)

Heh heh heh.


Paul Rubin wrote:

>
from itertools import izip
pos = map(dict(izip(l2, count())).__getitem__, l1)

or probably less efficiently ...

>>l1 = [ ''abc'', ''ghi'', ''mno'' ]
l2 = [ ''abc'', ''def'', ''ghi'', ''jkl'', ''mno'', ''pqr'']
pos = [ l2.index(i) for i in l1 ]
print pos

[0, 2, 4]

Charles


这篇关于比较两个列表并返回“位置”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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