您的位置:首页技术文章
文章详情页

python - 为什么在main()函数里result 会变成那样?

【字号: 日期:2022-07-02 15:57:29浏览:50作者:猪猪

问题描述

#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, 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, 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], [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) #剩下是你原来的代码

标签: Python 编程
相关文章: