我正在编写一个程序来查找字符串中的数字

时间:2015-06-20 22:20:37

标签: python

此代码将检查字符串并按顺序返回数字,并提供字符串中显示的单个数字。我知道有很多方法比我的代码更好地在字符串中查找数字,这是一个赋值。我只对循环有问题,循环应该运行到字符串的结尾并终止。我无法做到这一点。

#variables
start = None
end = None
zero= None
one = None
two = None
three = None
four = None
five = None
six = None
seven = None
eight = None
nine = None
check = None
def numfind():
    #will find numbers in a string and allocate position in the string
    global zero
    global one
    global two
    global three
    global four
    global five
    global six
    global seven
    global eight
    global nine
    global end
    global start
    zero=text.find('0',end)
    one=text.find('1',end)
    two=text.find('2',end)
    three=text.find('3',end)
    four=text.find('4',end)
    five=text.find('5',end)
    six=text.find('6',end)
    seven=text.find('7',end)
    eight=text.find('8',end)
    nine=text.find('9',end)
def numstart():
    #will find the smallest number from among the previous function output and will allocate it as the start of the number, in "start" variable, will use slicing function to return the number
    global zero
    global one
    global two
    global three
    global four
    global five
    global six
    global seven
    global eight
    global nine
    global start
    for n in [zero,one,two,three,four,five,six,seven,eight,nine]:
        if start is None:
            if n != -1:
                start = n
                #print start
            else:
                continue
        else:
            if n != -1:
                if start>n:
                    start=n
                    #print start
            else:
                continue
def numend1():
    #Will find the space after numbers begin, will use "end" in the slicing function to return the first number from the string.
    global start
    global end
    end=text.find(" ",start)
    #print end
def endchecker():
    #this is some bullshit I came up with to terminate the loop, but doesn't work :(
    global zero
    global one
    global two
    global three
    global four
    global five
    global six
    global seven
    global eight
    global nine
    global start
    global end
    global check
    for n in [zero,one,two,three,four,five,six,seven,eight,nine]:
        if check is None:
            if n != -1:
                check = n
                #print check
            else:
                check=n
        else:
            if n != -1:
                check=n
                    #print check
            else:
                if check>n:
                    continue
                else:
                    check = n
text=raw_input("Please enter a string containing a set of numbers:") 
while True:
    numfind()
    endchecker()
    print check
    if check == -1:
        break
    numstart()
    numend1()
    try:
        if end!=-1:
            number1=float(text[start:end])
            print number1
        else:
            number1=float(text[start:])
            print number1
            break
    except:
        print "Error"
        break
    print "End of Program"

2 个答案:

答案 0 :(得分:0)

我已经检查了你的代码,但是对你要做的事情有点困惑,似乎你试图从文本中获取数字子串。

#python
text=raw_input("Please enter a string containing a set of numbers:")

s = ""
for c in text:
    if c >= '0' and c <= '9':
        s += c
    else:
        if len(s) != 0:
            print s
            s = ""
if len(s) != 0:
    print s

print "End of Program"

希望能帮到你一些。

一些建议,在编码时更好的调试,不要编写大块代码但从未尝试过运行它,一个小但有用的代码要好得多。

顺便说一句,最好避免使用Global,这会使你的代码难以阅读,函数参数和返回值更好。

答案 1 :(得分:0)

伙计,您的代码中发生了什么?我不确定你要对你的代码做什么。如果你需要做的就是检查一个字符串是否包含数字并按顺序显示这些数字,那么你可以使用正则表达式非常容易地完成它们。

#python
import re
string = raw_input("Enter a string: ")

match = re.findall(r'\d', string)

# print the found numbers in the same sequence
# as found in the string
for i in match:
    print i,

如果要按有序顺序打印数字,可以使用sorted()函数。修改for循环如下。

for i in sorted(match):
    print i,

如果您要查找更长的子字符串,可以按如下方式修改模式:

match = re.findall(r'\d+', string)

或非贪婪版本:

match = re.findall(r'\d+?', string)

同样,我不确定这是否能解决您的问题。