Reputation: 147
I've been working on this for a day or two in order to tell if an input is an integer, float or string.
In short the program is designed to turn every input into a string, loop through each string and check through the list digits. If the string has all digits its an integer, if it has a '.' its a float, and if it has none it's not a number. The obvious flaw is strings containing letters and '.' which would be considered floats in this program.
The end goal for this program is to open text files and see what input is an int, float, or other.
Questions
-Is there any way to further optimize this program
-How can I further modify this program to open text files, read, analyze, and write which input is in which list
First post!!!
#Checks input to see if input is integer, float, or character
integer = []
float = []
not_number = []
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
input_list = [100, 234, 'random', 5.23, 55.55, 'random2']
for i in input_list:
i = str(i)
length = len(i)
count = 0
marker = 0
for j in i:
for k in digits:
if k == j:
count = count + 1
#k loops through digits to see if j single character
#string input is number
if count == length:
integer.append(i)
marker = 1
#count is equal to length if entire string is integers
if j == '.':
float.append(i)
marker = 1
#Once '.' is found, input is "considered" a float
if marker == 1:
break
else:
not_number.append(i)
#If code above else proves that input is not a number the
#only result is that it isn't a number
print ('Integers: ', integer)
print ('Float: ', float)
print ('Not Numbers', not_number)
Upvotes: 1
Views: 6755
Reputation: 147
Better solution to those looking for smaller and more optimized code
integer = []
floats = []
not_number = []
full_list = [100, 234, 'random', 5.23, 55.55, 'random2', 'vdfj.324.23tk.sdfklsj']
for i in full_list:
if isinstance(i, int):
integer.append(i)
elif isinstance(i, float):
floats.append(i)
else:
not_number.append(i)
print ('Integers ', integer)
print ('Floats ', floats)
print ('Not numbers ', not_number)
Upvotes: 1
Reputation: 2323
If you are reading from a text file, you will get always string, so you can decide to which type belong each element using int()
and float()
and trapping the exception:
integers = []
floats = [] # Don't use float as a variable, it will override a built-in python function
not_number = []
# I modified this list so all the elements are string, if you already have ints and floats, you can use type() to know where to append
input_list = ["100", "234", 'random', "5.23", "55.55", 'random2']
for i in input_list:
value = None
try:
value = int(i)
except ValueError:
try:
value = float(i)
except ValueError:
not_number.append(i)
else:
floats.append(value)
else:
integers.append(value)
print(not_number)
print(floats)
print(integers)
# ['random', 'random2']
# [5.23, 55.55]
# [100, 234]
Upvotes: 4
Reputation: 1849
You can actually check it in this way
if type(i) is IntType:
#do something
if type(i) is StrType:
#do something
if type(i) is FloatType:
#do something
http://docs.python.org/2/library/types.html
Upvotes: 1
Reputation: 101
you can make use of the eval function of python. for checking whether input string is of what type simply use type(eval( your string )).
Upvotes: 0
Reputation: 239443
You don't have to convert the data to string and process them, you can simply use type
function to get the type and put the result in a dict
like this.
input_list = [100, 234, 'random', 5.23, 55.55, 'random2']
result = {}
for item in input_list:
result.setdefault(type(item), []).append(item)
print('Integers: ', result[int])
# Integers: [100, 234]
print('Float: ', result[float])
# Float: [5.23, 55.55]
print('Not Numbers', result[str])
# Not Numbers ['random', 'random2']
Upvotes: 1