473,545 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
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 47479
boxfish
469 Recognized Expert Contributor
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
Elias Alhanatis
56 New Member
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 New Member
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 Recognized Expert Contributor
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 Recognized Expert Contributor
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 Recognized Expert Contributor
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 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
If you can do things like that, then what's the map function for?
Aug 25 '08 #9
ss30
6 New Member
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

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

Similar topics

5
31158
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 will split myString up using the delimiter of 1 space so that
9
4333
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. I would have expected the second example to have also returned an empty
4
5768
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
2510
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 IIS does not get reset when aspnet_wp.exe (PID: 1234) was recycled...so I would like to have an IISReset program run on this event....how do I...
3
12504
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
34898
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, bytesRead) .... Dim parts() As String = data.Split(vbCrLf)
12
3202
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 without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1)...
4
2067
by: penzer | last post by:
how to exchange 1234(number) -bytes sequence in the C#?
2
1650
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 problem to me. can someone look at my code and please help me maybe i am missing something. <?php $q=$_POST; $jaar=$_POST;...
0
7502
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7692
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7946
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
744
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.