Re: [Tutor] [tutor] creating list of tuples
Varsha Purohit [EMAIL PROTECTED] wrote One small point. for x in file: value=0 tup = () There ia no point in creating an emnpty tuuple. Tuples are immutable so this tuple will simply be thrown away. It doesnm't do much harm, but it does no good either. for developerAgent in developers: #print developerAgent.disutility value +=developerAgent.disutility tup = (value,globalVar.cntr) This creates a brand new tuple and is all that is needed. BTW going by the names used are you sure a list of tuples is what you need? It looks like maybe a dictionary could be used instead storing the values against the cntr as a key. That would perform the role of borth list and tuple and make retrieval of the values easier later. Just a thought. HTH, -- Alan Gauld Author of the Learn to Program web site Temorarily at: http://uk.geocities.com/[EMAIL PROTECTED]/ Normally: http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] [tutor] creating list of tuples
Hello all, In my application i have to create a list of tuples. I have a for loop which will create (x,y) tuple each time it iterates, and i ahve to store each tuple in a list like list1= [(x1,y1), (x2,y2).] any ideas how to do that ??? thanks, -- Varsha Purohit, Graduate Student ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [tutor] creating list of tuples
Varsha Purohit wrote: Hello all, In my application i have to create a list of tuples. I have a for loop which will create (x,y) tuple each time it iterates, and i ahve to store each tuple in a list like list1= [(x1,y1), (x2,y2).] any ideas how to do that ??? list1 = [] for ... # code to create x and y list1.append( (x, y) ) Note you do need the double parentheses; the inner pair creates a tuple, the outer pair is for the function call. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [tutor] creating list of tuples
I think i got it i m creating a tuple of 2 elements everytime and i m adding it to a list. i m creating a new tup everytime and adding it to a list for x in file: value=0 tup = () for developerAgent in developers: #print developerAgent.disutility value +=developerAgent.disutility tup = (value,globalVar.cntr) globalVar.cntr +=1 globalVar.disutilList.append(tup) print globalVar.disutilList i am getting output as what i described... thanks for the help :) On Mon, Feb 25, 2008 at 6:52 PM, Kent Johnson [EMAIL PROTECTED] wrote: Varsha Purohit wrote: Hello all, In my application i have to create a list of tuples. I have a for loop which will create (x,y) tuple each time it iterates, and i ahve to store each tuple in a list like list1= [(x1,y1), (x2,y2).] any ideas how to do that ??? list1 = [] for ... # code to create x and y list1.append( (x, y) ) Note you do need the double parentheses; the inner pair creates a tuple, the outer pair is for the function call. Kent -- Varsha Purohit, Graduate Student ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor