On Sat, Nov 12, 2011 at 11:40 AM, Andreas Perstinger < andreas.perstin...@gmx.net> wrote:
> On 2011-11-12 16:24, lina wrote: > >> Thanks, ^_^, now better. >> > > No, I'm afraid you are still not understanding. > > > I checked, the sublist (list) here can't be as a key of the results >> (dict). >> > > "result" isn't a dictionary. It started as an empty list and later becomes > a null object ("NoneType"). > > You must not forget that you are inside a for-loop. Simplified your > situation is like this: > > >>> result = [] > >>> for i in range(1,10): > ... print("Iteration {0}, result = {1}".format(i, result)) > ... result = result.append(i) > ... > Iteration 1, result = [] > Iteration 2, result = None > > Traceback (most recent call last): > File "<stdin>", line 3, in <module> > > AttributeError: 'NoneType' object has no attribute 'append' > > As you see the error happens in the *second* iteration, because result is > no list any more. > Dave gave you already the explanation: functions and method always return > a value in Python. If the don't have a return statement they return "None". > > Another simple example: > > >>> a = print("Test") > Test > > "print" is a function which prints out the text you passed to it and you > usually aren't interested in its return value. But every function/method in > Python returns something. You save this value in "a" > > >>> print(a) > None > > As you see the return value of "print" is "None". > > >>> a.append(x) > > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > > AttributeError: 'NoneType' object has no attribute 'append' > > Same error as above, because "NoneType" objects (null objects) don't have > a method "append". > > I also think you mix two different ways to add an element to a list: > > result.append(x) > > is equivalent to > > result = result + [x] (that's what you will use in other languages) > > HTH, Andreas > > ______________________________**_________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor> > This is a fascinating thread in the same way that people can't help slowing down and looking at a car crash on the side of the road is fascinating. The original poster it seems is new to programming and has offered that he likes to run before he learns to walk. That doesn't seem like a good proclamation to make when you are asking people to help you. Anyway, this particular piece of code is pretty tricky stuff. It involves understanding list comprehensions, multidimensional lists, and slices. All things that take more than a passing interest in to grasp. Furthermore, the algorithm itself is pretty tricky. If you follow the link to the wikipedia article: http://en.wikipedia.org/wiki/Longest_common_substring you learn that understanding the algorithm requires understanding of trees (Generalized suffix trees at that!). I copied the code from the original article and played around with it for an hour or so to understand it. I didn't get the answers that I expected either. If you are learning coding in general and python in particular this exercise seems unproductive. Work through basic concepts. If you don't like one set of tutorials, find a different one. if you don't like to read, check out google videos for learning python, or youtube for that matter. But taking on a concise algorithm that solves a problem that would challenge a 3rd year CS student doesn't seem like a good idea -- Joel Goldstick
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor