471,338 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Need to split the number

440 256MB
Hi,

I have a number ,i have to split it into 3 digits

For Example num = 001001001001000001

After the split it should be

001
001
001
001
000
001
Is there any better way to split it

num = '001001001001000001'
x1 = num[0:3]
x2 = num[3:6]
x3 = num[6:9]
x4 = num[9:12]
x5 = num[12:15]
x6 = num[15:18]

Thanks
PSB
Jan 6 '08 #1
4 4600
ghostdog74
511 Expert 256MB
Have you read the references?
Expand|Select|Wrap|Line Numbers
  1. >>> s='001001001001000001'
  2. >>> import textwrap
  3. >>> textwrap.wrap(s,3)
  4. ['001', '001', '001', '001', '000', '001']                     
  5.  
You should really spend time reading up on Python docs because after 400+ posts, you should really be able to grasp the essence of Python.
Jan 6 '08 #2
psbasha
440 256MB
Have you read the references?
Expand|Select|Wrap|Line Numbers
  1. >>> s='001001001001000001'
  2. >>> import textwrap
  3. >>> textwrap.wrap(s,3)
  4. ['001', '001', '001', '001', '000', '001']                     
  5.  
You should really spend time reading up on Python docs because after 400+ posts, you should really be able to grasp the essence of Python.
Thanks for the solution.

I have read couple of books on Python
- Dive in python
- Core Python Programming

and gone thru the links in the Internet.Python is having so many concepts,that we cannot get everything within short duration.So as we keep programming ,then thier will be more chance of learning more concepts( based on our requirements) than reading.I really appreciapte for your suggestion.

Could you please suggest some of the books you have read,which really help me in understanding the concepts in still better manner.

Thanks
PSB
Jan 6 '08 #3
ghostdog74
511 Expert 256MB
Could you please suggest some of the books you have read,which really help me in understanding the concepts in still better manner.
I have already told you one of my replies to your earlier posts, the official Python documentation site is where you should go. Read the whole web site as if its a book.
Jan 6 '08 #4
bvdet
2,851 Expert Mod 2GB
Hi,

I have a number ,i have to split it into 3 digits

For Example num = 001001001001000001

After the split it should be

001
001
001
001
000
001
Is there any better way to split it

num = '001001001001000001'
x1 = num[0:3]
x2 = num[3:6]
x3 = num[6:9]
x4 = num[9:12]
x5 = num[12:15]
x6 = num[15:18]

Thanks
PSB
This is exactly what we did with re in another thread of yours:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. patt = re.compile(r'\d{1,3}')
  4. num = '00100100100100000101'
  5. print patt.findall(num)
  6.  
  7. # >>> ['001', '001', '001', '001', '000', '001']
Remember pattnum?
Taking it one more step:
Expand|Select|Wrap|Line Numbers
  1. def fixed_field(s, n=3):
  2.     return re.findall(r'\w{1,%s}' % n, s)
  3.  
  4. print fixed_field('00100100100100000101')
  5. print fixed_field('0010010010010000010110007008abc', 4)
  6.  
  7. >>> ['001', '001', '001', '001', '000', '001', '01']
  8. ['0010', '0100', '1001', '0000', '0101', '1000', '7008', 'abc']
  9. >>> 
To assign list items to variables:
Expand|Select|Wrap|Line Numbers
  1. for i, item in enumerate(fixed_field(num)):
  2.     exec 'x%s = item' % (i+1)
Jan 6 '08 #5

Post your reply

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

Similar topics

2 posts views Thread by nieuws | last post: by
9 posts views Thread by martin | last post: by
12 posts views Thread by John Frame | last post: by
8 posts views Thread by Jack | last post: by
3 posts views Thread by Eric_Dexter | last post: by
2 posts views Thread by ogo796 | last post: by
reply views Thread by rosydwin | last post: by

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.