[Tutor] longest common substring

lina lina.lastname at gmail.com
Sat Nov 12 10:01:16 CET 2011


<snip>

Sorry I finished last email in two different time,

while:
> INFILEEXT=".doc"
>
>
> def CommonSublist(L1, L2):
>    sublist=[]
>    sublists=[]
>    result=[]
>    M = [[0]*(1+len(L2)) for i in range(1+len(L1))]
>    longest, x_longest = 0, 0
>    for x in range(1,1+len(L1)):
>        for y in range(1,1+len(L2)):
>            if L1[x-1] == L2[y-1]:
>                M[x][y] = M[x-1][y-1]+1
>                if M[x][y] > longest:
>                    longest = M[x][y]
>                    x_longest = x
>                if longest >= 2:
>                    sublist=L1[x_longest-longest:x_longest]
>                    if sublist not in sublists:
>                         sublists.append(sublist)
>
>
>            else:
>                    M[x][y] = 0
>
>    return sublists
>
>
>
> if __name__=="__main__":
>
>
>    for i in range(1,11):
>        for j in range(1,11):
>            if i != j:
>                fileone="atom-pair_"+str(i)+".txt"
>                filetwo="atom-pair_"+str(j)+".txt"

correction: here not ".txt", it's ".doc"
>                a=open(fileone,"r").readline().strip().split(' ')
>                b=open(filetwo,"r").readline().strip().split(' ')
>                print(fileone,filetwo)
>                print(CommonSublist(a,b))
>
> The output results:
>
the output is:

atom-pair_10.doc atom-pair_8.doc
[['75', '64'], ['13', '64', '75'], ['64', '62', '75', '16']]
atom-pair_10.doc atom-pair_9.doc
[['65', '46'], ['13', '75', '64']]

seems a bit better than before.

> atom-pair_10.txt atom-pair_8.txt
> [["'75',", "'64',"], ["'13',", "'64',", "'75',"], ["'64',", "'62',",
> "'75',", "'16',"]]
> atom-pair_10.txt atom-pair_9.txt
> [["'65',", "'46',"], ["'13',", "'75',", "'64',"]]
>

> the $ python3 CommonSublists.py
> atom-pair_1.txt atom-pair_2.txt
> Traceback (most recent call last):
>  File "CommonSublists.py", line 47, in <module>
>    print(CommonSublist(a,b))
>  File "CommonSublists.py", line 24, in CommonSublist
>    result=result.append(sublist)
> AttributeError: 'NoneType' object has no attribute 'append'
>
> in local domain I set the result=[]
> I don't know why it complains its NoneType, since the "result" is
> nearly the same as "sublists".
>

Thanks with best regards,


More information about the Tutor mailing list