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. docker绑定了nginx端口 外部访问不到2. docker-compose 为何找不到配置文件?3. node.js - vue-cli构建报错。。。生成不了模板,求解~!!4. docker start -a dockername 老是卡住,什么情况?5. dockerfile - 为什么docker容器启动不了?6. boot2docker无法启动7. docker不显示端口映射呢?8. dockerfile - 我用docker build的时候出现下边问题 麻烦帮我看一下9. 在mac下出现了两个docker环境10. docker内创建jenkins访问另一个容器下的服务器问题