# [Tutor] summing lists

6 messages
Open this post in threaded view
|

## [Tutor] summing lists

 Hi:I have 4 lists:>>> a[40]>>> b[2]>>> c[23]>>> d[12]how is it possible to do add elements in list. I can do this using tupples, but I do not know how to append elements to tuple, thats the reason I am using list. I want to find the value of a+c/a+b+c+d - which is 40+23/40+2+23+12. Any help appreciated. thanksHs._______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] summing lists

 sorry I did this following, please advise if any better way exist:gc = lambda x,y: x[0]+y[0]atgc = lambda x,y,k,l: x[0]+y[0]+k[0]+l[0]>>> gc(a,b)/atgc(a,b,c,d)0.54545454545454541 Hi:I have 4 lists:>>> a[40]>>> b[2]>>> c[23]>>> d[12]how is it possible to do add elements in list. I can do this using tupples, but I do not know how to append elements to tuple, thats the reason I am using list. I want to find the value of a+c/a+b+c+d - which is 40+23/40+2+23+12. Any help appreciated. thanksHs. _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] summing lists

 In reply to this post by Hs Hs On Tue, Apr 10, 2012 at 11:22 AM, Hs Hs wrote: Hi: I have 4 lists: >>> a[40]>>> b [2]>>> c[23]>>> d [12] how is it possible to do add elements in list. I can do this using tupples, but I do not know how to append elements to tuple, thats the reason I am using list.  I want to find the value of a+c/a+b+c+d - which is 40+23/40+2+23+12.  Any help appreciated.  thanksHs._______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor In python, the '+' operator when applied to lists will concatenate the lists together. So:>>> a = [1]>>> b = [2]>>> print a+b [1,2]the 'sum' function in python can be used on lists.>>> a = [1]>>> b = [2]>>> print sum(a+b) 3   _______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Open this post in threaded view
|

## Re: [Tutor] summing lists

 In reply to this post by Hs Hs > I have 4 lists: > > >>> a > [40] > >>> b > [2] > >>> c > [23] > >>> d > [12] > Why are you using lists with a single element, instead of single integer variables? (thus, a=40, b=2, c=23, d=12.) > how is it possible to do add elements in list. sum() > I can do this using tupples, but I do not know how to append elements to tuple You can't. but you can make a a new tuple from two existing tuples: >>> (1, 2) + (3, 4) (1, 2, 3, 4) However, > , thats the reason I am using list. Lists have little to do with your problem below (the sums + division), and certainly not tuples. If possible, better to use plain variables. In this specific case, however, you can just add the lists together (which forms a new list), and then use sum(): >>> a+b [40, 2] >>> a+b+c+d [40, 2, 23, 12] >>> sum(a+b)/sum(a+b+c+d) 0 >>> # oops, I'm using Python 2, not Python 3 here >>> from __future__ import division >>> sum(a+b)/sum(a+b+c+d) 0.5454545454545454 You can also extend lists, which adds a list to an existing list: >>> a.extend(b) >>> a [40, 2] >>> a.extend(c) >>> a [40, 2, 23] etc.   Evert > > I want to find the value of a+c/a+b+c+d - which is 40+23/40+2+23+12. > > Any help appreciated. > > thanks > Hs. > _______________________________________________ > Tutor maillist  -  [hidden email] > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor_______________________________________________ Tutor maillist  -  [hidden email] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor