[Tutor] longest common substring

lina lina.lastname at gmail.com
Sat Nov 12 15:48:16 CET 2011


On Sat, Nov 12, 2011 at 9:22 PM, Dave Angel <d at davea.name> wrote:
> On 11/12/2011 03:54 AM, lina wrote:
>>
>> <SNIP>
>> The one I tried :
>>                 if longest>= 2:
>>                     sublist=L1[x_longest-longest:x_longest]
>>                     result=result.append(sublist)
>>                     if sublist not in sublists:
>>                          sublists.append(sublist)
>>
>> 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".
>>
> Assuming this snippet is part of a loop, I see the problem:
>
> result  = result.append(sublist)
>
> list.append() returns none.  It modifies the list object in place, but it
> doesn't return anything.  So that statement modifies the result object,
> appending the sublist to it, then it sets it to None.  The second time
> around you see that error.

I am sorry.  haha ... still lack of understanding above sentence.

>>> a
['3', '5', '7', '8', '9']
>>> d.append(a)
>>> d
[['3', '5', '7', '8', '9']]
>>> type(a)
<class 'list'>

Sorry and thanks, best regards,

lina

>
> In general, most methods in the standard library either modify the object
> they're working on, OR they return something.   The append method is in the
> first category.
>
>
> --
>
> DaveA
>
>


More information about the Tutor mailing list