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. 网页爬虫 - Python:爬虫的中文编码问题?2. javascript - vue生成一维码?求助!!!!!急3. javascript - H5页面怎么查看console信息?4. thinkjs - 使用mysql搭建cms应该如何设计表?或怎样开始?5. thread - Android 主线程更新UI问题6. javascript - 关于Js中 this的一道题7. css - 手机页面在安卓和苹果浏览器显示不同的小小问题8. javascript - 修改表单多选项时和后台同事配合的问题。9. javascript - jqery ajax问题10. ubuntu 远程管理KVM设置问题
