无法将值存储在全局变量中
cant store the value in global variable
我正在尝试使用回溯算法在 leetcode 中解决排列问题,在打印时我得到了所有的可能性,但是当我试图将这些值存储在全局变量中时,我不允许这样做
Ex:
AnswerIwant:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
when i print those value while recursion it is print
Example:
[1, 2, 3]
[1, 3, 2]
[2, 3, 1]
[2, 1, 3]
[3, 1, 2]
[3, 2, 1]
当我为 outPut 存储全局变量中的值时,我得到了这样的结果
[1, 2, 3]
[[1, 2, 3]]
[[1, 2]]
[1, 3, 2]
[[1, 3, 2], [1, 3, 2]]
[[1, 3], [1, 3]]
[[1], [1]]
[2, 3, 1]
[[2, 3, 1], [2, 3, 1], [2, 3, 1]]
[[2, 3], [2, 3], [2, 3]]
[2, 1, 3]
[[2, 1, 3], [2, 1, 3], [2, 1, 3], [2, 1, 3]]
[[2, 1], [2, 1], [2, 1], [2, 1]]
[[2], [2], [2], [2]]
[3, 1, 2]
[[3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2]]
[[3, 1], [3, 1], [3, 1], [3, 1], [3, 1]]
[3, 2, 1]
[[3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1]]
[[3, 2], [3, 2], [3, 2], [3, 2], [3, 2], [3, 2]]
[[3], [3], [3], [3], [3], [3]]
[[], [], [], [], [], []]
这是我上面输出的代码
nums = [1,2,3]
val=nums
answerIwant = []
numberOfValues=len(val)-1
answer=[]
def permutation(nums, bucket, index,sub):
global answerIwant,numberOfValues
for i in range(len(nums)):
val=nums.pop(0)
bucket.append(val)
if len(bucket)-1==numberOfValues:
print(bucket)
answerIwant.append(bucket)
permutation(nums, bucket, index,sub)
nev=bucket.pop()
nums.append(nev)
print(answerIwant)
#[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
sub=[]
bucket = []
val=(permutation(nums, bucket, index,sub))
print(val)
用符号词解释我遇到的问题
谢谢
您必须在添加到 answerIwant 列表时附加列表的副本。所以你应该将 answerIwant.append(bucket)
替换为 answerIwant.append(bucket.copy())
.
参考this了解更多信息。所以你的代码可能是这样的,
nums = [1,2,3]
val=nums
answerIwant = []
numberOfValues=len(val)-1
answer=[]
def permutation(nums, bucket, index,sub):
global numberOfValues
for i in range(len(nums)):
val=nums.pop(0)
bucket.append(val)
if len(bucket)-1==numberOfValues:
# print(bucket)
answerIwant.append(bucket.copy())
permutation(nums, bucket, index,sub)
nev = bucket
nev=bucket.pop()
nums.append(nev)
#[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
sub=[]
bucket = []
permutation(nums, bucket, 0,sub)
print(answerIwant)
我正在尝试使用回溯算法在 leetcode 中解决排列问题,在打印时我得到了所有的可能性,但是当我试图将这些值存储在全局变量中时,我不允许这样做
Ex:
AnswerIwant:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
when i print those value while recursion it is print
Example:
[1, 2, 3]
[1, 3, 2]
[2, 3, 1]
[2, 1, 3]
[3, 1, 2]
[3, 2, 1]
当我为 outPut 存储全局变量中的值时,我得到了这样的结果
[1, 2, 3]
[[1, 2, 3]]
[[1, 2]]
[1, 3, 2]
[[1, 3, 2], [1, 3, 2]]
[[1, 3], [1, 3]]
[[1], [1]]
[2, 3, 1]
[[2, 3, 1], [2, 3, 1], [2, 3, 1]]
[[2, 3], [2, 3], [2, 3]]
[2, 1, 3]
[[2, 1, 3], [2, 1, 3], [2, 1, 3], [2, 1, 3]]
[[2, 1], [2, 1], [2, 1], [2, 1]]
[[2], [2], [2], [2]]
[3, 1, 2]
[[3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2]]
[[3, 1], [3, 1], [3, 1], [3, 1], [3, 1]]
[3, 2, 1]
[[3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1]]
[[3, 2], [3, 2], [3, 2], [3, 2], [3, 2], [3, 2]]
[[3], [3], [3], [3], [3], [3]]
[[], [], [], [], [], []]
这是我上面输出的代码
nums = [1,2,3]
val=nums
answerIwant = []
numberOfValues=len(val)-1
answer=[]
def permutation(nums, bucket, index,sub):
global answerIwant,numberOfValues
for i in range(len(nums)):
val=nums.pop(0)
bucket.append(val)
if len(bucket)-1==numberOfValues:
print(bucket)
answerIwant.append(bucket)
permutation(nums, bucket, index,sub)
nev=bucket.pop()
nums.append(nev)
print(answerIwant)
#[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
sub=[]
bucket = []
val=(permutation(nums, bucket, index,sub))
print(val)
用符号词解释我遇到的问题 谢谢
您必须在添加到 answerIwant 列表时附加列表的副本。所以你应该将 answerIwant.append(bucket)
替换为 answerIwant.append(bucket.copy())
.
参考this了解更多信息。所以你的代码可能是这样的,
nums = [1,2,3]
val=nums
answerIwant = []
numberOfValues=len(val)-1
answer=[]
def permutation(nums, bucket, index,sub):
global numberOfValues
for i in range(len(nums)):
val=nums.pop(0)
bucket.append(val)
if len(bucket)-1==numberOfValues:
# print(bucket)
answerIwant.append(bucket.copy())
permutation(nums, bucket, index,sub)
nev = bucket
nev=bucket.pop()
nums.append(nev)
#[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
sub=[]
bucket = []
permutation(nums, bucket, 0,sub)
print(answerIwant)