IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Significations de fonctions python


Sujet :

Python

  1. #1
    Invité
    Invité(e)
    Par défaut Significations de fonctions python
    Bonjour,

    Je suis débutante en python et j'aimerais savoir ce que font exactement les fonctions des programmes suivants :

    def makeTSP(nCities)
    positions = 2*random.rand(nCities,2)-1;
    distances = zeros((nCities,nCities))

    for i in range(nCities):
    for j in range(i+1,nCities):
    distances[i,j] = sqrt((positions[i,0] - positions[j,0])**2 + (positions[i,1] - positions[j,1])**2);
    distances[j,i] = distances[i,j];

    return distances

    def greedy(distances):
    nCities = shape(distances)[0]
    distanceTravelled = 0

    # Need a version of the matrix we can trash
    dist = distances.copy()

    cityOrder = zeros(nCities)
    cityOrder[0] = random.randint(nCities)
    dist[:,cityOrder[0]] = Inf

    for i in range(nCities-1):
    cityOrder[i+1] = argmin(dist[cityOrder[i],:])
    distanceTravelled += dist[cityOrder[i],cityOrder[i+1]]
    # Now exclude the chance of travelling to that city again
    dist[:,cityOrder[i+1]] = Inf

    # Now return to the original city
    distanceTravelled += distances[cityOrder[nCities-1],0]

    return cityOrder, distanceTravelled

    c'est à dire les fonctions random.rand, zeros, range, shape, rand.int, le remplissage de matrice etc...

    Cordialement

  2. #2
    Expert confirmé
    Avatar de fred1599
    Homme Profil pro
    Lead Dev Python
    Inscrit en
    Juillet 2006
    Messages
    4 053
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Lead Dev Python
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2006
    Messages : 4 053
    Par défaut
    Merci de mettre votre code avec les balises prévues à cet effet ( balise en haut à droite # )

    Sinon le code est illisible !

  3. #3
    Expert confirmé

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 305
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 305
    Par défaut
    Salut,
    ...
    c'est à dire les fonctions random.rand, zeros, range, shape, rand.int, le remplissage de matrice etc...
    Mais c'est simple:

    random.randint(a, b): Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).

    range(start, stop[, step]): The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method). If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. If step is zero, ValueError is raised.
    For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.
    For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.
    A range object will be empty if r[0] does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.
    Ranges containing absolute values larger than sys.maxsize are permitted but some features (such as len()) may raise OverflowError.

    zeros, shape: voir dans le code source.

    rand.int: non, randint, voir plus haut.

    le remplissage de matrice: ??

    Je te propose de commencer par le début: http://python.developpez.com/cours/apprendre-python3/

Discussions similaires

  1. Réponses: 3
    Dernier message: 03/07/2009, 09h58
  2. [langage] traduction d'un fonction python en perl
    Par ay_pepito dans le forum Langage
    Réponses: 3
    Dernier message: 04/12/2007, 15h06
  3. [Python API] Appeller une fonction Python depuis le C++
    Par wetneb dans le forum Bibliothèques
    Réponses: 0
    Dernier message: 18/10/2007, 12h49
  4. nom de fonction python
    Par deb75 dans le forum Général Python
    Réponses: 4
    Dernier message: 31/03/2006, 21h54
  5. fonction python similaire à find en bash
    Par deb75 dans le forum Général Python
    Réponses: 1
    Dernier message: 21/12/2005, 11h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo