Python3:打印字典键,如果子字符串匹配值中的某个位置

时间:2017-11-01 11:15:14

标签: python python-3.x substring matching

如果字符串匹配字典中的任何值,我就成功检查了。字符串是值的子字符串(如果匹配)。匹配后,我将值的键(找到子字符串的位)附加到列表中。

在python中是否有任何方法可以引入类似if条件的内容,以执行以下操作:

"如果子字符串在值&#34中的位置60+(100)匹配,则仅附加到列表中;

到目前为止我的代码:

#!/usr/bin/python3
# imports
import re,sys
import itertools
# handling stdin
faFile=sys.argv[1]
pat=sys.argv[2]
pat2=sys.argv[3]
# opening .fa-File and building dictionary
with open (faFile) as f:
    # reading file-content line by line
    content=f.readlines()
    # stripping linebreaks and >
    content=[x.strip()for x in content]
    content=[x.strip('>') for x in content]
# building dictionary from content
# IDs are keys, sequences are values
d=dict(itertools.zip_longest(*[iter(content)]*2, fillvalue=""))

# motif- search function
def search (myDict, search1, search2):
    # initialize empty list to store found keys
    search.a=[]
    # iterating through dictionary
    for key, value in myDict.items():
        if search1 in value:
            if search2 in value:
                search.a.append(key)

search(d,pat,pat2)
print(search.a)

命令行调用:

./search.py file.fa pat pat2

我想过要介绍一个计数器,但还无法弄清楚

0 个答案:

没有答案