[Tutor] recursive problem
Hello, I have this : def recursive_count(target, nested_num_list): """ >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) 4 >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) 2 >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) 0 >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) 6 """ for element in nested_num_list: if type(element) == type([]): test = recursive_count(target, element) print element, target if element == target : count = count + 1 return count if __name__ == "__main__": import doctest doctest.testmod() Now I get this message : UnboundLocalError: local variable 'count' referenced before assignment But if I do this : def recursive_count(target, nested_num_list): """ >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) 4 >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) 2 >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) 0 >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) 6 """ count = 0 for element in nested_num_list: if type(element) == type([]): test = recursive_count(target, element) print element, target if element == target : count = count + 1 return count if __name__ == "__main__": import doctest doctest.testmod() The count will always be 0 if a nested list is being found. What's the python way to solve this Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
On Thu, Sep 9, 2010 at 2:21 PM, Roelof Wobben wrote: > Hello, > > I have this : > > def recursive_count(target, nested_num_list): > """ > >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) > 4 > >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) > 2 > >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) > 0 > >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) > 6 > """ > for element in nested_num_list: > if type(element) == type([]): >test = recursive_count(target, element) > print element, target > if element == target : >count = count + 1 > return count > > if __name__ == "__main__": > import doctest > doctest.testmod() > > > Now I get this message : > > UnboundLocalError: local variable 'count' referenced before assignment > It is because you are doing count = count + 1 But where is count defined. > > But if I do this : > > def recursive_count(target, nested_num_list): > """ > >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) > 4 > >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) > 2 > >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) > 0 > >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) > 6 > """ > count = 0 > for element in nested_num_list: > if type(element) == type([]): >test = recursive_count(target, element) > print element, target > if element == target : >count = count + 1 > return count > > if __name__ == "__main__": > import doctest > doctest.testmod() > > The count will always be 0 if a nested list is being found. > > What's the python way to solve this > I am not sure what do you want to achieve by this ? What is the problem statement ? > > Roelof > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- ~l0nwlf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
From: anand.shash...@gmail.com Date: Thu, 9 Sep 2010 15:08:10 +0530 Subject: Re: [Tutor] recursive problem To: rwob...@hotmail.com CC: tutor@python.org On Thu, Sep 9, 2010 at 2:21 PM, Roelof Wobben wrote: Hello, I have this : def recursive_count(target, nested_num_list): """ >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) 4 >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) 2 >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) 0 >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) 6 """ for element in nested_num_list: if type(element) == type([]): test = recursive_count(target, element) print element, target if element == target : count = count + 1 return count if __name__ == "__main__": import doctest doctest.testmod() Now I get this message : UnboundLocalError: local variable 'count' referenced before assignment It is because you are doing count = count + 1 But where is count defined. But if I do this : def recursive_count(target, nested_num_list): """ >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) 4 >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) 2 >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) 0 >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) 6 """ count = 0 for element in nested_num_list: if type(element) == type([]): test = recursive_count(target, element) print element, target if element == target : count = count + 1 return count if __name__ == "__main__": import doctest doctest.testmod() The count will always be 0 if a nested list is being found. What's the python way to solve this I am not sure what do you want to achieve by this ? What is the problem statement ? The problem statement is that I must count how many times the target is in the nested_list. So I thougt that every nested list the function is called again so this list is also iterated. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
On Thu, Sep 9, 2010 at 3:11 PM, Roelof Wobben wrote: > > > -- > From: anand.shash...@gmail.com > Date: Thu, 9 Sep 2010 15:08:10 +0530 > Subject: Re: [Tutor] recursive problem > To: rwob...@hotmail.com > CC: tutor@python.org > > > > On Thu, Sep 9, 2010 at 2:21 PM, Roelof Wobben wrote: > > Hello, > > I have this : > > def recursive_count(target, nested_num_list): > """ > >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) > 4 > >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) > 2 > >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) > 0 > >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) > 6 > """ > for element in nested_num_list: > if type(element) == type([]): >test = recursive_count(target, element) > print element, target > if element == target : >count = count + 1 > return count > > if __name__ == "__main__": > import doctest > doctest.testmod() > > > Now I get this message : > > UnboundLocalError: local variable 'count' referenced before assignment > > > It is because you are doing count = count + 1 > But where is count defined. > > > > But if I do this : > > def recursive_count(target, nested_num_list): > """ > >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) > 4 > >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) > 2 > >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) > 0 > >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) > 6 > """ > count = 0 > for element in nested_num_list: > if type(element) == type([]): >test = recursive_count(target, element) > print element, target > if element == target : >count = count + 1 > return count > > if __name__ == "__main__": > import doctest > doctest.testmod() > > The count will always be 0 if a nested list is being found. > > What's the python way to solve this > > > I am not sure what do you want to achieve by this ? > What is the problem statement ? > > The problem statement is that I must count how many times the target is in > the nested_list. > So I thougt that every nested list the function is called again so this > list is also iterated. > Let's say n, l = 2, [2, 9, [2, 1, 13, 2], 8, [2, 6]] Simply Flatten the list l, which will be then be; flatten(l) = [ 2, [2, 9, 2, 1,13, 2, 8, 2, 6 ] Now count for n; flatten.count(n) -- ~l0nwlf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
On 9 September 2010 12:59, Shashwat Anand wrote: > > Let's say n, l = 2, [2, 9, [2, 1, 13, 2], 8, [2, 6]] > Simply Flatten the list l, which will be then be; flatten(l) = [ 2, [2, 9, > 2, 1,13, 2, 8, 2, 6 ] > Now count for n; flatten.count(n) > > This is fine except that the excercise probably has to do with introducing recursion and scoping, which is unfortunately defeated by this solution. Roelof, look at what you do with the result of the recursive call which you currently assign to test and otherwise ignore -- you want to add this to count and/or return it... Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
On Thu, Sep 9, 2010 at 7:59 AM, Shashwat Anand wrote: > > > On Thu, Sep 9, 2010 at 3:11 PM, Roelof Wobben wrote: > >> >> >> -- >> From: anand.shash...@gmail.com >> Date: Thu, 9 Sep 2010 15:08:10 +0530 >> Subject: Re: [Tutor] recursive problem >> To: rwob...@hotmail.com >> CC: tutor@python.org >> >> >> >> On Thu, Sep 9, 2010 at 2:21 PM, Roelof Wobben wrote: >> >> Hello, >> >> I have this : >> >> def recursive_count(target, nested_num_list): >> """ >> >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) >> 4 >> >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) >> 2 >> >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) >> 0 >> >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) >> 6 >> """ >> for element in nested_num_list: >> if type(element) == type([]): >>test = recursive_count(target, element) >> print element, target >> if element == target : >>count = count + 1 >> return count >> >> if __name__ == "__main__": >> import doctest >> doctest.testmod() >> >> What you are trying to do is walk through the list. If you get a value and not another list, check if it is the value you are counting. If it is, added it to your count. When you are done walking, return your count. If you get another list, walk through the list > So here is my stab at the code >> >> 14 count = 0 # set your count to 0 15 for element in nested_num_list: # walk through each element 16 if type(element) == type([]): 17 count += recursive_count(target, element) # since its another list start anew and add the count result to what you already have 18 elif element == target: # its a value, so check if its YOUR value, and if it is, increment your counter 19 count += 1 20 #print element, count 21 return count # must be done. You get here each time you walk thru a list 22 > >> Now I get this message : >> >> UnboundLocalError: local variable 'count' referenced before assignment >> >> >> It is because you are doing count = count + 1 >> But where is count defined. >> >> >> >> But if I do this : >> >> def recursive_count(target, nested_num_list): >> """ >> >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) >> 4 >> >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) >> 2 >> >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) >> 0 >> >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) >> 6 >> """ >> count = 0 >> for element in nested_num_list: >> if type(element) == type([]): >>test = recursive_count(target, element) >> print element, target >> if element == target : >>count = count + 1 >> return count >> >> if __name__ == "__main__": >> import doctest >> doctest.testmod() >> >> The count will always be 0 if a nested list is being found. >> >> What's the python way to solve this >> >> >> I am not sure what do you want to achieve by this ? >> What is the problem statement ? >> >> The problem statement is that I must count how many times the target is in >> the nested_list. >> So I thougt that every nested list the function is called again so this >> list is also iterated. >> > > Let's say n, l = 2, [2, 9, [2, 1, 13, 2], 8, [2, 6]] > Simply Flatten the list l, which will be then be; flatten(l) = [ 2, [2, 9, > 2, 1,13, 2, 8, 2, 6 ] > Now count for n; flatten.count(n) > > > > -- > ~l0nwlf > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
Shouldn't there be a way to do this without type checking? Duck typing! Sent from my iPhone On Sep 9, 2010, at 7:33 AM, Joel Goldstick wrote: > > > On Thu, Sep 9, 2010 at 7:59 AM, Shashwat Anand > wrote: > > > On Thu, Sep 9, 2010 at 3:11 PM, Roelof Wobben wrote: > > > From: anand.shash...@gmail.com > Date: Thu, 9 Sep 2010 15:08:10 +0530 > Subject: Re: [Tutor] recursive problem > To: rwob...@hotmail.com > CC: tutor@python.org > > > > On Thu, Sep 9, 2010 at 2:21 PM, Roelof Wobben wrote: > Hello, > > I have this : > > def recursive_count(target, nested_num_list): > """ > >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) > 4 > >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) > 2 > >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) > 0 > >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) > 6 > """ > for element in nested_num_list: > if type(element) == type([]): >test = recursive_count(target, element) > print element, target > if element == target : >count = count + 1 > return count > > if __name__ == "__main__": > import doctest > doctest.testmod() > > What you are trying to do is walk through the list. If you get a value and > not another list, check if it is the value you are counting. If it is, added > it to your count. > When you are done walking, return your count. If you get another list, walk > through the list > So here is my stab at the code > 14 count = 0 > # set your count to 0 > 15 for element in nested_num_list: > # walk through each element > 16 if type(element) == type([]): > 17 count += recursive_count(target, element) # > since its another list start anew and add the count result to what you > already have > 18 elif element == target: > # its a value, so check if its YOUR value, and if it is, increment > your counter > 19 count += 1 > 20 #print element, count > 21 return count ># must be done. You get here each time you walk thru a list > 22 > > > > Now I get this message : > > UnboundLocalError: local variable 'count' referenced before assignment > > It is because you are doing count = count + 1 > But where is count defined. > > > But if I do this : > > def recursive_count(target, nested_num_list): > """ > >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]]) > 4 > >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]]) > 2 > >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]]) > 0 > >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]]) > 6 > """ > count = 0 > for element in nested_num_list: > if type(element) == type([]): >test = recursive_count(target, element) > print element, target > if element == target : >count = count + 1 > return count > > if __name__ == "__main__": > import doctest > doctest.testmod() > > The count will always be 0 if a nested list is being found. > > What's the python way to solve this > > I am not sure what do you want to achieve by this ? > What is the problem statement ? > > The problem statement is that I must count how many times the target is in > the nested_list. > So I thougt that every nested list the function is called again so this list > is also iterated. > > Let's say n, l = 2, [2, 9, [2, 1, 13, 2], 8, [2, 6]] > Simply Flatten the list l, which will be then be; flatten(l) = [ 2, [2, 9, 2, > 1,13, 2, 8, 2, 6 ] > Now count for n; flatten.count(n) > > > > -- > ~l0nwlf > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > > -- > Joel Goldstick > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
On Thu, Sep 9, 2010 at 10:26 AM, Luke Paireepinart wrote: > Shouldn't there be a way to do this without type checking? Duck typing! > > Your post got me thinking. Maybe better to test if the object can return an iter method. If it throws an error, then look at its value. If it doesn't, then its a list or a tuple ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
On 09/09/2010 17.05, Joel Goldstick wrote: On Thu, Sep 9, 2010 at 10:26 AM, Luke Paireepinart mailto:rabidpoob...@gmail.com>> wrote: Shouldn't there be a way to do this without type checking? Duck typing! Your post got me thinking. Maybe better to test if the object can return an iter method. If it throws an error, then look at its value. If it doesn't, then its a list or a tuple ... Oh, my ... I think type checking is simpler. Maybe not that duckly, but... Francesco Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.851 / Database dei virus: 271.1.1/3121 - Data di rilascio: 09/08/10 08:07:00 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
On Fri, 10 Sep 2010 01:05:22 am Joel Goldstick wrote: > On Thu, Sep 9, 2010 at 10:26 AM, Luke Paireepinart > > wrote: > > Shouldn't there be a way to do this without type checking? Duck > > typing! > > > > Your post got me thinking. Maybe better to test if the object can > > return > an iter method. If it throws an error, then look at its value. If > it doesn't, then its a list or a tuple It's not clear what you mean by "return an iter method". Taken literally, that would imply the object is a function. I think you mean "*has* an iter method" -- except that's not right either: >>> [].iter Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute 'iter' Perhaps you mean an object which can be passed to iter(), but lots of objects can do that, not just lists and tuples: >>> iter("not a list or tuple") >>> iter({1: None, 2: "a", 4: 5}) -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
On Thu, Sep 9, 2010 at 12:07 PM, Steven D'Aprano wrote: > On Fri, 10 Sep 2010 01:05:22 am Joel Goldstick wrote: > > On Thu, Sep 9, 2010 at 10:26 AM, Luke Paireepinart > > > > wrote: > > > Shouldn't there be a way to do this without type checking? Duck > > > typing! > > > > > > Your post got me thinking. Maybe better to test if the object can > > > return > > an iter method. If it throws an error, then look at its value. If > > it doesn't, then its a list or a tuple > > > It's not clear what you mean by "return an iter method". Taken > literally, that would imply the object is a function. I think you > mean "*has* an iter method" -- except that's not right either: > > >>> [].iter > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'list' object has no attribute 'iter' > > > Perhaps you mean an object which can be passed to iter(), but lots of > objects can do that, not just lists and tuples: > > >>> iter("not a list or tuple") > > >>> iter({1: None, 2: "a", 4: 5}) > > > > I was googling, and found that if an object has an __iter__ method it will > return it. If not it will throw an error. You are right about more than > lists and tuples being iterable. But, in this thread, it was brought up > that checking type may not be pythonic. If you wanted to use the same code > to find values in a nested list of any objects, you could dispense with the > type checking and just see if it is iterable. > I'm new to python. Am I off base? > > > -- > Steven D'Aprano > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive problem
Nope Joel, that's what I meant. Remember EAFP over LBYL! I'm not sure the best way to make sure the object is iterable though. Sent from my iPhone On Sep 9, 2010, at 11:41 AM, Joel Goldstick wrote: > > > On Thu, Sep 9, 2010 at 12:07 PM, Steven D'Aprano wrote: > On Fri, 10 Sep 2010 01:05:22 am Joel Goldstick wrote: > > On Thu, Sep 9, 2010 at 10:26 AM, Luke Paireepinart > > > > wrote: > > > Shouldn't there be a way to do this without type checking? Duck > > > typing! > > > > > > Your post got me thinking. Maybe better to test if the object can > > > return > > an iter method. If it throws an error, then look at its value. If > > it doesn't, then its a list or a tuple > > > It's not clear what you mean by "return an iter method". Taken > literally, that would imply the object is a function. I think you > mean "*has* an iter method" -- except that's not right either: > > >>> [].iter > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'list' object has no attribute 'iter' > > > Perhaps you mean an object which can be passed to iter(), but lots of > objects can do that, not just lists and tuples: > > >>> iter("not a list or tuple") > > >>> iter({1: None, 2: "a", 4: 5}) > > > > I was googling, and found that if an object has an __iter__ method it will > return it. If not it will throw an error. You are right about more than > lists and tuples being iterable. But, in this thread, it was brought up that > checking type may not be pythonic. If you wanted to use the same code to > find values in a nested list of any objects, you could dispense with the type > checking and just see if it is iterable. > > I'm new to python. Am I off base? > > > -- > Steven D'Aprano > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > -- > Joel Goldstick > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python social network / cms components?
Hi, After much research, I've come up with a list of what I think might be the best way of putting together a Python based social network/cms, but have some questions about how some of these components fit together. Before I ask about the particular components, here are some of the key features of the site to be built: - a modern almost desktop-like gui - future ability to host an advanced html5 sub-application (ex.http://www.lucidchart.com) - high scalability both for functionality and user load - user ability to password protect and permission manage content on per item/group basis - typical social network features - ability to build a scaled down mobile version in the future Here's the list of tools I'm considering using: Google App Engine Python Django Pinax Pyjamas wxPython And the questions: 1. Google App Engine -- this is an attempt to cut to the chase as many pieces of the puzzle seem to be in place. Question: Am I limiting my options with this choice? Example: datastore not being relational? Should I wait for SQL support under the Business version? 2. Python -- I considered 'drupal' at first, but in the end decided that being dependent on modules that may or may not exist tomorrow + limitations of its templating system are a no-no. Learning its API, too, would be useless elsewhere whereas Python seems like a swiss army knife of languages -- good for almost anything. Question: v.2.5.2 is required by GAE, but python.org recommends 2.5.5. Which do I install? 3. Django -- v.0.96 is built into GAE. You seem to be able to upgrade it. Questions: Any reason not to upgrade to the latest version? Ways to get around the lack of HTML5 support? 4. Pinax (http://pinaxproject.com) Rides on top of Django and appears to provide most of the social network functionality anyone would want. Question: Reasons NOT to use it? Alternatives? 5. Pyjamas and wxPython -- this is the part that gets a little confusing. The basic idea behind these is the ability to build a GUI. I've considered Silverlight and Flash, before the GAE/Python route, but a few working versions of HTML5 apps convinced me that enough of it ALREADY runs on the latest batch of browsers to chose the HTML5/Javascript route instead. Question: How do I extend/supplement Python/Django to build an app-like HTML5 interface? Are Pyjamas and wxPython the way to go? Or should I change my thinking completely? Answers to some/any of these questions would be of great help. Please excuse my ignorance if any of this doesn't make much sense. My last venture into web programming was a decent sized LAMP website some 5-6 years ago. On the desktop side of things, my programming experience boils down to very high level scripting languages that I keep on learning to accomplish very specific tasks :) Thank you all, - igor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Random list exercise
Hi tutors, Still on my Python learning journey! I've just competed an exercise which asks the student to "Create a program that creates a list of words in random order. This program should print all the words and not repeat any." I've printed the list for my own needs. The list randwords aims to answer the specific request of the exercise author. If anyone has the inclination and a minute to spare, please run your eyes over my answer. It works, but is it an OK way to approach the exercise? Thanks again :-D Chris import random #LIST words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven"] randwords = [] while words: #has entries in it wordslen = len(words) #get the length of the list index = random.randint(0, wordslen -1) #get a random index chosenword = words[index] randwords.append(chosenword) #append the random word to a new list del words[index] #del the word from the old list for word in randwords: print word # print them ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random list exercise
On Thu, Sep 9, 2010 at 4:51 PM, lists wrote: > Hi tutors, > > Still on my Python learning journey! I've just competed an exercise > which asks the student to "Create a program that creates a list of > words in random order. This program should print all the words and not > repeat any." I've printed the list for my own needs. The list > randwords aims to answer the specific request of the exercise author. > > If anyone has the inclination and a minute to spare, please run your > eyes over my answer. It works, but is it an OK way to approach the > exercise? > > Thanks again :-D > > Chris > > import random > > #LIST > words = ["one", "two", "three", "four", "five", "six", "seven", > "eight", "nine", "ten", "eleven"] > randwords = [] > > while words: #has entries in it >wordslen = len(words) #get the length of the list >index = random.randint(0, wordslen -1) #get a random index >chosenword = words[index] >randwords.append(chosenword) #append the random word to a new list >del words[index] #del the word from the old list > > for word in randwords: >print word # print them > Several small and not so small points: 1. you assign wordslen each pass through your loop. While it doesn't matter in a small loop, it wastes time on the order of the size of your list. Instead move wordslen = len(... above your while loop. Any time you put code in a loop that doesn't change in each iteration, you should move it out of the loop. 2. since you only use chosenword once in your loop, you could remove chosenword = words[index] line and replace chosenword in the append(... with words[index] 3. your list doesn't contain any duplicate words. Since your program is supposed to catch this, you should add a duplicate to see if it works. (No!) 4. I think your line del words[index] is supposed to help out with item 3 but it doesn't. It just removes the word you just used selected. 5. And finally, I think you want to print Just minor points. nice job -- getting there -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random list exercise
On Thu, Sep 9, 2010 at 5:42 PM, Joel Goldstick wrote: > > > On Thu, Sep 9, 2010 at 4:51 PM, lists wrote: > >> Hi tutors, >> >> Still on my Python learning journey! I've just competed an exercise >> which asks the student to "Create a program that creates a list of >> words in random order. This program should print all the words and not >> repeat any." I've printed the list for my own needs. The list >> randwords aims to answer the specific request of the exercise author. >> >> If anyone has the inclination and a minute to spare, please run your >> eyes over my answer. It works, but is it an OK way to approach the >> exercise? >> >> Thanks again :-D >> >> Chris >> >> import random >> >> #LIST >> words = ["one", "two", "three", "four", "five", "six", "seven", >> "eight", "nine", "ten", "eleven"] >> randwords = [] >> >> while words: #has entries in it >>wordslen = len(words) #get the length of the list >>index = random.randint(0, wordslen -1) #get a random index >>chosenword = words[index] >>randwords.append(chosenword) #append the random word to a new list >>del words[index] #del the word from the old list >> >> for word in randwords: >>print word # print them >> > > Several small and not so small points: > > 1. you assign wordslen each pass through your loop. While it doesn't > matter in a small loop, it wastes time on the order of the size of your > list. Instead move wordslen = len(... above your while loop. Any time you > put code in a loop that doesn't change in each iteration, you should move it > out of the loop. > > 2. since you only use chosenword once in your loop, you could remove > chosenword = words[index] line and replace chosenword in the append(... with > words[index] > > 3. your list doesn't contain any duplicate words. Since your program is > supposed to catch this, you should add a duplicate to see if it works. > (No!) > > 4. I think your line del words[index] is supposed to help out with item 3 > but it doesn't. It just removes the word you just used selected. > > 5. And finally, I think you want to print > > Just minor points. nice job -- getting there > -- > Joel Goldstick > > I looked into this a little more, and although I'm ok with my comments, I did some experimenting and looked into the stuff in random. I came up with this. It may not be helpful since you may be learning about loops and randint, but this works: import random #LIST words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "four", "nine", "ten", "eleven"] word_set = list(set(words))# this removes duplicates since set contains one of each value. Then convert back to a list print "original words: ", words# just wanted to see my original list print "removed duplicates with set:", word_set # this shows that I don't have duplicates random.shuffle(word_set) # this shuffles the list in place. Now word_set is shuffled (randomized!) print "randomized: ", word_set # see! -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random list exercise
> > > On Thu, Sep 9, 2010 at 4:51 PM, lists wrote: >> >> Hi tutors, >> >> Still on my Python learning journey! I've just competed an exercise >> which asks the student to "Create a program that creates a list of >> words in random order. This program should print all the words and not >> repeat any." I've printed the list for my own needs. The list >> randwords aims to answer the specific request of the exercise author. >> >> If anyone has the inclination and a minute to spare, please run your >> eyes over my answer. It works, but is it an OK way to approach the >> exercise? >> >> Thanks again :-D >> >> Chris >> >> import random >> >> #LIST >> words = ["one", "two", "three", "four", "five", "six", "seven", >> "eight", "nine", "ten", "eleven"] >> randwords = [] >> >> while words: #has entries in it >> wordslen = len(words) #get the length of the list >> index = random.randint(0, wordslen -1) #get a random index >> chosenword = words[index] >> randwords.append(chosenword) #append the random word to a new list >> del words[index] #del the word from the old list >> >> for word in randwords: >> print word # print them > > Several small and not so small points: > > 1. you assign wordslen each pass through your loop. While it doesn't matter > in a small loop, it wastes time on the order of the size of your list. > Instead move wordslen = len(... above your while loop. Any time you put > code in a loop that doesn't change in each iteration, you should move it out > of the loop. > > 2. since you only use chosenword once in your loop, you could remove > chosenword = words[index] line and replace chosenword in the append(... with > words[index] > > 3. your list doesn't contain any duplicate words. Since your program is > supposed to catch this, you should add a duplicate to see if it works. > (No!) > > 4. I think your line del words[index] is supposed to help out with item 3 > but it doesn't. It just removes the word you just used selected. > > 5. And finally, I think you want to print > > Just minor points. nice job -- getting there > -- > Joel Goldstick > Ah, back to the drawing boards! I'll fix and post back! Chris ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random list exercise
>>> >>> Hi tutors, >>> >>> Still on my Python learning journey! I've just competed an exercise >>> which asks the student to "Create a program that creates a list of >>> words in random order. This program should print all the words and not >>> repeat any." I've printed the list for my own needs. The list >>> randwords aims to answer the specific request of the exercise author. >>> >>> If anyone has the inclination and a minute to spare, please run your >>> eyes over my answer. It works, but is it an OK way to approach the >>> exercise? >>> >>> Thanks again :-D >>> >>> Chris >>> >>> import random >>> >>> #LIST >>> words = ["one", "two", "three", "four", "five", "six", "seven", >>> "eight", "nine", "ten", "eleven"] >>> randwords = [] >>> >>> while words: #has entries in it >>> wordslen = len(words) #get the length of the list >>> index = random.randint(0, wordslen -1) #get a random index >>> chosenword = words[index] >>> randwords.append(chosenword) #append the random word to a new list >>> del words[index] #del the word from the old list >>> >>> for word in randwords: >>> print word # print them >> >> Several small and not so small points: >> >> 1. you assign wordslen each pass through your loop. While it doesn't >> matter in a small loop, it wastes time on the order of the size of your >> list. Instead move wordslen = len(... above your while loop. Any time you >> put code in a loop that doesn't change in each iteration, you should move it >> out of the loop. >> >> 2. since you only use chosenword once in your loop, you could remove >> chosenword = words[index] line and replace chosenword in the append(... with >> words[index] >> >> 3. your list doesn't contain any duplicate words. Since your program is >> supposed to catch this, you should add a duplicate to see if it works. >> (No!) >> >> 4. I think your line del words[index] is supposed to help out with item 3 >> but it doesn't. It just removes the word you just used selected. >> >> 5. And finally, I think you want to print >> >> Just minor points. nice job -- getting there >> -- >> Joel Goldstick >> > > > I looked into this a little more, and although I'm ok with my comments, I > did some experimenting and looked into the stuff in random. I came up with > this. It may not be helpful since you may be learning about loops and > randint, but this works: > > import random > > #LIST > words = ["one", "two", "three", "four", "five", "six", "seven", > "eight", "four", "nine", "ten", "eleven"] > > word_set = list(set(words)) # this removes duplicates since set contains > one of each value. Then convert back to a list > print "original words: ", words # just wanted to see my original list > print "removed duplicates with set:", word_set # this shows that I don't > have duplicates > random.shuffle(word_set) # this shuffles the list in place. Now word_set > is shuffled (randomized!) > print "randomized: ", word_set # see! > > Hey Joel, I've not done sets yet. I guess part of the problem when you're a noob like myself is that there is always going to be a more efficient way of doing something (but just that I haven't learnt it yet) :-D Looking at the original code again, I think that the wordslen = len(words) bit might have to stay in the loop. The value of wordslen changes at each iteration of the loop you see. The reason I'm removing one entry from the words list at each iteration is to ensure that all of the words are included in randwords (although again, this probably isn't the best way to do it!). I think I may need to add something like if words[index] not in randwords: to make sure that the word isn't already in the list I want to jumble up' and print perhaps? Thanks Chris Chris ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random list exercise
>>> Several small and not so small points: >>> >>> 1. you assign wordslen each pass through your loop. While it doesn't >>> matter in a small loop, it wastes time on the order of the size of your >>> list. Instead move wordslen = len(... above your while loop. Any time you >>> put code in a loop that doesn't change in each iteration, you should move it >>> out of the loop. >>> >>> 2. since you only use chosenword once in your loop, you could remove >>> chosenword = words[index] line and replace chosenword in the append(... with >>> words[index] >>> >>> 3. your list doesn't contain any duplicate words. Since your program is >>> supposed to catch this, you should add a duplicate to see if it works. >>> (No!) >>> >>> 4. I think your line del words[index] is supposed to help out with item 3 >>> but it doesn't. It just removes the word you just used selected. >>> >>> 5. And finally, I think you want to print >>> >>> Just minor points. nice job -- getting there >>> -- >>> Joel Goldstick This seems to work Joel, It removes the extraneous assignment of chosenword and gets rid of duplicate entries from the word list. while words: #has entries in it wordslen = len(words) #get the length of the list index = random.randint(0, wordslen -1) #get a random index if words[index] not in randwords: #as long as the word isn't already in the new list randwords.append(words[index]) #append the random word to a new list del words[index] #del the word from the old list else: del words[index] #remove the duplicate word from the source list ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mutable Properties
sorry, accidentally hit reply instead of reply to all On Thu, Sep 9, 2010 at 8:42 AM, Steven D'Aprano wrote: > Please don't reply privately to me unless you mean to ask me something > private or personal. > > If you send your reply to the tutor list, I'll respond there. > > > Regards, > > > > -- > Steven D'Aprano > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random list exercise
forgot to send it to the list On Thu, Sep 9, 2010 at 9:58 PM, Christopher King wrote: > you could try random.shuffle and save a lot of time, it takes a mutable > sequence (like a list) and shuffles it > > > On Thu, Sep 9, 2010 at 6:39 PM, lists wrote: > >> >>> Several small and not so small points: >> >>> >> >>> 1. you assign wordslen each pass through your loop. While it doesn't >> >>> matter in a small loop, it wastes time on the order of the size of >> your >> >>> list. Instead move wordslen = len(... above your while loop. Any >> time you >> >>> put code in a loop that doesn't change in each iteration, you should >> move it >> >>> out of the loop. >> >>> >> >>> 2. since you only use chosenword once in your loop, you could remove >> >>> chosenword = words[index] line and replace chosenword in the >> append(... with >> >>> words[index] >> >>> >> >>> 3. your list doesn't contain any duplicate words. Since your program >> is >> >>> supposed to catch this, you should add a duplicate to see if it works. >> >>> (No!) >> >>> >> >>> 4. I think your line del words[index] is supposed to help out with >> item 3 >> >>> but it doesn't. It just removes the word you just used selected. >> >>> >> >>> 5. And finally, I think you want to print >> >>> >> >>> Just minor points. nice job -- getting there >> >>> -- >> >>> Joel Goldstick >> >> This seems to work Joel, >> >> It removes the extraneous assignment of chosenword and gets rid of >> duplicate entries from the word list. >> >> while words: #has entries in it >>wordslen = len(words) #get the length of the list >>index = random.randint(0, wordslen -1) #get a random index >> if words[index] not in randwords: #as long as the word isn't >> already in the new list >>randwords.append(words[index]) #append the random word to a new >> list >> del words[index] #del the word from the old list >> else: >>del words[index] #remove the duplicate word from the source list >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor