pedrosorio Posted October 16, 2009 at 08:09 PM Report Share #291929 Posted October 16, 2009 at 08:09 PM def play(board): score=-10000 for i in range(3): for j in range(3): if board[i][j]==0: board[i][j]=me s=scoring(features(board)) print s,score if s>score: print "wtf",s,score score = s bmax = list(board) print bmax,board board[i][j]=0 print bmax,board return bmax Não percebo porquê mas bmax e board são sempre iguais, isto é, quando faço board[j] = 0, altera-me também o bmax... Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
newbeen Posted October 16, 2009 at 08:21 PM Report Share #291935 Posted October 16, 2009 at 08:21 PM Le este artigo, http://henry.precheur.org/2009/2/11/Python:_copying_a_list_the_right_way.html python faz ambos bmax e board apontarem para a mesma lista.... Edit: Depois de ler melhor o teu código estas a copiar da maneira correcta, tenta fazer id(bmax), id(board) e ve se esta a apontar para o mesmo endereço de memoria RHCE - 120-062-534 Link to comment Share on other sites More sharing options...
pedrosorio Posted October 16, 2009 at 08:34 PM Author Report Share #291943 Posted October 16, 2009 at 08:34 PM Le este artigo, http://henry.precheur.org/2009/2/11/Python:_copying_a_list_the_right_way.html python faz ambos bmax e board apontarem para a mesma lista.... Edit: Depois de ler melhor o teu código estas a copiar da maneira correcta, tenta fazer id(bmax), id(board) e ve se esta a apontar para o mesmo endereço de memoria Sim, eu sei que copiar estruturas em python tem o seu quê de pouco intuitivo. O problema é que acabei de fazer isso e dá-me valores de memória diferentes. Estou parvo... Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
Betovsky Posted October 16, 2009 at 09:28 PM Report Share #291961 Posted October 16, 2009 at 09:28 PM Ora como são listas de listas, o mais provável é que as listas internas estejas a ser copiadas de maneira não correcta. Ou seja, apesar de ambas as listas serem diferentes (diferentes endereços de memória) os seus elementos são iguais (as listas internas tem os mesmos endereços de memória). Podes verificar isso se fizeres id(bmax[0]) e id(board[0]) "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk Link to comment Share on other sites More sharing options...
pedrosorio Posted October 16, 2009 at 09:34 PM Author Report Share #291962 Posted October 16, 2009 at 09:34 PM Ora como são listas de listas, o mais provável é que as listas internas estejas a ser copiadas de maneira não correcta. Ou seja, apesar de ambas as listas serem diferentes (diferentes endereços de memória) os seus elementos são iguais (as listas internas tem os mesmos endereços de memória). Podes verificar isso se fizeres id(bmax[0]) e id(board[0]) Très bien =) Como é que se copia listas de listas correctamente em python, então? Parece ser uma linguagem tão acessível em que o programador não tem que se preocupar com os pormenores de base... de certeza tem uma forma elementar de copiar listas sem tanta chatice Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
Betovsky Posted October 16, 2009 at 09:45 PM Report Share #291968 Posted October 16, 2009 at 09:45 PM Boa pergunta. Não sei 😛 Experimenta usar o copy.deepcopy() "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk Link to comment Share on other sites More sharing options...
pedrosorio Posted October 16, 2009 at 09:48 PM Author Report Share #291970 Posted October 16, 2009 at 09:48 PM Boa pergunta. Não sei 😛 Experimenta usar o copy.deepcopy() Python sucks =X Não respondo a dúvidas por mensagem. Link to comment Share on other sites More sharing options...
Betovsky Posted October 16, 2009 at 09:50 PM Report Share #291971 Posted October 16, 2009 at 09:50 PM Python sucks =X Yep!Mas o deepcopy funcionou ou não? Penso que serve para qualquer tipo de objectos, inclusive listas. "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk Link to comment Share on other sites More sharing options...
djthyrax Posted October 16, 2009 at 09:58 PM Report Share #291975 Posted October 16, 2009 at 09:58 PM http://docs.python.org/library/copy.html Se o [:] não der, tem de dar com o deepcopy. Não peças ajuda por PM! A tua dúvida vai ter menos atenção do que se for postada na secção correcta do fórum! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now