equivalent of NULL or OM in Python

Greg Ewing greg at cosc.canterbury.ac.nz
Tue Feb 20 18:51:43 EST 2001


cyberian bear wrote:
> 
> Maybe I
> should just check if the second term of every tuple(i.e the coefficient) is
> zero which means that the term is not present.

It depends on how your algorithms work. If you find that,
e.g. it makes your multiplication algorithm awkward if
terms can be missed out, then always storing every term,
possibly with a zero coefficient, makes sense.

If you're doing that, you don't really need to store
the exponents, because they're implied by the position
in the list -- so all you need is a list of coefficients.
I would probably store it reversed, so that the coefficient
of x^i is in position i of the list, e.g.

  3x^2+2x+4 --> [4, 2, 3]

> how do i tell it to iterate specifically over the second term in every
> tuple and not over the whole tuples

Note that you can "unpack" a tuple by doing:

  exp, coeff = term

so one way to do what you want is

  for term in poly:
    exp, coeff = term
      #do something with coeff

or, more compactly,

  for exp, coeff in poly:
    #do something with coeff

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list