Reverse function python? How to use?

Discussion in 'Python' started by frankie_85, Oct 29, 2006.

  1. frankie_85

    frankie_85 Guest

    Ok I'm really lost (I'm new to python) how to use the reverse function.


    I made a little program which basically the a, b, c, d, e which I have
    listed below and basically I want it th result to be printed reverse so
    instead doing "print e, d, c, b, a", I'd like to use the reverse
    function

    Can someone give pointersguidelines / on how to do it?

    Code:
        a = str(math.sqrt(math.fabs(x1)) + 5*((math.pow(x1,3))))
        b = str(math.sqrt(math.fabs(x2)) + 5*((math.pow(x2,3))))
        c = str(math.sqrt(math.fabs(x3)) + 5*((math.pow(x3,3))))
        d = str(math.sqrt(math.fabs(x4)) + 5*((math.pow(x4,3))))
        e = str(math.sqrt(math.fabs(x5)) + 5*((math.pow(x5,3))))
    
    Thanks in advance
    frankie_85, Oct 29, 2006
    #1
    1. Advertising

  2. Re: Reverse function python? How to use?

    Use the list's reverse() function. The only thing to keep in mind is
    that it will reverse in-place.
    Here is an example:
    --------------------------------
    In [1]: l=[1,2,3]

    In [2]: l.reverse()

    In [3]: l
    Out[3]: [3, 2, 1]
    ------------------------------------
    So you could accumulate your results in a list then apply reverse() on
    it.

    Hope this helps,
    Nick Vatamaniuc


    frankie_85 wrote:
    > Ok I'm really lost (I'm new to python) how to use the reverse function.
    >
    >
    > I made a little program which basically the a, b, c, d, e which I have
    > listed below and basically I want it th result to be printed reverse so
    > instead doing "print e, d, c, b, a", I'd like to use the reverse
    > function
    >
    > Can someone give pointersguidelines / on how to do it?
    >
    >
    Code:
    >     a = str(math.sqrt(math.fabs(x1)) + 5*((math.pow(x1,3))))
    >     b = str(math.sqrt(math.fabs(x2)) + 5*((math.pow(x2,3))))
    >     c = str(math.sqrt(math.fabs(x3)) + 5*((math.pow(x3,3))))
    >     d = str(math.sqrt(math.fabs(x4)) + 5*((math.pow(x4,3))))
    >     e = str(math.sqrt(math.fabs(x5)) + 5*((math.pow(x5,3))))
    > 
    >
    > Thanks in advance
    Nick Vatamaniuc, Oct 29, 2006
    #2
    1. Advertising

  3. frankie_85

    Murali Guest

    Re: Reverse function python? How to use?

    Something like this?

    Code:
    foo = [x1,x2,x3,x4,x5]
    bar = [math.sqrt(math.fabs(x))+5*math.pow(x,3) for x in foo]
    bar.reverse()
    print bar
    
    frankie_85 wrote:
    > Ok I'm really lost (I'm new to python) how to use the reverse function.
    >
    >
    > I made a little program which basically the a, b, c, d, e which I have
    > listed below and basically I want it th result to be printed reverse so
    > instead doing "print e, d, c, b, a", I'd like to use the reverse
    > function
    >
    > Can someone give pointersguidelines / on how to do it?
    >
    >
    Code:
    >     a = str(math.sqrt(math.fabs(x1)) + 5*((math.pow(x1,3))))
    >     b = str(math.sqrt(math.fabs(x2)) + 5*((math.pow(x2,3))))
    >     c = str(math.sqrt(math.fabs(x3)) + 5*((math.pow(x3,3))))
    >     d = str(math.sqrt(math.fabs(x4)) + 5*((math.pow(x4,3))))
    >     e = str(math.sqrt(math.fabs(x5)) + 5*((math.pow(x5,3))))
    > 
    >
    > Thanks in advance
    Murali, Oct 29, 2006
    #3
  4. frankie_85

    Ben Finney Guest

    "frankie_85" <> writes:

    > I made a little program which basically the a, b, c, d, e which I
    > have listed below and basically I want it th result to be printed
    > reverse so instead doing "print e, d, c, b, a", I'd like to use the
    > reverse function


    As was pointed out before, your assignment requires you to use a
    list. You're using completely distinct names instead of storing these
    sequences in a container. Read your course notes again, paying
    attention to "containers" and especially "lists".

    --
    \ "For of those to whom much is given, much is required." -- |
    `\ John F. Kennedy |
    _o__) |
    Ben Finney
    Ben Finney, Oct 29, 2006
    #4
  5. frankie_85

    Guest

    Re: Reverse function python? How to use?

    If you wanted to keep the original list intact, you could do...

    Code:
    foo = [x1,x2,x3,x4,x5]
    bar = [math.sqrt(math.fabs(x))+5*math.pow(x,3) for x in foo]
    bar_reversed = reversed(bar)
    
    On Oct 29, 4:23 pm, "Murali" <> wrote:
    > Something like this?
    >
    >
    Code:
    > foo = [x1,x2,x3,x4,x5]
    > bar = [math.sqrt(math.fabs(x))+5*math.pow(x,3) for x in foo]
    > bar.reverse()
    > print bar
    > 
    >
    > frankie_85 wrote:
    > > Ok I'm really lost (I'm new to python) how to use the reverse function.

    >
    > > I made a little program which basically the a, b, c, d, e which I have
    > > listed below and basically I want it th result to be printed reverse so
    > > instead doing "print e, d, c, b, a", I'd like to use the reverse
    > > function

    >
    > > Can someone give pointersguidelines / on how to do it?

    >
    > >
    Code:
    > >     a = str(math.sqrt(math.fabs(x1)) + 5*((math.pow(x1,3))))
    > >     b = str(math.sqrt(math.fabs(x2)) + 5*((math.pow(x2,3))))
    > >     c = str(math.sqrt(math.fabs(x3)) + 5*((math.pow(x3,3))))
    > >     d = str(math.sqrt(math.fabs(x4)) + 5*((math.pow(x4,3))))
    > >     e = str(math.sqrt(math.fabs(x5)) + 5*((math.pow(x5,3))))
    > > 

    >
    > > Thanks in advance
    , Oct 30, 2006
    #5
  6. frankie_85

    Kay Schluehr Guest

    Re: Reverse function python? How to use?

    frankie_85 wrote:
    > Ok I'm really lost (I'm new to python) how to use the reverse function.
    >
    >
    > I made a little program which basically the a, b, c, d, e which I have
    > listed below and basically I want it th result to be printed reverse so
    > instead doing "print e, d, c, b, a", I'd like to use the reverse
    > function


    You can use extended slice operators

    http://www.python.org/doc/2.3.5/whatsnew/section-slices.html [1]

    This function call should do what yo expect

    print [e, d, c, b, a][::-1]

    [1] Does anyone know where to find a comprehensible description of
    enhanced slices in the Python docs besides an an aged "What's new?"
    column? Or is it intended that newbies read this
    http://docs.python.org/ref/slicings.html ?
    Kay Schluehr, Oct 30, 2006
    #6
    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. Curt_C [MVP]
    Replies:
    0
    Views:
    1,876
    Curt_C [MVP]
    Jan 22, 2004
  2. dogbite
    Replies:
    4
    Views:
    673
    osmium
    Oct 10, 2003
  3. manuel
    Replies:
    3
    Views:
    263
    Roy Smith
    Sep 11, 2004
  4. Daniel
    Replies:
    5
    Views:
    374
  5. list reverse function

    , Oct 16, 2007, in forum: C Programming
    Replies:
    4
    Views:
    332
Loading...

Share This Page