473,320 Members | 1,902 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,320 software developers and data experts.

How do I isolate individual digits of numbers in python?

If i had the number 123 how would i make it 321?
Sep 14 '10 #1

✓ answered by bvdet

I'll bite
Expand|Select|Wrap|Line Numbers
  1. >>> int(str('123')[::-1])
  2. 321
  3. >>> 

6 12879
bvdet
2,851 Expert Mod 2GB
I'll bite
Expand|Select|Wrap|Line Numbers
  1. >>> int(str('123')[::-1])
  2. 321
  3. >>> 
Sep 14 '10 #2
dwblas
626 Expert 512MB
Add 2 to the first digit and subtract 2 from the last digit?
or add 198 to the first number??
This code was done for some reason some time ago but I don't remember why.
Expand|Select|Wrap|Line Numbers
  1. from collections import deque
  2.  
  3. x = 123
  4. new_x = 0
  5. while x > 0:
  6.     x, y = divmod(x, 10)
  7.     new_x = new_x*10 + y
  8. print new_x
  9.  
  10. x = 123
  11. d = deque()
  12. for num in str(x):
  13.    d.appendleft(num)
  14. print "using deque",
  15. print "".join(d) 
Sep 15 '10 #3
bvdet
2,851 Expert Mod 2GB
I like that dwblas.
Sep 15 '10 #4
thanks guys.
Sep 15 '10 #5
dwblas
626 Expert 512MB
One more for the sake of completeness for any searchers.
Expand|Select|Wrap|Line Numbers
  1. x = 12345
  2. new_str = ""
  3. for ch in str(x):
  4.     new_str = ch + new_str
  5. print int(new_str)
Sep 17 '10 #6
bvdet
2,851 Expert Mod 2GB
Here's another way, also using built-in function divmod():
Expand|Select|Wrap|Line Numbers
  1. >>> def digits(n):
  2. ...     results = []
  3. ...     while n:
  4. ...         n, x = divmod(n, 10)
  5. ...         results.insert(0, x)
  6. ...     return results
  7. ... 
  8. >>> digits(123456789)
  9. [1, 2, 3, 4, 5, 6, 7, 8, 9]
  10. >>> sum([d*10**i for i, d in enumerate(digits(123456789))])
  11. 987654321
  12. >>> 
Nov 26 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
10
by: Tuvas | last post by:
I've been thinking about writing a program to generate the world's largest prime numbers, just for the fun of it. This would require being able to hold an 8000000 digit number into memory (25...
13
by: Kosio | last post by:
Hello, I know of a way to extract digits from a number using the %10 and divide by 10. But I am wondering if there is an algorithm out there that does not use a divide by 10 feature. The...
22
by: Frinton | last post by:
Hi, I am trying to do some calculations on large numbers (ie 7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it doesn't get it quite right. Its always somewhere between 10...
6
by: ocean | last post by:
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits...
49
by: Umesh | last post by:
Is there any way by which i can do it? Thanks.
14
by: thehobbit | last post by:
Hi, Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and pass the result back to a calling program in COBOL? We were able to add up to 15 digit numbers without any problems,...
8
by: Abedin | last post by:
I have 9 digits in form of "111020402". Then I have another 9 digits in form of "111020403". I have 100,000 records of these two 9-digit numbers. I want to filter this as follows: District:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.