473,385 Members | 1,919 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,385 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 4667
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

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

Similar topics

2
by: nieuws | last post by:
Hi, I was trying to do the following. It's my first php "project", so it's quiet logic that i have some problems. Perhaps the php community might help. It's about this : I have a txt file...
9
by: martin | last post by:
Hi, a very newbie question. How do I split the adress and number to 2 variables? ex. "Kingsroad 347" = variabel1 = "Kingsroad" variabel2 = "347" Ill guess i have to search the string from...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
12
by: John Frame | last post by:
Hi, I've got a Python program that I'm trying to edit, and I need some help. If I would like to read a matrix from a previously created text file into a two dimensional array, how would I do...
8
by: Jack | last post by:
Hello, I need to split: 2 1066 1.30 172.90 1065.9 -14.2 3.0 -13.3 0.1 3 1528 1.00 188.10 1527.8 -23.3 3.0 -20.9 0.1 4 2007 0.60 182.60 2006.7 -30.0 2.3 -25.9 0.1 5 2484 1.00 195.20 2483.7...
2
by: Transcend2030 | last post by:
Hi, I'm having problems with string.split() My problem is; I have a sentence which the user inputs, I then input a word and the number of times that word appears in the sentence is displayed. ...
7
thatos
by: thatos | last post by:
Here is the EdgeList class class Graph { protected int numvertices; protected int numedges; protected boolean directed; protected EdgeList adjlist ; // Constructors
3
by: Eric_Dexter | last post by:
I am trying to take some data in file that looks like this command colnum_1 columnum_2 and look for the command and then cange the value in the collum(word) number indicated. I am under...
2
by: ogo796 | last post by:
Hi guys am having a problem with a split(),i retrieve line from the text file and i wanna split that line.i manage to split two words but splitting the string enclosed on brackets it seems to be a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.