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

Split() [solved with re.split]

How do make a split on a string at a few different places? For example i wanna make a split on 5*4+1-1+3 to put all the numbers in a list. How do i do that?
Oct 26 '06 #1
5 3552
Geos
2
Hi,

This should do the job although I don't believe it is the best solution.


a = "1+2-3+5-6*8"
z = []
for x in a:
try :
int(x)
z.append(x)
except:
pass
print z

greetz Geos
Oct 26 '06 #2
bartonc
6,596 Expert 4TB
Hi,

This should do the job although I don't believe it is the best solution.


a = "1+2-3+5-6*8"
z = []
for x in a:
try :
int(x)
z.append(x)
except:
pass
print z

greetz Geos
Geos, Please use code tags so your post looks like this:
Expand|Select|Wrap|Line Numbers
  1. a = "1+2-3+5-6*8"
  2. z = []
  3. for x in a:
  4.     try :
  5.         int(x)
  6.     z.append(x)
  7.     except:
  8.         pass
  9. print z
  10.  
Otherwise, it is very hard to use. Thanks,
Barton
Oct 26 '06 #3
fuffens
38
You can use a regular expression to split the string. You still have to go through the list to convert to integers as in the previous example though.

Expand|Select|Wrap|Line Numbers
  1. import re
  2. re.split('[*+-]', '5*4+1-1+3')
Best regards
/Fredrik
Oct 26 '06 #4
bartonc
6,596 Expert 4TB
You can use a regular expression to split the string. You still have to go through the list to convert to integers as in the previous example though.

Expand|Select|Wrap|Line Numbers
  1. import re
  2. re.split('[*+-]', '5*4+1-1+3')
Best regards
/Fredrik
This is VERY cool! I've used the re module, but hadn't come across this usage. Thanks Fredrik!
Barton
Oct 27 '06 #5
kudos
127 Expert 100+
Here is a possible solution...

Expand|Select|Wrap|Line Numbers
  1. for a in "5*4+1-1+3".split("*"):
  2.  for b in a.split("+"):
  3.   for c in b.split("-"):
  4.    print c
  5.  
-kudos


How do make a split on a string at a few different places? For example i wanna make a split on 5*4+1-1+3 to put all the numbers in a list. How do i do that?
Oct 27 '06 #6

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

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: Will McGugan | last post by:
Hi, I'm curious about the behaviour of the str.split() when applied to empty strings. "".split() returns an empty list, however.. "".split("*") returns a list containing one empty string. ...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
7
by: Mark A | last post by:
If server 01 running HADR in the primary role crashes, and the DBA does a HADR takeover by force on the 02 server to switch roles, then the 02 server is now the primary. What happens when the...
19
by: Mark 123 | last post by:
I just read at http://groups.google.com/group/microsoft.public.access.forms/browse_frm/thread/766ba7b493eacb63/8a7d5504ee848c21?lnk=st&q=migrate+access+97+to+access+2007&rnum=1#8a7d5504ee848c21...
2
by: AAOO (Sean) | last post by:
When I try to access the back end of a split database, I get the error message, that it's not trusted by access, and that I should move it to my computer, or an accessible network location? What...
2
by: programmo | last post by:
I need to divide in two string an article, to split in more pages: I have this string: <hello word> <hello word2>
12
by: Siah | last post by:
Hi, I need to convert the string: '(a, b, "c", d, "e")' into the following list . Much like a csv reader does. I usually use the split function, but this mini-monster wouldn't properly get...
8
by: John Google | last post by:
Hi, I've been asked to amend an Access 2002 DB that has been used by users using the MDE version. I have, what seems to be the original MDB version containing all the front end stuff but, by...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.