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

split 1234 into ('1', '2', '3', '4')

6
Hi,

I was wondering if there is a simple way to split 1234 into a string of ('1', '2', '3', '4').

Also, is there a way to test whether an element of a string (or list) is an integer. For instance, if i had a list a = ['1', '2', '3', '4'], the elements are all integers, the function needs to return True.

However, if the list was ['1', '2', '3', 'x'], how would i distinguish that 'x' is not an integer and therefore the function would return False.

So far i have used a for loop:
for i in a:
if int(i) is type(int):
return True
if int(i) is not type(int):
return False

but this isn't working at all.

Thanks for your help :)
Aug 23 '08 #1
10 47399
boxfish
469 Expert 256MB
Hi,
The str() function takes an integer argument and returns a string. You can also use the list() and tuple() functions to convert strings to lists and tuples. Your loop has two problems. When it finds a number, it returns true before even checking the other strings. You need to make a loop that quits as soon as it finds a non-number string, and only return true if it goes all the way through the list with no problems. Secondly, int(i) will crash your program with a ValueError if it finds a string that does not convert to an integer. Use this to your advantage. Make a try-except block to detect whether the value is an int or not.
Hope this helps.
Aug 23 '08 #2
Also i would suggest that you'd change your test to:
Expand|Select|Wrap|Line Numbers
  1. if type(i) is type(1):
  2.    return True
  3. else:
  4.    return False
  5.  
Elias
Aug 23 '08 #3
Smygis
126 100+
You can do this.
Expand|Select|Wrap|Line Numbers
  1. >>> for i in repr(1234):
  2.     if i.isdigit():
  3.         print("OMFG {0} is a digit".format(i))
  4.  
  5.  
  6. OMFG 1 is a digit
  7. OMFG 2 is a digit
  8. OMFG 3 is a digit
  9. OMFG 4 is a digit
  10. >>> 
  11.  
and if we throw in an 'X':
Expand|Select|Wrap|Line Numbers
  1. >>> for i in repr(1234)+"X":
  2.     if i.isdigit():
  3.         print("OMFG {0} is a digit".format(i))
  4.     else:
  5.         print("OMFG {0} is not a digit".format(i))
  6.  
  7.  
  8. OMFG 1 is a digit
  9. OMFG 2 is a digit
  10. OMFG 3 is a digit
  11. OMFG 4 is a digit
  12. OMFG X is not a digit
  13.  
You can also use the isinstance function:
Expand|Select|Wrap|Line Numbers
  1. >>> isinstance(10, int)
  2. True
  3. >>> isinstance("omg", list)
  4. False
  5. >>> isinstance((), tuple)
  6. True
  7. >>> isinstance("", (list, str, tuple))
  8. True
  9.  
Aug 23 '08 #4
boxfish
469 Expert 256MB
Also i would suggest that you'd change your test to:
Yes, but that test doesn't work anyway. "1", "2", and "1234" are still strings, so they aren't of type int.
Aug 23 '08 #5
ghostdog74
511 Expert 256MB
Hi,

I was wondering if there is a simple way to split 1234 into a string of ('1', '2', '3', '4').
Expand|Select|Wrap|Line Numbers
  1. >>> a=1234
  2. >>> list(str(a))
  3. ['1', '2', '3', '4']
  4.  
Also, is there a way to test whether an element of a string (or list) is an integer. For instance, if i had a list a = ['1', '2', '3', '4'], the elements are all integers, the function needs to return True.
Expand|Select|Wrap|Line Numbers
  1. if not [i for i in a if not  i.isdigit()]: print "True"
  2.  
Aug 24 '08 #6
boxfish
469 Expert 256MB
if not [i for i in a if not i.isdigit()]: print "True"
Wow, that syntax is really wierd. I had no idea you could do that.
Aug 24 '08 #7
bvdet
2,851 Expert Mod 2GB
Another possibility:
Expand|Select|Wrap|Line Numbers
  1. >>> False not in [c.isdigit() for c in '1234']
  2. True
  3. >>> False not in [c.isdigit() for c in '1234z']
  4. False
  5. >>> 
Aug 24 '08 #8
boxfish
469 Expert 256MB
If you can do things like that, then what's the map function for?
Aug 25 '08 #9
ss30
6
Expand|Select|Wrap|Line Numbers
  1. >>> a=1234
  2. >>> list(str(a))
  3. ['1', '2', '3', '4']
  4.  

Expand|Select|Wrap|Line Numbers
  1. if not [i for i in a if not  i.isdigit()]: print "True"
  2.  

that's brilliant! and so simple!

Thanks heaps :)
Aug 25 '08 #10
bvdet
2,851 Expert Mod 2GB
If you can do things like that, then what's the map function for?
Built-in function map() has largely been replaced by list comprehensions. Example:
Expand|Select|Wrap|Line Numbers
  1. map(function, some_list)
is equivalent to:
Expand|Select|Wrap|Line Numbers
  1. [function(x) for x in some_list]
Aug 25 '08 #11

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

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"
2
by: Sunil Menon | last post by:
Dear All, I would like run a program that does an IISReset when aspnet_wp.exe (PID: 1234) was recycled...In IIS service we have a property "Recovery" where you can specify a program to run...but...
3
by: Eddy Soeparmin | last post by:
Hi, How do I apply phone format in a string field? for example (770) 123-1234. Please let me know. Thanks. Eddy
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
4
by: penzer | last post by:
how to exchange 1234(number) -bytes sequence in the C#?
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: 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...
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: 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:
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
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
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...

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.