簡體   English   中英

如何在python中對齊/匹配多個字符串

[英]How to align/match multiple strings in python

我需要某種方式來打開輸入,例如:

['luv cats',  'lovv cots', 'lov cotts']

在對齊/匹配的輸出中:

['l','l','l']
['u','o','o']
[None,'v',None]
['v','v','v']
[' ',' ',' ']
['c','c','c']
['a','o','o']
['t','t','t']
[None,None,'t']
['s','s','s']

我正在研究 Python,如果有什么可以幫助我解決這個問題的前女仆,我想知道。 我看到了一些 DNA 測序的東西,這似乎是我想做的,但我從來沒有做過這樣的事情,我很困惑。

任何形式的幫助表示贊賞。 謝謝。

我是這樣解決的:

ls = ['luv cats',  'lovv cots', 'lov cotts']

lets = [list(set(a)) for a in ls]

lst1 = lets[0]
lst2 = lets[1]
lst3 = lets[2]
sub = [] #final result list
for i in lst1:
    a = [i]
    if i in lst2:
        a.append(i)
    else:
        a.append(None)
    if i in lst3:
        a.append(i)
    else:
        a.append(None)
    sub.append(a)

for j in lst2:
    b = 3*[0]
    b[1] = j
    if j in lst1:
        b[0] = j
    else:
        b[0] = None
    if j in lst3:
        b[2] = j
    else:
        b[2] = None
    if b not in sub:
        sub.append(b)

for j in lst3:
    c = 3*[0]
    c[2] = j
    if j in lst1:
        c[0] = j
    else:
        c[0] = None
    if j in lst3:
        c[1] = j
    else:
        c[1] = None
    if b not in sub:
        sub.append(c)

print(sub)

結果

[['u', None, None], ['l', 'l', 'l'], ['c', 'c', 'c'], ['v', 'v', 'v'], ['s', 's', 's'], [' ', ' ', ' '], ['t', 't', 't'], ['a', None, None], [None, 'o', 
'o']]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM