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 :)
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.
Also i would suggest that you'd change your test to: -
if type(i) is type(1):
-
return True
-
else:
-
return False
-
Elias
You can do this. -
>>> for i in repr(1234):
-
if i.isdigit():
-
print("OMFG {0} is a digit".format(i))
-
-
-
OMFG 1 is a digit
-
OMFG 2 is a digit
-
OMFG 3 is a digit
-
OMFG 4 is a digit
-
>>>
-
and if we throw in an 'X': -
>>> for i in repr(1234)+"X":
-
if i.isdigit():
-
print("OMFG {0} is a digit".format(i))
-
else:
-
print("OMFG {0} is not a digit".format(i))
-
-
-
OMFG 1 is a digit
-
OMFG 2 is a digit
-
OMFG 3 is a digit
-
OMFG 4 is a digit
-
OMFG X is not a digit
-
You can also use the isinstance function: -
>>> isinstance(10, int)
-
True
-
>>> isinstance("omg", list)
-
False
-
>>> isinstance((), tuple)
-
True
-
>>> isinstance("", (list, str, tuple))
-
True
-
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.
Hi,
I was wondering if there is a simple way to split 1234 into a string of ('1', '2', '3', '4'). -
>>> a=1234
-
>>> list(str(a))
-
['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. -
if not [i for i in a if not i.isdigit()]: print "True"
-
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.
bvdet 2,851
Recognized Expert Moderator Specialist
Another possibility: - >>> False not in [c.isdigit() for c in '1234']
-
True
-
>>> False not in [c.isdigit() for c in '1234z']
-
False
-
>>>
boxfish 469
Recognized Expert Contributor
If you can do things like that, then what's the map function for?
-
>>> a=1234
-
>>> list(str(a))
-
['1', '2', '3', '4']
-
-
if not [i for i in a if not i.isdigit()]: print "True"
-
that's brilliant! and so simple!
Thanks heaps :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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"
|
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...
|
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
| |
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)
|
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)...
|
by: penzer |
last post by:
how to exchange 1234(number) -bytes sequence in the C#?
|
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;...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| | |