[Tutor] Problems with iterations and breaking loops
alan.gauld at btinternet.com
Thu Mar 18 09:50:54 CET 2010
"Karjer Jdfjdf" <karper12345 at yahoo.com> wrote
> In reality I have to do this for a much larger dataset
> (eg 10000 range * 100000 = a lot), so the for v1, v2 in
> list_of_tuples doesn't work because it is too large for it.
What does the size have to do with it?
v1,v2 in list_of_tuples will only unpack one tuple at a
time regardless of how big the list is.
> I've tried to put it in a def, but:
Take the outer loop out of the function.
The point of functions is to break the problem down into
You should probably have 3 functions here:
1) to do the calculation on a single element
2) to apply function(1) to each item in the tuple list
3) a function to apply function(") from min to max
That makes your code look like
for n in range(min,max):
def function2((val, tuples)
for tuple in tuples:
# whatever calc you need
That way you can test each function in turn and
stop the nested loops from dominating the logic.
def perform_calculations2(n, nmax, value, list_of_tuples, ):
#this works too good. It returns 10 times the correct 30 values
Because you have repeated the outer loop inside the function.
Get rid of it.
something = 
for i2 in xrange(n, int(nmax+1)):
And please stop calling int() when the values are already ints.
for t in list_of_tuples:
v1 = t
v2 = t
something.append(str(i2) + "\tdoes something for\t" + str(v2))
The above is all you need in your function.
Author of the Learn to Program web site
More information about the Tutor