Thank you all for the input. 

I've not been clear in my original question. For each value in the range (in 
example 10) I have to perform calculations with the values in the tuple (in 
example 3). This makes 30 calculations in total.

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.

Solution 1 works, but the double for-statements seem problematic to me because 
if I have to add a 3rd or 4th etc the code becomes unreadable. 

I've tried to put it in a def, but:
* Solution 2 is wrong because it only does part of the calculations
* Solution 3 works, but returns it 10 times, so something is wrong with it!

I gues I should go for solution 1, but I prefer one of the def solutions ( 2 or 
3) because if I have to add more variables I get a very nested chunk of code 
(which I don't understand anymore in a month from now). 



list_of_tuples = [(90629, 4644.91), (90706, 4617.87), (90729, 4709.50)]

n = 100
nmax = 110


def perform_calculations1(n, nmax, value, list_of_tuples, ):
    # this gives totally wrong results
    something = []
    for i2 in xrange(n, int(nmax+1)):
        for t in list_of_tuples:
            v1 = t[0]
            v2 = t[1]
            something = str(i2) + "\tdoes something for\t" + str(v2)    
    return something

def perform_calculations2(n, nmax, value, list_of_tuples, ):
    #this works too good. It returns 10 times the correct 30 values
    something = []
    for i2 in xrange(n, int(nmax+1)):
        for t in list_of_tuples:
            v1 = t[0]
            v2 = t[1]
            something.append(str(i2) + "\tdoes something for\t" + str(v2))
    return something




print "\n\nSolution 1"
for i in xrange(n, int(nmax+1)):
    #returns good results
    for t in list_of_tuples:
        val1 = t[0]
        val2 = t[1]
        print "do stuff with\t" + str(val1) + '\t' + str(val2) + \
              '\tfor range\t' + str(i)


print "\n\nSolution 2"
for i in xrange(n, int(nmax + 1)):
    #returns wrong results
    result = perform_calculations1(n, nmax, i, list_of_tuples, )
    print result

print "\n\nSolution 3"
for i in xrange(n, int(nmax + 1)):
    print "\n"
    #returns good results, but gives it back 10 times!!!
    result = perform_calculations2(n, nmax, i, list_of_tuples, )
    for r in result:
        print r






      
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to