Python 在两组同步列表中查找匹配项
我有两组同步的列表,如下所示: (所谓同步,我的意思是cal中的“A”在CPO中属于12个,mal中的“A”在mpos中属于11个) set1 set2 我想在两个集合之间找到一个匹配,在这个例子中只有一个匹配,cpos&cal中的13T和mpos&mal中的13T 我编写了这个脚本,但它似乎只通过索引比较值,因为匹配字符串为空:Python 在两组同步列表中查找匹配项,python,Python,我有两组同步的列表,如下所示: (所谓同步,我的意思是cal中的“A”在CPO中属于12个,mal中的“A”在mpos中属于11个) set1 set2 我想在两个集合之间找到一个匹配,在这个例子中只有一个匹配,cpos&cal中的13T和mpos&mal中的13T 我编写了这个脚本,但它似乎只通过索引比较值,因为匹配字符串为空: mat = [] for i in xrange(len(cpos)): if mpos[i] == cpos[i] and mal[i] == cal[i
mat = []
for i in xrange(len(cpos)):
if mpos[i] == cpos[i] and mal[i] == cal[i]:
mat.append(cpos[i])
这就是我想要的:
mat = [13]
有什么办法解决这个问题吗
cpos = [12, 13, 14, 15]
cal = ['A', 'T', 'C', 'G']
mpos = [11, 12, 13, 16]
mal = ['A', 'T', 'T', 'G']
set1 = set(zip(cpos, cal))
set2 = set(zip(mpos, mal))
print set1 & set2
结果:
## set([(13, 'T')])
根据@Janne Karila的以下评论,以下内容将更有效:
from itertools import izip
print set(izip(cpos, cal)).intersection(izip(mpos, mal))
时间:
import timeit
repeat = 1
setup = '''
num = 1000000
import random
import string
from itertools import izip
cpos = [random.randint(1, 100) for x in range(num)]
cal = [random.choice(string.letters) for x in range(num)]
mpos = [random.randint(1, 100) for x in range(num)]
mal = [random.choice(string.letters) for x in range(num)]
'''
# izip: 0.38 seconds (Python 2.7.2)
t = timeit.Timer(
setup = setup,
stmt = '''set(izip(cpos, cal)).intersection(izip(mpos, mal))'''
)
print "%.2f second" % (t.timeit(number=repeat))
# zip: 0.53 seconds (Python 2.7.2)
t = timeit.Timer(
setup = setup,
stmt = '''set(zip(cpos, cal)) & set(zip(mpos, mal))'''
)
print "%.2f second" % (t.timeit(number=repeat))
# Nested loop: 616 seconds (Python 2.7.2)
t = timeit.Timer(
setup = setup,
stmt = '''
mat = []
for i in xrange(len(cpos)):
for j in xrange(len(mpos)):
if mpos[j] == cpos[i] and mal[j] == cal[i]:
mat.append(mpos[j]) # or mat.append((mpos[j], mal[j])) ?
break
'''
)
print "%.2f seconds" % (t.timeit(number=repeat))
结果:
## set([(13, 'T')])
根据@Janne Karila的以下评论,以下内容将更有效:
from itertools import izip
print set(izip(cpos, cal)).intersection(izip(mpos, mal))
时间:
import timeit
repeat = 1
setup = '''
num = 1000000
import random
import string
from itertools import izip
cpos = [random.randint(1, 100) for x in range(num)]
cal = [random.choice(string.letters) for x in range(num)]
mpos = [random.randint(1, 100) for x in range(num)]
mal = [random.choice(string.letters) for x in range(num)]
'''
# izip: 0.38 seconds (Python 2.7.2)
t = timeit.Timer(
setup = setup,
stmt = '''set(izip(cpos, cal)).intersection(izip(mpos, mal))'''
)
print "%.2f second" % (t.timeit(number=repeat))
# zip: 0.53 seconds (Python 2.7.2)
t = timeit.Timer(
setup = setup,
stmt = '''set(zip(cpos, cal)) & set(zip(mpos, mal))'''
)
print "%.2f second" % (t.timeit(number=repeat))
# Nested loop: 616 seconds (Python 2.7.2)
t = timeit.Timer(
setup = setup,
stmt = '''
mat = []
for i in xrange(len(cpos)):
for j in xrange(len(mpos)):
if mpos[j] == cpos[i] and mal[j] == cal[i]:
mat.append(mpos[j]) # or mat.append((mpos[j], mal[j])) ?
break
'''
)
print "%.2f seconds" % (t.timeit(number=repeat))
您现在仅按索引进行比较,即仅在所有列表中的位置
i
。但cpos和cal中的13T位于位置1
,mpos和mal中的13T位于位置2
。这意味着您的if语句将不为真,并且mat
将为空。您现在仅通过索引进行比较,即仅在所有列表中的i
位置进行比较。但cpos和cal中的13T位于位置1
,mpos和mal中的13T位于位置2
。这意味着您的if语句将不为true,mat
将为空。您可以在示例中添加第二个循环:
cpos = [12, 13, 14, 15]
cal = ['A', 'T', 'C', 'G']
mpos = [11, 12, 13, 16]
mal = ['A', 'T', 'T', 'G']
mat = []
for i in xrange(len(cpos)):
for j in xrange(len(mpos)):
if mpos[j] == cpos[i] and mal[j] == cal[i]:
mat.append(mpos[j]) # or mat.append((mpos[j], mal[j])) ?
print mat # [13]
…尽管这是非常低效的,如thg435答案中的计时所示您可以在示例中添加第二个循环:
cpos = [12, 13, 14, 15]
cal = ['A', 'T', 'C', 'G']
mpos = [11, 12, 13, 16]
mal = ['A', 'T', 'T', 'G']
mat = []
for i in xrange(len(cpos)):
for j in xrange(len(mpos)):
if mpos[j] == cpos[i] and mal[j] == cal[i]:
mat.append(mpos[j]) # or mat.append((mpos[j], mal[j])) ?
print mat # [13]
…虽然这是非常低效的,如thg435回答中的计时所示我考虑过,但我认为使用zip比不使用zip更耗时,我有一个巨大的文件,我想使用此过程,在没有PIP的情况下,没有简单的方法吗?@ EDG:在Python 2中考虑,这是懒惰版本的<代码> zip < /代码>,也可以使用<代码> SET1.交集(IZIP(MPOS,MAL))< /C> >我想,但是我认为使用ZIP比不使用Zip更耗时,我有一个巨大的文件,我想使用这个过程,没有简单的方法来做这个没有zip?@ EDG:在Python 2考虑这是懒惰版本的<代码>胡普< /COD>还,而不是构建SET2,你可以使用<代码> SET1.交集(IZIP(MPOS,MAL))< /代码>我知道,这就是我上面写的,这就是我想要解决的问题,也就是说,不按索引进行比较……我知道,这就是我上面写的,这就是我想要解决的问题,也就是说,不按索引进行比较……是否“位置”值必须排序?是否“位置”值必须排序?