Yawar Amin wrote:
Cool ... but it looks like this can still potentially hit the max
recursion limit?
It depends on the nature of your data. If the data is
a tree, it's very unlikely you'll reach the recursion
limit unless the tree is massively unbalanced.
If there's a chance of that, or if th
Roy Smith wrote:
I will commonly put something like:
import logging
logger = logging.getLogger("logger-name-for-my-module")
But that's not really a global variable, it's a
global constant. There's nothing wrong with those,
we use them all the time -- classes, functions, etc.
If you were to re
On Sun, Jan 18, 2015 at 4:30 AM, Albert van der Horst
wrote:
> The proper technique is make the global local to the normal subroutine,
> then make the subroutine with those parameters you don't want to see
> also local to that subroutine.
> E.g.
>
> def fib(n):
> ' return the n-th Fibonacci nu
In article <5e4ccec6-7a00-467d-8cf6-258ab0421...@googlegroups.com>,
Tim wrote:
>I have this type of situation and wonder if I should use a global
>variable outside the recursive function instead of passing the updated
>parameter through.
>
>I want to get a union of all the values that any 'things
In article <54ba3654$0$13008$c3e8da3$54964...@news.astraweb.com>,
Steven D'Aprano wrote:
> Good reasons for using global variables are few and far between. Just about
> the only good reason for using global variables that I can think of is if
> you have one or more settings/preference that get s
Tim wrote:
> I have this type of situation and wonder if I should use a global variable
> outside the recursive function instead of passing the updated parameter
> through.
To a first approximation, the answer to:
"I have a X, should I use a global variable or a parameter?"
is *always* "use a p
On Friday, January 16, 2015 at 9:24:15 PM UTC-5, Yawar Amin wrote:
> [...]
> vals.extend(curr_obj.values())
Ah, I should mention that the above will do a breadth-first search. If
we want to do a depth-first search we simply replace the above line
with:
vals.extendleft(curr_obj.values(
On Friday, January 16, 2015 at 1:34:51 PM UTC-5, Peter Otten wrote:
> [...]
> I recommend that you use a generator:
>
> >>> def walk(obj):
> ... if not hasattr(obj, "keys"):
> ... return
> ... if "things" in obj:
> ... yield obj["things"]
> ... for v in obj.values():
>
On Sat, Jan 17, 2015 at 9:20 AM, Gregory Ewing
wrote:
> The only thing I would change is to wrap it all up
> in a top-level function that takes care of creating
> the result set and returning it.
>
> def walk(obj):
> res = set()
> _walk(obj, res)
> return res
>
> def _walk(obj, res):
> ...
Tim wrote:
I have this type of situation and wonder if I should use a global variable
outside the recursive function instead of passing the updated parameter
through.
No! Globals are evil, at least for that sort of thing.
The way you're doing it is fine.
The only thing I would change is to wra
On Friday, January 16, 2015 at 1:34:51 PM UTC-5, Peter Otten wrote:
>> Tim wrote:
>>
> Globals are generally bad as they make code non-reentrant; when two calls of
> the function run simultaneously the data will be messed up.
>
> I recommend that you use a generator:
>
> >>> def walk(obj):
> ..
Tim wrote:
> I have this type of situation and wonder if I should use a global variable
> outside the recursive function instead of passing the updated parameter
> through.
>
> I want to get a union of all the values that any 'things' key may have,
> even in a nested dictionary (and I do not know
On Friday, January 16, 2015 at 11:26:46 PM UTC+5:30, Chris Angelico wrote:
> On Sat, Jan 17, 2015 at 4:49 AM, Tim wrote:
> > I want to get a union of all the values that any 'things' key may have,
> > even in a nested dictionary (and I do not know beforehand how deep the
> > nesting might go):
>
On Sat, Jan 17, 2015 at 4:49 AM, Tim wrote:
> I want to get a union of all the values that any 'things' key may have, even
> in a nested dictionary (and I do not know beforehand how deep the nesting
> might go):
>
> d = {'things':1, 'two':{'things':2}}
>
> def walk(obj, res):
> if not hasatt
On 2013-03-05, Ana Dion?sio wrote:
> Yes, I simplified it a lot.
>
> I need to run it 24 times. What I don't really understand is
> how to put the final temperature (result) in it = 0 in temp_-1
> in it =1
One way to compose a function that recurses by calling itself is
to first write the simples
Yes, I simplified it a lot.
I need to run it 24 times. What I don't really understand is how to put the
final temperature (result) in it = 0 in temp_-1 in it =1
--
http://mail.python.org/mailman/listinfo/python-list
2013/3/5 Ana Dionísio :
> Hello!
>
> I have to make a script that calculates temperature, but one of the
> parameters is the temperature in the iteration before, for example:
> temp = (temp_-1)+1
>
> it = 0
> temp = 3
>
> it = 1
> temp = 3+1
>
> it = 2
> temp = 4+1
>
> How can I do this in a simple
On 03/05/2013 10:32 AM, Ana Dionísio wrote:
Hello!
I have to make a script that calculates temperature, but one of the
parameters is the temperature in the iteration before, for example:
temp = (temp_-1)+1
it = 0
temp = 3
it = 1
temp = 3+1
it = 2
temp = 4+1
How can I do this in a simple way?
"Diez B. Roggisch" <[EMAIL PROTECTED]> writes:
>> #include
>> #include
>>
>> def RecursiveFact(n):
>> if(n>1):
>> return n*RecursiveFact(n-1)
>> else:
>> return 1
>>
>> fact = RecursiveFact(31)
>> print fact
>>
>> fact = "End of program"
>> print fact
>>
>>
>> ..but y
On Apr 2, 5:00 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] schrieb:
>
>
>
> > #include
> > #include
>
> > def RecursiveFact(n):
> > if(n>1):
> > return n*RecursiveFact(n-1)
> > else:
> > return 1
>
> > fact = RecursiveFact(31)
> > print fact
>
> >
[EMAIL PROTECTED] schrieb:
> #include
> #include
>
> def RecursiveFact(n):
> if(n>1):
> return n*RecursiveFact(n-1)
> else:
> return 1
>
> fact = RecursiveFact(31)
> print fact
>
> fact = "End of program"
> print fact
>
>
> ..but yet it still gives the right answe
In article <[EMAIL PROTECTED]>,
<[EMAIL PROTECTED]> wrote:
(Subject: Recursive function won't compile)
>#include
>#include
>
>def RecursiveFact(n):
>..but yet it still gives the right answer. How is this possible?
Possibly because it gives the right answer in a different language than
it
On Apr 2, 5:23 pm, [EMAIL PROTECTED] wrote:
> #include
> #include
>
> def RecursiveFact(n):
> if(n>1):
> return n*RecursiveFact(n-1)
> else:
> return 1
>
> fact = RecursiveFact(31)
> print fact
The output is 822283865417792281772556288000 and is correct. But
the "#incl
[EMAIL PROTECTED] wrote:
> #include
> #include
>
> def RecursiveFact(n):
> if(n>1):
> return n*RecursiveFact(n-1)
> else:
> return 1
>
> fact = RecursiveFact(31)
> print fact
>
> fact = "End of program"
> print fact
>
>
> ..but yet it still gives the right answer. How
Hendrik van Rooyen wrote:
> "cesco" <[EMAIL PROTECTED]> wrote:
>
> >
> > Neil Cerutti wrote:
> > > On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote:
> > > > Hi,
> > > >
> > > > I have a dictionary of lists of tuples like in the following example:
> > > > dict = {1: [(3, 4), (5, 8)],
> > > >
"cesco" <[EMAIL PROTECTED]> wrote:
>
> Neil Cerutti wrote:
> > On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote:
> > > Hi,
> > >
> > > I have a dictionary of lists of tuples like in the following example:
> > > dict = {1: [(3, 4), (5, 8)],
> > > 2: [(5, 4), (21, 3), (19, 2)],
> > >
"cesco" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have a dictionary of lists of tuples like in the following
> example: dict = {1: [(3, 4), (5, 8)],
> 2: [(5, 4), (21, 3), (19, 2)],
> 3: [(16, 1), (0, 2), (1, 2), (3, 4)]]
>
> In this case I have three lists inside the dict but this
On 8 Jan 2007 16:03:53 +0100, Neil Cerutti <[EMAIL PROTECTED]> wrote:
>
> len(dict.keys()).
>
Or
len(dict)
:)
--
http://mail.python.org/mailman/listinfo/python-list
First possible solution:
def rloop(seqin, comb):
# xcross product recipe 302478 by David Klaffenbach
if seqin:
for item in seqin[0]:
newcomb = comb + [item]
for item in rloop(seqin[1:], newcomb):
yield item
else:
yield comb
data
Neil Cerutti wrote:
> On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I have a dictionary of lists of tuples like in the following example:
> > dict = {1: [(3, 4), (5, 8)],
> > 2: [(5, 4), (21, 3), (19, 2)],
> > 3: [(16, 1), (0, 2), (1, 2), (3, 4)]]
> >
> > In this
On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have a dictionary of lists of tuples like in the following example:
> dict = {1: [(3, 4), (5, 8)],
> 2: [(5, 4), (21, 3), (19, 2)],
> 3: [(16, 1), (0, 2), (1, 2), (3, 4)]]
>
> In this case I have three lists inside the dict
[EMAIL PROTECTED] wrote:
> Bruno Desthuilliers wrote:
>
>>[EMAIL PROTECTED] wrote:
>
> [...]
>
>>>Sorry, but I kinda agree with Boris here.
>>
>>On what ?
>
>
> On the argument that you are (implicitly?) disagreeing with him
it's getting messy - too much level of indirection !-)
> on,
> obv
Bruno Desthuilliers wrote:
> [EMAIL PROTECTED] wrote:
[...]
> > Sorry, but I kinda agree with Boris here.
>
> On what ?
On the argument that you are (implicitly?) disagreeing with him on,
obviously. That the OP problem is not definitely the default values
question. As you say:
> >>If the OP has o
Bruno Desthuilliers wrote:
>
> Nope, it's about trying to make sure that anyone googling for a similar
> problem will notice the canonical solution somehow.
>
Hey, I challenge you to cook up a plausible query even loosely fitting your
"googling for a similar problem" that would return my answe
Steve Holden wrote:
> Bruno Desthuilliers wrote:
>
>> Boris Borcic a écrit :
>>
>>> Hello Bruno,
>>>
>>> Bruno Desthuilliers wrote:
>
> [...]
>
Or how to *not* address the real problem...
Boris, using a generator may be a pretty good idea, but *not* as a way
to solve a proble
[EMAIL PROTECTED] wrote:
> Bruno Desthuilliers wrote:
>
>>Boris Borcic a écrit :
>>
>>>Hello Bruno,
>>>
>>>Bruno Desthuilliers wrote:
>>>
>>>
Boris Borcic wrote:
>>Do you have any ideas?
>
>
>you could use a recursive generator, like
>
>def genAllChildren(self
Bruno Desthuilliers wrote:
> Boris Borcic a écrit :
>> Hello Bruno,
>>
>> Bruno Desthuilliers wrote:
>>
>>> Boris Borcic wrote:
>>>
> Do you have any ideas?
you could use a recursive generator, like
def genAllChildren(self) :
for child in self.children :
Bruno Desthuilliers wrote:
> Boris Borcic a écrit :
>
>>Hello Bruno,
>>
>>Bruno Desthuilliers wrote:
[...]
>>>Or how to *not* address the real problem...
>>>
>>>Boris, using a generator may be a pretty good idea, but *not* as a way
>>>to solve a problem that happens to be a FAQ !-)
>>>
>>
>>Sorry,
Bruno Desthuilliers wrote:
> Boris Borcic a écrit :
> > Hello Bruno,
> >
> > Bruno Desthuilliers wrote:
> >
> >> Boris Borcic wrote:
> >>
> Do you have any ideas?
> >>>
> >>>
> >>> you could use a recursive generator, like
> >>>
> >>> def genAllChildren(self) :
> >>> for child in self.chil
Boris Borcic a écrit :
> Hello Bruno,
>
> Bruno Desthuilliers wrote:
>
>> Boris Borcic wrote:
>>
Do you have any ideas?
>>>
>>>
>>> you could use a recursive generator, like
>>>
>>> def genAllChildren(self) :
>>> for child in self.children :
>>> yield child
>>> for childc
Hello Bruno,
Bruno Desthuilliers wrote:
> Boris Borcic wrote:
>>> Do you have any ideas?
>>
>> you could use a recursive generator, like
>>
>> def genAllChildren(self) :
>> for child in self.children :
>> yield child
>> for childchild in child.genAllChildren() :
>>
Boris Borcic wrote:
>> Do you have any ideas?
>
>
> you could use a recursive generator, like
>
> def genAllChildren(self) :
> for child in self.children :
> yield child
> for childchild in child.genAllChildren() :
> yield childchild
Or how to *not* address the
> Do you have any ideas?
you could use a recursive generator, like
def genAllChildren(self) :
for child in self.children :
yield child
for childchild in child.genAllChildren() :
yield childchild
--
http://mail.python.org/mailman/listinfo/python-list
Fabian Steiner wrote:
> Hello!
>
> I have got a Python "Device" Object which has got a attribute (list)
> called children which my contain several other "Device" objects. I
> implemented it this way in order to achieve a kind of parent/child
> relationship.
>
> Now I would like to get all chil
In <[EMAIL PROTECTED]>, Fabian Steiner wrote:
> This is what I got so far:
>
> def getAllChildren(self, children=[]):
> if self.children:
> children.extend(self.children)
> for child in self.children:
> child.getAllChildren(children)
>
Ok, I finally got it working! See below
On 2/4/06, Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> On Sat, 04 Feb 2006 02:18:27 -0500, Gregory Piñero wrote:
> > class Node:
> > def __init__(self):
> > self.arg0=0
> > self.arg1=0
> > self.arg2=0
> > self.arg3=0
>
>
Gregory Piñero wrote:
> I want to walk down each tree and get a random subtree at a random
> depth.
Can you quantify that randomness? Should it be uniform at each level?
Thinking about this may be fruitful. I don't yet know whether you need
to see all leaves before you know which subtree
Thanks for the advice guys. See below.
On 2/4/06, Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> On Sat, 04 Feb 2006 02:18:27 -0500, Gregory Piñero wrote:
>
> > class Node:
> > def __init__(self):
> > self.arg0=0
> > self.arg1=0
> > self.arg2=0
> > self.arg3=0
>
On Sat, 04 Feb 2006 02:18:27 -0500, Gregory Piñero wrote:
> Hi,
>
> Would anyone be able to tell me why my function below is getting stuck
> in infinite recusion?
> Maybe I'm just tired and missing something obvious?
Your code is quite confusing, especially since there is very little
documentati
>Would anyone be able to tell me why my function below is getting stuck
>in infinite recusion?
>def replace_within_node(node,oldnode,newnode):
>if node is oldnode:
>return newnode
Without looking further, the most likely reason is that the base case is
never true: ie, node is never o
By the way, all I'm trying to do here is take two trees, randomly find
a sub-tree of each and swap the sub-trees. So if anyone has a simple
method for doing that I'm certainly open to that too.
Thanks again,
-Greg
On 2/4/06, Gregory Piñero <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Would anyone be a
[EMAIL PROTECTED] wrote:
> hi, i have the following recursive function (simplified to demonstrate
> the problem):
>
> >>> def reTest(bool):
> ... result = []
> ... if not bool:
> ... reTest(True)
> ... else:
> ... print "YAHHH"
> ... result = ["should be the onl
[EMAIL PROTECTED] wrote:
> hi, i have the following recursive function (simplified to demonstrate
> the problem):
>
>
def reTest(bool):
>
> ... result = []
> ... if not bool:
> ... reTest(True)
> ... else:
> ... print "YAHHH"
> ... result = ["should be the
[EMAIL PROTECTED] writes:
> hi, i have the following recursive function (simplified to demonstrate
> the problem):
def reTest(bool):
> ... result = []
> ... if not bool:
> ... reTest(True)
> ... else:
> ... print "YAHHH"
> ... result = ["should be the only t
[EMAIL PROTECTED] wrote:
> ... if not bool:
> ... reTest(True)
> I don't understand why results are returned twice? is there something
> special i missed about recursive functions?
Yes, although it is not clear *what* it is that you are missing.
Here is my guess: you fail to see that
On Wed, 28 Dec 2005 16:05:30 -0800, randomtalk wrote:
> the final returned value is: []
>
> the two values printed is (note i only have one print statement
> printing "print result",. however, in the actualality, it's printed
> twice):
> printing result:
> ['should be the only thing returned']
>
ah, result = reTest(True) works, thanks alot :D
--
http://mail.python.org/mailman/listinfo/python-list
On Wed, 28 Dec 2005 15:25:30 -0800, randomtalk wrote:
> hi, i have the following recursive function (simplified to demonstrate
> the problem):
>
def reTest(bool):
> ... result = []
> ... if not bool:
> ... reTest(True)
> ... else:
> ... print "YAHHH"
> ...
You have two calls to reTest and so reTest needs to return twice. One
return is from the reTest(True) call back to reTest(False). The second
return is from reTest(False) back to the prompt.
What were you expecting to happen?
--
http://mail.python.org/mailman/listinfo/python-list
On 28 Dec 2005 15:25:30 -0800,
[EMAIL PROTECTED] wrote:
> hi, i have the following recursive function (simplified to demonstrate
> the problem):
def reTest(bool):
> ... result = []
> ... if not bool:
> ... reTest(True)
Don't do that. Do this instead:
result =
the final returned value is: []
the two values printed is (note i only have one print statement
printing "print result",. however, in the actualality, it's printed
twice):
printing result:
['should be the only thing returned']
printing result:
[]
therefore, sadly, i don't thinkg you've understand
[EMAIL PROTECTED] wrote:
def reTest(bool):
>
> ... result = []
> ... if not bool:
> ... reTest(True)
> ... else:
> ... print "YAHHH"
> ... result = ["should be the only thing returned"]
> ... print "printing result: "
> ... print result
> ... return re
Nicolas Vigier wrote:
> Hello,
>
> I have in my python script a function that look like this :
>
> def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42):
> if type(arg1) is ListType:
How should it behave with tuples or subclasses of List ? Or if it's any
other iterable ?
Testing against
Peter Otten said:
> Here is a non-recursive approach:
>
> def tolist(arg):
>if isinstance(arg, list):
>return arg
>return [arg]
>
> def f(arg1, arg2, more_args):
>for arg1 in tolist(arg1):
>for arg2 in tolist(arg2):
># real code
>
> If it were my code I would
Nicolas Vigier wrote:
> I have in my python script a function that look like this :
>
> def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42):
> if type(arg1) is ListType:
> for a in arg1:
> my_function(a, arg2, opt1=opt1, opt2=opt2,
>
Nicolas Vigier wrote:
> Hello,
>
> I have in my python script a function that look like this :
>
> def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42):
> if type(arg1) is ListType:
> for a in arg1:
> my_function(a, arg2, opt1=opt1, opt2=opt2,
>
> def foo(j):
> while j < n:
> j+=1
> return j
>
of course I mean:
def foo(j):
while j < n:
j+=1
return j
sorry
== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups
==
Get Anonymous, Uncensored, Access to West and East Coast Server Fa
Is there no way to implement your idea in a classical loop? Usually the
syntax is cleaner, and there is no limit (except the limit of the range
function in certain cases). For example what would be wrong with.
def foo(j):
while j < n:
j+=1
return j
I don't know much about th
mg wrote:
> Hello,
>
> In a recursive function like the following :
>
>
> def foo( j ) :
> j += 1
> while j < n : j = foo( j )
> return j
>
>
> in found that the recursivity is limited (1000 iterations). Then, I have
> two questions :
> - why this mecanism has been implemented ?
> - it is
69 matches
Mail list logo