|
|
|
|
Salut,
Tout d'abord tu crées une liste de chiffre (mais en caractères): chiffres= ('1','2','3','4','5','6','7','8','9')
Metton que tu aies cette chaine à vérifier: chaine="jsldkgh\rm.qeigj534qshfsgn\n" Il suffit de faire un test d'appartenance à chiffres pour chaque caractère, et si un élément n'appartient pas à chiffres, on l'élimine. for caractere in chaine: if not caractere in chiffre: chaine=chaine.split(caractere) chaine="".join(chaine) Et voilà. Il doit y avoir plus rapide je pense.... |
--Kilian:
Merci pour cette reponse eclaire! Je vous tiens au courant.. Je precise, en fait j'essaie d'ecrire un programme pour faire un regression lineaire: Il faut ouvrir un fichier de donnees (juste des coordonnees). Comme file.read() pond une string, je dois en extraire les coordonnees que je vais mettre dans une liste. Ensuite on verra pour la manipulation des donnees..
|
Coucou, re,
Dans mon programme (ich bin keloss) je tente de separer une liste en deux: avec un sur 2 elements de la premiere liste dans chaque liste fille. J'ai commence par faire: def getxnylist(listemere): xlist = [n for n in listemere if listemere.index(n) % 2 == 0] ylist = [n for n in listemere if listemere.index(n) % 2 != 0] return xlist, ylist Ahem, evidemment si on a 2 fois le meme element dans la liste mere ca fausse tout.. J'ai donc ruse: def getxlist(firstlist): lx = [] if firstlist: lx = [firstlist[0]] + getxlist(firstlist[2:]) return lx def getylist(firstlist): ly = [] if firstlist: ly = [firstlist[1]] + getylist(firstlist[2:]) return ly def getxnylist(firstlist): """Creates two lists. Each list has one out of two elements of the first list. The first list will represent x-values, the second y-values.""" lx = getxlist(firstlist) ly = getylist(firstlist) return lx, ly Notez la fonction recursive ;-) (en fait j'avais survole le manuel de Ocaml juste avant). Mais y a-t-il une technique plus rapide (un moyen de slicer une liste avec un pas defini?), plus malin, plus cool: un truc qui pythonne?! Peace, ~Wonderful Bangladesh~ |
Bon mais j'ai tout de meme une beta-beta version a vous offrir (pour ces foules silencieuses qui attendaient un programme de regression lineaire..)
import sys
def main():
"""Will calculate a simple linear regression."""
try:
filename = raw_input("Which file to import? ")
f = open(filename, "r")
try:
stringdata = f.read()
f.close()
okdata = convert(stringdata)
xlist, ylist = getxnylist(okdata)
T = len(xlist)
Ex = sum(xlist)
Ey = sum(ylist)
Exsq = listsqsum(xlist)
Exy = listxysum(okdata)
b2 = (T*Exy - Ex*Ey)/(T*Exsq - Ex**2)
b1 = (Ey/T) - b2*(Ex/T)
print "yt = " + str(b1) + " + (" + str(b2) + ")Xt"
except ValueError:
print "Invalid values in there, sorry, only take numbers.."
sys.exit()
except IOError:
print "File does not exist!"
sys.exit()
def convert(ustring):
"""Formats input text file into a list of floating numbers.
Admits anything as separators between numbers but '.'. Will take all nu-
bers in consecutively, ignoring anything in between. Make sure your x-values
are first."""
accepted = ('.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
for char in ustring:
if not char in accepted:
ustring = ustring.split(char)#knock off anything not in
ustring = " ".join(ustring)#accepted, replace by whitespace
ustring = ustring.split()#then split on whitespace
ustring = [float(num) for num in ustring]#make sure numbers are decimals
return ustring
def alternativeconvert(ustring):
"""Formats through regular expression."""
pat = re.compile('[^0-9.]')#import re..
ustring = re.split(pat, ' ', ustring)
ustring = ustring.split()
ustring = [decimal.Decimal(num) for num in ustring]#import decimal..
return ustring
def getxlist(firstlist):
lx = []
if firstlist:
lx = [firstlist[0]] + getxlist(firstlist[2:])
return lx
def getylist(firstlist):
ly = []
if firstlist:
ly = [firstlist[1]] + getylist(firstlist[2:])
return ly
def getxnylist(firstlist):
"""Creates two lists. Each list has one out of two elements of the first list.
The first list will represent x-values, the second y-values."""
lx = getxlist(firstlist)
ly = getylist(firstlist)
if len(lx) != len(ly):
print "Missing coordinates.. Make sure you have pairs."
sys.exit()
else:
pass
return lx, ly
def listsqsum(l):
"""Sums the squares of the elements of a list."""
total = 0
if l:
total = (l[0])**2 + listsqsum(l[1:])
return total
def listxysum(l):
"""Sums the product of the l-list elements two by two."""
total = 0
if l:
total = l[0]*l[1] + listxysum(l[2:])
return total
if __name__ == "__main__":
main()
#TODO:
#code a general summation function --> summation(expression, indexstart, indexstop)
#as yet one can only calculate y=b1+b2x --> allow for different transformations on x
#expand simple linear regression to multiple regression
#add standard errors, R**2 and other interesting values
#def summation(expr, i=0, n):
Bon c'est un debut :-( Sinon est-ce que quelqu'un connait un bon tuto sur les librairies standard, comme sys et os: ca m'a l'air vachement marrant, j'ai hate de tester les system calls et les flags et tout et tout (nohup here i come!). Peace, ~Wonderful Bangladesh~
|
Excellent!
Merci! Je me sens pousser une langue fourchue et un corps glissant! Ca va pythonner! Peace, |
Résultats pour [Python] manipuler un string
Résultats pour [Python] manipuler un string
Résultats pour [Python] manipuler un string
Résultats pour [Python] manipuler un string
Résultats pour [Python] manipuler un string
Résultats pour [Python] manipuler un string