elementwise multiplication of 2 lists of numbers

Discussion in 'Python' started by harryos, Sep 20, 2010.

  1. harryos

    harryos Guest

    hi
    I have 2 lists of numbers,say
    x=[2,4,3,1]
    y=[5,9,10,6]
    I need to create another list containing
    z=[2*5, 4*9, 3*10, 1*6] ie =[10,36,30,6]

    I did not want to use numpy or any Array types.I tried to implement
    this in python .I tried the following

    z=[]
    for a,b in zip(x,y):
    z.append(a*b)
    This gives me the correct result.Still,Is this the correct way?
    Or can this be done in a better way?

    Any pointers most welcome,
    harry
    harryos, Sep 20, 2010
    #1
    1. Advertising

  2. harryos

    Gary Herron Guest

    On 09/20/2010 07:02 AM, harryos wrote:
    > hi
    > I have 2 lists of numbers,say
    > x=[2,4,3,1]
    > y=[5,9,10,6]
    > I need to create another list containing
    > z=[2*5, 4*9, 3*10, 1*6] ie =[10,36,30,6]
    >
    > I did not want to use numpy or any Array types.I tried to implement
    > this in python .I tried the following
    >
    > z=[]
    > for a,b in zip(x,y):
    > z.append(a*b)
    > This gives me the correct result.Still,Is this the correct way?
    > Or can this be done in a better way?
    >
    > Any pointers most welcome,
    > harry
    >


    List comprehension might be considered better by some, but that's a
    subjective judgment. (One with which I agree.) List comprehension may
    also be faster, but you'd have to test to know for sure.

    >>> x=[2,4,3,1]
    >>> y=[5,9,10,6]
    >>> z = [a*b for a,b in zip(x,y)]
    >>> print z

    [10, 36, 30, 6]


    Gary Herron
    Gary Herron, Sep 20, 2010
    #2
    1. Advertising

  3. harryos a écrit :
    > hi
    > I have 2 lists of numbers,say
    > x=[2,4,3,1]
    > y=[5,9,10,6]
    > I need to create another list containing
    > z=[2*5, 4*9, 3*10, 1*6] ie =[10,36,30,6]
    >
    > I did not want to use numpy or any Array types.I tried to implement
    > this in python .I tried the following
    >
    > z=[]
    > for a,b in zip(x,y):
    > z.append(a*b)
    > This gives me the correct result.Still,Is this the correct way?


    If it gives the expected results then it's at least *a* correct way !-)

    > Or can this be done in a better way?


    A list comp comes to mind, as well as using itertools.izip if your lists
    are a bit on the huge side.

    from itertools import izip
    z = [a * b for a, b in izip(x, y)]
    Bruno Desthuilliers, Sep 20, 2010
    #3
  4. harryos <> writes:

    > hi
    > I have 2 lists of numbers,say
    > x=[2,4,3,1]
    > y=[5,9,10,6]
    > I need to create another list containing
    > z=[2*5, 4*9, 3*10, 1*6] ie =[10,36,30,6]
    >
    > I did not want to use numpy or any Array types.I tried to implement
    > this in python .I tried the following
    >
    > z=[]
    > for a,b in zip(x,y):
    > z.append(a*b)
    > This gives me the correct result.Still,Is this the correct way?
    > Or can this be done in a better way?


    what you've done is correct, rather than in better ways this can be
    done in different ways

    first, there is list comprehension
    >>> [x*y for x,y in zip([2,4,3,1],[5,9,10,6])]

    [10, 36, 30, 6]
    >>>


    if you feel that "zip" looks like an artifact, python has some
    functional bit
    >>> map(lambda x,y: x*y, [2,4,3,1],[5,9,10,6])

    [10, 36, 30, 6]
    >>>


    if you feel that "lambda" looks like an artifact,
    >>> from operator import mul
    >>> map(mul, [2,4,3,1],[5,9,10,6])

    [10, 36, 30, 6]
    >>>


    hth,
    --
    > In tutti noi c'è un lato interista

    Lato perlopiù nascosto dalle mutande.
    --- Basil Fawlty, a reti unificate (IFQ+ISC)
    Giacomo Boffi, Sep 20, 2010
    #4
  5. harryos

    harryos Guest

    On Sep 20, 7:28 pm, Bruno wrote:
    >> A list comp comes to mind, as well as using itertools.izip


    thanks Bruno,thanks Gary..
    Should have thought of list comprehension..
    Thanks for the pointer about izip

    harry
    harryos, Sep 20, 2010
    #5
  6. On Sep 20, 3:02 pm, harryos <> wrote:
    > hi
    > I have 2 lists of numbers,say
    > x=[2,4,3,1]
    > y=[5,9,10,6]
    > I need to create another list containing
    > z=[2*5, 4*9, 3*10, 1*6]  ie =[10,36,30,6]
    >
    > I did not want to use numpy or any Array types.I tried to implement
    > this in python .I tried the following
    >
    > z=[]
    > for a,b in zip(x,y):
    >         z.append(a*b)
    > This gives me the correct result.Still,Is this the correct way?
    > Or can this be done in a better way?
    >
    > Any pointers most welcome,
    > harry


    In addition to what others have said, you can use the map builtin as
    follows:
    In Python 2.x:

    >>> map(int.__mul__, [2, 4, 3, 1], [5, 9, 10, 6])

    [10, 36, 30, 6]

    If you don't know the list only contain ints use operator.mul.

    In Python 3.x you need to wrap it in list() as map returns an
    iterator.

    HTH

    --
    Arnaud
    Arnaud Delobelle, Sep 20, 2010
    #6
  7. harryos

    Peter Otten Guest

    harryos wrote:

    > I have 2 lists of numbers,say
    > x=[2,4,3,1]
    > y=[5,9,10,6]
    > I need to create another list containing
    > z=[2*5, 4*9, 3*10, 1*6] ie =[10,36,30,6]
    >
    > I did not want to use numpy or any Array types.I tried to implement
    > this in python .I tried the following
    >
    > z=[]
    > for a,b in zip(x,y):
    > z.append(a*b)
    > This gives me the correct result.Still,Is this the correct way?
    > Or can this be done in a better way?
    >
    > Any pointers most welcome,


    Finally, if you have a lot of data that you want to process efficiently
    consider using numpy arrays instead of lists:

    >>> import numpy
    >>> x = numpy.array([2,4,3,1])
    >>> y = numpy.array([5,9,10,6])
    >>> x*y

    array([10, 36, 30, 6])

    Peter
    Peter Otten, Sep 20, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. er
    Replies:
    7
    Views:
    2,081
    terminator
    Sep 5, 2007
  2. William Hughes
    Replies:
    13
    Views:
    1,204
    Ben Bacarisse
    Mar 15, 2010
  3. Nathan Rice
    Replies:
    10
    Views:
    346
    Ethan Furman
    Dec 21, 2011
  4. Nathan Rice
    Replies:
    4
    Views:
    224
    Nathan Rice
    Dec 21, 2011
  5. Nathan Rice
    Replies:
    0
    Views:
    148
    Nathan Rice
    Jan 16, 2012
Loading...

Share This Page