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 <joel.goldst...@gmail.com> wrote: > > > On Thu, Sep 9, 2010 at 7:59 AM, Shashwat Anand <anand.shash...@gmail.com> > wrote: > > > On Thu, Sep 9, 2010 at 3:11 PM, Roelof Wobben <rwob...@hotmail.com> 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 <rwob...@hotmail.com> 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