Matrice 2 dimension, probleme valeurs

Résolu
Bonjour,

j'ai une matrice a 2 dimension, initialisée a matrice_hauteur[['-']*10]*10
je remplace certaines cases de sorte a avoir (visuelement) ca :

['A', 'A', 'A', 'A', '-', '-', '-', '-', '-', '-']
['A', 'A', 'A', 'A', '-', '-', '-', '-', '-', '-']
['A', 'A', 'A', 'A', '-', '-', '-', '-', '-', '-']
['A', 'A', 'A', 'A', '-', '-', '-', '-', '-', '-']
['A', 'A', 'A', 'A', '-', '-', '-', '-', '-', '-']
['A', 'A', 'A', 'A', '-', '-', '-', '-', '-', '-']
['A', 'A', 'A', 'A', '-', '-', '-', '-', '-', '-']
['A', 'A', 'A', 'A', '-', '-', '-', '-', '-', '-']
['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']

quand j'accede a la valeur de matrice[8][0] ca me donne : '-'
et cela vaut pour l'ensemble des valeur quand j'y accede individuelement.

quane j'affiche la ligne entiere matrice[8] ca me donne :
['A', 'A', 'A', 'A', '-', '-', '-', '-', '-', '-']

quelqu'un pourrait m'aider ?

merci

Configuration: Linux / Firefox 75.0

7 réponses

  1. Cele me donne un resultat que je n'attends pas :
                d_d=0
                d_c=0
                x=8
                y=4
                print(x,y)
                print('rr',matrice_hauteur[7:9],d_c)
                for d_d in range(0,x):
                    for d_c in range(0,y):
                        matrice_hauteur[d_d][d_c]=A
                        print(d_d,d_c)
                        if d_c==y:
                            break
                    if d_d==x:
                        break
    
    
                for d_d in range(0,10):
                    for d_c in range(0,10):
                        print(matrice_hauteur[d_d-d_c])
    
                print(matrice_hauteur[0-9])
                print(matrice_hauteur[1-9])
                print(matrice_hauteur[2-9])
                print(matrice_hauteur[3-9])
                print(matrice_hauteur[4-9])
                print(matrice_hauteur[5-9])
                print(matrice_hauteur[6-9])
                print(matrice_hauteur[7][0],matrice_hauteur[7][1],
                matrice_hauteur[7][2],matrice_hauteur[7][3],
                matrice_hauteur[7][4],matrice_hauteur[7][5],
                matrice_hauteur[7][6],matrice_hauteur[7][7],
                matrice_hauteur[7][8],matrice_hauteur[7][9]),
                print(matrice_hauteur[8][0],matrice_hauteur[8][1],
                matrice_hauteur[8][2],matrice_hauteur[8][3],
                matrice_hauteur[8][4],matrice_hauteur[8][5],
                matrice_hauteur[8][6],matrice_hauteur[8][7],
                matrice_hauteur[8][8],matrice_hauteur[8][9]),
                print(matrice_hauteur[9-9])
    
                print(matrice_hauteur[8][4])
                print(matrice_hauteur[8])
    0
    1. Il manque l'initialisation de la matrice et le changement de certaines cellules
      0
      1. matrice_hauteur = [['-']*10]*10
        matrice_largeur = [['-']*10]*10
        
        d_d=0
        d_c=0
        x=8
        y=4
        print(x,y)
        print('rr',matrice_hauteur[7:9],d_c)
        for d_d in range(0,x):
            for d_c in range(0,y):
                matrice_hauteur[d_d][d_c]='A'
                print(d_d,d_c)
                if d_c==y:
                    break
            if d_d==x:
                break
        
        
        for d_d in range(0,10):
            for d_c in range(0,10):
                print(matrice_hauteur[d_d-d_c])
        
        print(matrice_hauteur[0-9])
        print(matrice_hauteur[1-9])
        print(matrice_hauteur[2-9])
        print(matrice_hauteur[3-9])
        print(matrice_hauteur[4-9])
        print(matrice_hauteur[5-9])
        print(matrice_hauteur[6-9])
        print(matrice_hauteur[7][0],matrice_hauteur[7][1],
        matrice_hauteur[7][2],matrice_hauteur[7][3],
        matrice_hauteur[7][4],matrice_hauteur[7][5],
        matrice_hauteur[7][6],matrice_hauteur[7][7],
        matrice_hauteur[7][8],matrice_hauteur[7][9]),
        print(matrice_hauteur[8][0],matrice_hauteur[8][1],
        matrice_hauteur[8][2],matrice_hauteur[8][3],
        matrice_hauteur[8][4],matrice_hauteur[8][5],
        matrice_hauteur[8][6],matrice_hauteur[8][7],
        matrice_hauteur[8][8],matrice_hauteur[8][9]),
        print(matrice_hauteur[9-9])
        
        print(matrice_hauteur[8][4])
        print(matrice_hauteur[8])
        
        0
        1. Je ne suis pas spécialiste en Python, mais comme pour l'instant personne d'autre ne te réponds, voilà le début des mes "investigations".

          J'ai modifié ton code, pour remplir la matrice de 1 à 100, en principe.
          Au passage, ces lignes
                  if d_c==y:
                      break
              if d_d==x:
                  break
          ne servent à rien puisque tes ranges commencent à 0 pour x ou y valeurs, elles ne les atteignent donc jamais.

          Ensuite, avec une seconde itération de la matrice j'affiche tous les élèments.
          matrice_hauteur = [['-']*10]*10
          matrice_largeur = [['-']*10]*10
          
          d_d=0
          d_c=0
          x=10
          y=10
          i = 1
          
          print("init matrice")
          print("")
          for d_d in range(0,x):
              for d_c in range(0,y):
                  matrice_hauteur[d_d][d_c]=i
                  print("indices", d_d,d_c, ", i =", i, ", matrice_hauteur[d_d][d_c] =", matrice_hauteur[d_d][d_c])
                  i += 1
                  
          print("")    
          print("Vérif matrice")
          print("")
          for d_d in range(0,x):
              for d_c in range(0,y):
                  print("indices", d_d,d_c, ", matrice_hauteur[d_d][d_c] =", matrice_hauteur[d_d][d_c])


          Et là, "surprise" au lieu d'avoir des valeurs de 1 à 100, j'ai des successions de valeurs de 91 à 100.
          Tu peux voir là https://www.onlinegdb.com/rJnJ3x9KI

          Je ne connais pas la raison, et je dois passer à autre chose là maintenant, mais manifestement c'est dans l'initialisation qu'il y a un problème.
          0
          1. Bonsoir,

            Je t'explique pourquoi il ne faut pas faire l'initialisation que tu as faite, cela pose effectivement problème dans le cas où tu modifies ta matrice ensuite.

            Explication

            Je repars de ton initialisation, en créant une matrice M :
            >>> M = [['-']*10]*10


            L'instruction ci-dessous permet d'afficher la matrice ligne par ligne
            >>> print("\n".join([str(ligne) for ligne in M]))
            ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']


            Ensuite, on modifie le premier élément en position (0,0)
            >>> M[0][0] = 'A'

            Et là... patatras ! Toutes les lignes ont été modifiées...
            >>> print("\n".join([str(ligne) for ligne in M]))
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            


            Pourquoi donc ?
            Eh bien, parce que chacune des lignes dans la matrice fait référence à une seule et même ligne. Donc si tu en modifies une, toutes les autres sont modifiées.
            La preuve : elles ont le même identifiant :
            >>> M[1]
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            >>> id(M[1])
            140154560403784
            >>> M[2]
            ['A', '-', '-', '-', '-', '-', '-', '-', '-', '-']
            >>> id(M[2])
            140154560403784


            Solution

            Tout ça pour dire que tu dois changer ta manière d'initialiser la matrice.
            Tu peux faire comme cela par exemple :
            >>> M = [['-' for colonne in range(10)] for ligne in range(10)]

            On constate, cette fois-ci, que les lignes de la matrice sont différentes (au sens identifiant de l'objet liste, même si leur contenu est le même) :
            >>> id(M[0])
            140154560403720
            >>> id(M[1])
            140154560404616


            Et ça devrait être plus facile maintenant pour modifier la matrice.
            0
            1. Ha, je me disais bien qu’il y avait une histoire de référence là dessous
              0
          2. Merci beaucoup ca a marché
            0
            1. Ravi d'avoir pu aider.
              0