[Tutor] longest common substring
lina
lina.lastname at gmail.com
Sat Nov 12 16:24:20 CET 2011
On Sat, Nov 12, 2011 at 10:57 PM, Dave Angel <d at davea.name> wrote:
> On 11/12/2011 09:48 AM, lina wrote:
>>
>> 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.
>>>
>>>
>
> To keep it simple, I'm using three separate variables. d and a are as you
> tried to show above. Now what happens when I append?
>
> Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
> [GCC 4.5.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> d = []
>>>> a = [3, 5, 7]
>>>> xxx = d.append(a)
>>>> print(repr(xxx))
> None
>>>> print d
> [[3, 5, 7]]
>
> Notice that d does change as we expected. But xxx, the return value, is
> None. The append() method doesn't return any useful value, so don't assign
> it to anything.
Thanks, ^_^, now better.
I checked, the sublist (list) here can't be as a key of the results (dict).
actually I also wish to get the occurence of those sublist in the
script, except using external one in command line as uniq -c.
^_^ Have a nice weekend,
>
> The statement in your code that's wrong is
> result = result.append(sublist)
>
> The final value that goes into result is None, no matter what the earlier
> values of result and sublist were.
>
> --
>
> DaveA
>
More information about the Tutor
mailing list