469,922 Members | 2,141 Online

Reverse function python? How to use?

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?

Expand|Select|Wrap|Line Numbers
1. a = str(math.sqrt(math.fabs(x1)) + 5*((math.pow(x1,3))))
2. b = str(math.sqrt(math.fabs(x2)) + 5*((math.pow(x2,3))))
3. c = str(math.sqrt(math.fabs(x3)) + 5*((math.pow(x3,3))))
4. d = str(math.sqrt(math.fabs(x4)) + 5*((math.pow(x4,3))))
5. e = str(math.sqrt(math.fabs(x5)) + 5*((math.pow(x5,3))))
6.

Oct 29 '06 #1
5 3544 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 : l=[1,2,3]

In : l.reverse()

In : l
Out: [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?

Expand|Select|Wrap|Line Numbers
1.     a = str(math.sqrt(math.fabs(x1)) + 5*((math.pow(x1,3))))
2.     b = str(math.sqrt(math.fabs(x2)) + 5*((math.pow(x2,3))))
3.     c = str(math.sqrt(math.fabs(x3)) + 5*((math.pow(x3,3))))
4.     d = str(math.sqrt(math.fabs(x4)) + 5*((math.pow(x4,3))))
5.     e = str(math.sqrt(math.fabs(x5)) + 5*((math.pow(x5,3))))
6.

Oct 29 '06 #2
Something like this?

Expand|Select|Wrap|Line Numbers
1. foo = [x1,x2,x3,x4,x5]
2. bar = [math.sqrt(math.fabs(x))+5*math.pow(x,3) for x in foo]
3. bar.reverse()
4. print bar
5.
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?

Expand|Select|Wrap|Line Numbers
1.     a = str(math.sqrt(math.fabs(x1)) + 5*((math.pow(x1,3))))
2.     b = str(math.sqrt(math.fabs(x2)) + 5*((math.pow(x2,3))))
3.     c = str(math.sqrt(math.fabs(x3)) + 5*((math.pow(x3,3))))
4.     d = str(math.sqrt(math.fabs(x4)) + 5*((math.pow(x4,3))))
5.     e = str(math.sqrt(math.fabs(x5)) + 5*((math.pow(x5,3))))
6.

Oct 29 '06 #3
"frankie_85" <st******@gmail.comwrites:
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
attention to "containers" and especially "lists".

--
\ "For of those to whom much is given, much is required." -- |
`\ John F. Kennedy |
_o__) |
Ben Finney

Oct 29 '06 #4
If you wanted to keep the original list intact, you could do...

Expand|Select|Wrap|Line Numbers
1. foo = [x1,x2,x3,x4,x5]
2. bar = [math.sqrt(math.fabs(x))+5*math.pow(x,3) for x in foo]
3. bar_reversed = reversed(bar)
4.
On Oct 29, 4:23 pm, "Murali" <gmkrishn.u...@gmail.comwrote:
Something like this?

Expand|Select|Wrap|Line Numbers
1. foo = [x1,x2,x3,x4,x5]
2. bar = [math.sqrt(math.fabs(x))+5*math.pow(x,3) for x in foo]
3. bar.reverse()
4. print bar
5.

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?
Expand|Select|Wrap|Line Numbers
1.      a = str(math.sqrt(math.fabs(x1)) + 5*((math.pow(x1,3))))
2.      b = str(math.sqrt(math.fabs(x2)) + 5*((math.pow(x2,3))))
3.      c = str(math.sqrt(math.fabs(x3)) + 5*((math.pow(x3,3))))
4.      d = str(math.sqrt(math.fabs(x4)) + 5*((math.pow(x4,3))))
5.      e = str(math.sqrt(math.fabs(x5)) + 5*((math.pow(x5,3))))
6.
Oct 30 '06 #5
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/what...on-slices.html 

This function call should do what yo expect

print [e, d, c, b, a][::-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 ?

Oct 30 '06 #6

 35 posts views Thread by Raymond Hettinger | last post: by 59 posts views Thread by Raymond Hettinger | last post: by 31 posts views Thread by Raymond Hettinger | last post: by 14 posts views Thread by Raymond Hettinger | last post: by 8 posts views Thread by rh0dium | last post: by 41 posts views Thread by rick | last post: by 1 post views Thread by sunnyluthra1 | last post: by 15 posts views Thread by Alex Snast | last post: by 1 post views Thread by Chris Rebert | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by isladogs | last post: by 1 post views Thread by isladogs | last post: by reply views Thread by Trystan | last post: by reply views Thread by Waqarahmed | last post: by 9 posts views Thread by anoble1 | last post: by 1 post views Thread by skydivetom | last post: by reply views Thread by Romlus | last post: by 1 post views Thread by MikeCant | last post: by