How to align/match multiple strings in python

Viewed 233

I need some way to turn an input such as:

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

In the aligned/matched output:

['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']

I'm working on Python, if there is anything pre-maid that could help me with this I'd like to know. I saw some stuff of DNA sequencing that seemed like what I wanted to do, but I've never done anything like that and I got quite confused about.

Any kind of help is appreciated. Thanks.

1 Answers

This is how i got it solved:

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)

Result

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