python - 为什么在main()函数里result 会变成那样?
问题描述
#Any Queens puzzledef share_diagonal(x0, y0, x1, y1): ''' Is (x0, y0) on a shared diagonal with (x1, y1)? ''' dy = abs(y1 - y0) dx = abs(x1 - x0) return dx == dydef col_clashes(bs, c): '''Return True if the queen at column c clasheswith any queen to its left. ''' for i in range(c):if share_diagonal(i, bs[i], c, bs[c]): return True return Falsedef has_clashes(the_board): '''Determine whether we have any queens clashing on the diagonals.We’re assuming here that the_board is a permutation of columnnumbers, so we’re not explicitly checking row or column clashes.If it has clashes, return True. ''' for col in range(1, len(the_board)):if col_clashes(the_board, col): return True return Falsedef interchange_list(j, k, list): temp = list[j] list[j] = list[k] list[k] = tempdef generating_next_permutation_in_lexicographic_order(per_list): n = len(per_list) - 1 j = n - 1 while per_list[j] > per_list[j + 1]:j = j - 1if j < 0: return 0 k = n while per_list[j] > per_list[k]:k = k - 1 interchange_list(j, k, per_list) r = n s = j + 1 while r > s:interchange_list(r, s, per_list)r = r - 1s = s + 1 return per_listdef main(num): per_list = list(range(0, num)) tries = 0 num_found = 0 result = [] while per_list != 0:tries += 1if not has_clashes(per_list): #print('Found solution {0} in {1} tries.'.format(per_list, tries)) list1 = per_list result.append(list1) #print(result) num_found += 1per_list = generating_next_permutation_in_lexicographic_order(per_list) print(num_found) print(result) main(8)
打印结果为92[[7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 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], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0], [7, 6, 5, 4, 3, 2, 1, 0]][Finished in 0.2s]为啥result都变成一样的了?
问题解答
回答1:问题出在generating_next_permutation_in_lexicographic_order这个函数。Python里List是可变类型,所以你全局事实上只操作了一个List,然后不断把同一个List的引用放入result里面当然会是这样。一种简单的修改:
generating_next_permutation_in_lexicographic_order(per_list): import copy per_list = copy.deepcopy(per_list) #剩下是你原来的代码
相关文章:
1. 关于《Effective Java中文版(第2版)》的问题2. angular.js - angularjs怎么实现跳转的,是要写路由吗3. css - 移动端 oppo 手机之 Border-radius4. angular.js - angular.ui:dropdown的下拉菜单关闭的偶尔失常问题5. angular.js - angularjs ng-repeat 不能刷新6. javascript - html5显示隐藏7. Angular.js 无法设置Authorization头,该怎么解决?8. 前端 - 封装d3的data.format功能遇到问题9. docker 下面创建的IMAGE 他们的 ID 一样?这个是怎么回事????10. PHP单例模式