Python:分析输入以查看它是整数,浮点数还是字符串

时间:2014-03-06 03:34:04

标签: python string

为了判断输入是整数,浮点数还是字符串,我已经在这一天工作了一两天。

简而言之,该程序旨在将每个输入转换为字符串,循环遍历每个字符串并检查列表数字。如果字符串的所有数字都是整数,如果它有'。'它是一个浮点数,如果它没有,它不是一个数字。明显的缺陷是包含字母和'。'的字符串。在这个程序中被认为是漂浮物。

这个程序的最终目标是打开文本文件,看看输入是int,float还是其他。

问题

- 有没有办法进一步优化这个程序

- 如何进一步修改此程序以打开文本文件,读取,分析和写入哪个输入在哪个列表中

第一篇文章!!!

#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)

5 个答案:

答案 0 :(得分:4)

如果您正在阅读文本文件,那么您将始终获得字符串,因此您可以使用int()float()决定每个元素属于哪种类型并捕获异常:

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]

答案 1 :(得分:1)

您不必将数据转换为字符串并处理它们,您只需使用type函数来获取类型并将结果放入dict这样的内容。

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']

答案 2 :(得分:1)

你可以用这种方式实际检查

 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

答案 3 :(得分:1)

为寻求更小和更优化代码的人提供更好的解决方案

  

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)

答案 4 :(得分:0)

你可以使用python的eval函数。 检查输入字符串是否属于什么类型只需使用type(eval(你的字符串))。