比较两个列表并返回“位置”。 [英] comparing two lists and returning "position"
问题描述
你好,我有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, -hCome, 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, 4Is 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屋!