[Tutor] summing lists

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] summing lists

Hs Hs
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. 

thanks
Hs.

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] summing lists

Hs Hs
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. 

thanks
Hs.



_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] summing lists

James-2
In reply to this post by Hs Hs


On Tue, Apr 10, 2012 at 11:22 AM, Hs Hs <[hidden email]> 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. 

thanks
Hs.

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] summing lists

Evert Rol
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(<list>)


> 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
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] summing lists

Hs Hs
thanks all for quick reply.  In my previous e-mail I send adding using lambda function. I now know that I am using too much functionality where simple ways to solve exist. That's python!
thanks again. 
Hs.


From: Evert Rol <[hidden email]>
To: Hs Hs <[hidden email]>
Cc: "[hidden email]" <[hidden email]>
Sent: Tuesday, April 10, 2012 11:37 AM
Subject: Re: [Tutor] summing lists

> 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(<list>)


> 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
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] summing lists

Dave Angel-3
In reply to this post by Hs Hs
On 04/10/2012 11:28 AM, Hs Hs wrote:
> sorry I did this following, please advise if any better way exist:
>

You top-posted, so we lose the context you were using.

You never really say why you're using lists with exactly one element in
them, but presuming that the real lists consist of something like
    a = [4, "Tom Jones"]
    b = [12, "Peter Rabbit", "March Hare"]

I'll follow your convention of assuming only the zeroth element is
relevant to your 'definition of add'.

> gc = lambda x,y: x[0]+y[0]
>
> atgc = lambda x,y,k,l: x[0]+y[0]+k[0]+l[0]
>
>

No point in using lambda for that purpose.  Since you are not trying to
make it anonymous, you could simply use

def gc(x, y):
   return x[0] + y[0]

for example.

But the more interesting question is how to combine the two functions,
and make one which can take a variable number of items.


def gc(*many):
     return sum( [ x[0] for x in many ] )





--

DaveA

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