Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check Array for numbers
#1
Hi, I want to do these steps.
1) Spit a string into an array by spaces
2) Check if each array element is a number

The isinstance(variable, int) function works if I make the array myself. But if I split the array like the code below, it doesn't. Any ideas?

stringA = "donkey 2 pirate"
SplitArray = stringA.split(" ")
for bb in SplitArray:
print(isinstance(bb, int));
Reply
#2
str.split gives you a list of strs. You need to convert the strs to ints explicitly.

By the way, in case you're young and not thinking about this, the username you've chosen is unlikely to do you good and may do you harm. Something to think about, perhaps.
Reply
#3
Thanks for the reply. The only way I can think to do this is to use the try/except functions with the int() function. And if it gives an error, it's not an integer. Is this the only way or can I directly somehow figure out if array elements could be numbers?

I will change my username if there's an option to
Reply
#4
(Feb-29-2020, 09:37 PM)GloryHoleLover Wrote: Thanks for the reply. The only way I can think to do this is to use the try/except functions with the int() function. And if it gives an error, it's not an integer. Is this the only way or can I directly somehow figure out if array elements could be numbers?
try/catch is a natural solution. What are you going to do when it's not an int?

Quote:I will change my username if there's an option to
You can make a post here: https://python-forum.io/Forum-Private-Inquiries
Reply
#5
The below code should work fine.

The string is split when a white space is found and stored to a list.
For loop is used to determine whether the element is a string or an integer.


a = "donkey 2 pirate"
print("Input string is: %s"%a)
print("\n")
l=[]
l=[str(x) for x in a.split(" ")]


print("Output is \n")

for i in l:
    if i.isalpha() is True:
        print (i,"\t is a string ")
    else:
        if i.isdigit() is True:
            print (i, "\t is an integer")
Output:
Input string is: donkey 2 pirate Output is donkey is a string 2 is an integer pirate is a string
Reply
#6
Pranav, is it really appropriate to provide a solution for someone?
Reply
#7
(Mar-01-2020, 06:45 PM)ndc85430 Wrote: Pranav, is it really appropriate to provide a solution for someone?

I am new to this forum. Will only explain the logic and provide minimal code henceforth.
Reply
#8
(Mar-01-2020, 04:21 AM)Pranav Wrote:
a = "donkey 2 pirate"
print("Input string is: %s"%a)
print("\n")
l=[]
l=[str(x) for x in a.split(" ")]


print("Output is \n")

for i in l:
    if i.isalpha() is True:
        print (i,"\t is a string ")
    else:
        if i.isdigit() is True:
            print (i, "\t is an integer")
In-line comments:
# Not a descriptive variable name.
a = "donkey 2 pirate"

# This is old-style string formatting. People nowadays are more likely to
# use the format() method of str, or string interpolation (format strings)
# in newer versions of Python.
print("Input string is: %s"%a)

print("\n")

# No need to declare the variable twice, this line can be omitted.
l=[]

# No need for the str call, so no need for a comprehension.
l=[str(x) for x in a.split(" ")]


print("Output is \n")

# Again, not descriptive variable names.
for i in l:
    # I'm not sure the point of this check, but omit "is True"
    if i.isalpha() is True:
        print (i,"\t is a string ")
    else:
        # isdigit will reject whitespace, which int() accepts.
        if i.isdigit() is True:
            print (i, "\t is an integer")
I don't say this to discourage new posters, who I encourage to stay engaged if you're really interested in doing so, but I don't want people reading this answer to develop bad habits.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,430 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  convert array of numbers to byte array adetheheat 3 2,732 Aug-13-2020, 05:09 PM
Last Post: bowlofred
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,667 May-09-2019, 12:19 PM
Last Post: Pleiades
  20 x 20 Integer array with random numbers harryk 3 3,289 Jul-28-2018, 03:38 PM
Last Post: harryk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020