473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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.  
Thanks in advance

Oct 29 '06 #1
5 4052
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?

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.  

Thanks in advance
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.  

Thanks in advance
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
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

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.  
Thanks in advance
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 [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 ?

Oct 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
31
by: Raymond Hettinger | last post by:
Based on your extensive feedback, PEP 322 has been completely revised. The response was strongly positive, but almost everyone preferred having a function instead of multiple object methods. The...
14
by: Raymond Hettinger | last post by:
Based on the feedback here on comp.lang.python, the pep has been updated: www.python.org/peps/pep-0322.html The key changes are: * reversed() is being preferred to ireverse() as the best...
8
by: rh0dium | last post by:
Hi all, I have a dict which looks like this.. dict={'130nm': {'umc': }, '180nm': {'chartered': , 'tsmc': }, '250nm': {'umc': , 'tsmc': } }
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
1
by: sunnyluthra1 | last post by:
Hi, I was creating an Application in MS Access for Geocoding a particular Address from Google to get the Lat & Long. I successfully able to did that. Here is the code:...
15
by: Alex Snast | last post by:
Hello I'm new to python and i can't figure out how to write a reverse for loop in python e.g. the python equivalent to the c++ loop for (i = 10; i >= 0; --i)
1
by: Chris Rebert | last post by:
On Thu, Oct 2, 2008 at 8:07 PM, David Di Biase <dave.dibiase@gmail.comwrote: Rather than defining a comparison function here (which is less efficient), you can use the 'key' argument, which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.