/ / Sto scrivendo un programma per trovare i numeri in una stringa - python

Sto scrivendo un programma per trovare numeri in una stringa - python

Questo codice controllerà una stringa e restituirà i numeriin sequenza e fornirà numeri individuali come appaiono nella stringa. So che ci sono modi molto migliori del mio codice per trovare numeri nelle stringhe, questo è un compito. Ho solo problemi con il ciclo, il ciclo dovrebbe essere eseguito fino alla fine della stringa e terminare. Non sono in grado di farlo.

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

risposte:

0 per risposta № 1

Ho controllato il tuo codice, ma un po 'confuso su quello che stai per fare, sembra che tu stia cercando di ottenere le sottostringhe del numero da un testo.

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

Spero che ti possa aiutare.

alcuni consigli, è meglio eseguire il debug durante la codifica, non scrivere un grosso blocco di codice ma non provare mai ad eseguirlo, un piccolo ma utile codice è molto meglio.

A proposito, è molto meglio evitare l'uso di Global, che renderà difficile la lettura del codice, i parametri di funzionamento e i valori di ritorno sono migliori.


0 per risposta № 2

Amico, cosa sta succedendo nel tuo codice? Non sono esattamente sicuro di cosa stai cercando di fare con il tuo codice: se tutto ciò che devi fare è controllare una stringa, se contiene numeri e visualizzare quei numeri in sequenza, allora puoi farli molto facilmente usando le espressioni regolari.

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

Se si desidera stampare i numeri nella sequenza ordinata, è possibile utilizzare la funzione Sort (). Modificare il ciclo for come segue.

for i in sorted(match):
print i,

Se si cercano sottostringhe più lunghe, è possibile modificare il motivo come segue:

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

O la versione non golosa:

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

Di nuovo, non sono esattamente sicuro che questo risolva il tuo problema.