0

查询“AAAA-AACAAA-AAAAAA”

参考“AA-AATAAAAAAAATAAAAAA”

在 Python 中,如何修复相对于参考字符串的字符串(查询),其中查询中的破折号替换参考字符,并且参考字符串中的破折号导致相应查询字符中的删除?

“AAAAA-AAACAAA-AAAAAA”应该变成

“AAAATAAAACAAATAAAAAA”(此处的括号“AA()AA(T)AAACAAA(T)AAAAAA”突出显示修改后的字符)

下面是可以修复查询中相对于参考的破折号的代码,这可能有用也可能没有帮助(行号特定于文件,在这里不相关,我为非 pythonic 代码道歉!),但我无法修改根据参考文献中的破折号查询....

    if "Query identifier" in line:
        Query = line[24:-12]

        if "-" in Query:
            indices = [i for i, x in enumerate(Query) if x == "-"]
            QueryStringUntilFirstDash = Query[:indices[0]]
            found = 2

    if found ==2 and "Reference identifier" in line:
        Ref = line[24:-12]

        if len(indices) == 1:
            QueryDashToEnd.append(Query[indices[0]+1:])
            print QueryStringUntilFirstDash+Ref[indices[0]]+str(QueryDashToEnd[0])
            del(A[:])

        else:

            while y < len(indices):
                    y+=1

                    if y < len(indices):

                        DashesMiddleofQuery.append(Query[indices[y-1]:indices[y]])

                        DashesMiddleofQuerySubstitution = [B.replace('-', Ref[indices[y-1]]) for B in B]

                        Concat= ''.join(B)

            del(B[:])
            print UID
            print Beg+str(Concat)+Query[indices[-1]+1:]+">1"
            found = 0
            y = 0
4

1 回答 1

2

IIUC,这样的事情可能会起作用:

>>> query = "AAAAA-AAACAAA-AAAAAA"
>>> ref = "AA-AATAAAAAAATAAAAAA"
>>> fixed = ''.join(r if q == '-' else '' if r == '-' else q
...     for q,r in zip(query, ref))
>>> 
>>> fixed
'AAAATAAACAAATAAAAAA'

或者,如果您想将逻辑推送到函数中:

>>> def fixer(q,r):
...     if q == '-':
...         return r
...     if r == '-':
...         return ''
...     return q
... 
>>> fixed = ''.join(map(fixer, query, ref))
>>> fixed
'AAAATAAACAAATAAAAAA'

我认为从成对的字符以及直接处理这些字符而不是索引的角度来思考更容易。

于 2013-07-14T02:02:27.937 回答