if语句测试2字符串但是没有输出?

时间:2012-07-23 21:02:46

标签: python regex if-statement

我的目标是尝试将字符串列表与整数列表进行匹配:

例如我的字符串列表:wholelookup[uniprotID] = [A177T,I126T,M418T]

我的整体列表:lookup[uniprotID] = [177,126,418]

如果有匹配,那么我想在wholelookup中打印令牌。

这是我到目前为止的结果,但结果并没有打印出任何内容:

for item in lookup[uniprotID]:
    for names in wholelookup[uniprotID]:
        if start <= item <= end and re.match(item, names) :
            item, start, end = map(int, (item, start, end))
            print names

2 个答案:

答案 0 :(得分:0)

match尝试从头开始匹配。另外:您不希望将1a321a匹配。

您可以使用're.match(r'\ w'+ str(item)+ r'\ w',名称)'而不是re.match(item, names)

或使用re.search

re.search(r'\d+',names).group(0)==item

答案 1 :(得分:0)

为什么要使用正则表达式?

假设startend已经是整数,而item是一个字符串,请尝试以下方法:

for item in lookup[uniprotID]:
    if start <= int(item) <= end: continue
    for names in wholelookup[uniprotID]:
        if str(item) in names :
            print names