Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试查找2状态向量的所有组合(在python中)_Python_List_Combinations - Fatal编程技术网

尝试查找2状态向量的所有组合(在python中)

尝试查找2状态向量的所有组合(在python中),python,list,combinations,Python,List,Combinations,我试图找到只包含1和-1的四元素向量的所有组合。 Ex(1,1,1,1),(-1,1,1),…(-1,-1,-1,-1)等 我想这样做是非常低效的,但我想不出其他的方法,所以这里是我想做的。 我找到了总共有多少向量,然后创建了那么多空列表。我从vectorA开始,然后将其与vector中的每个列表进行比较。如果A匹配任何列表,则我随机更改A元素的符号,然后再次对照向量检查新列表A。如果A未找到匹配项,则它将替换向量中的一个列表,而while循环将增加1。应继续进行此操作,直到找到并打印出所有可能

我试图找到只包含1和-1的四元素向量的所有组合。 Ex(1,1,1,1),(-1,1,1),…(-1,-1,-1,-1)等 我想这样做是非常低效的,但我想不出其他的方法,所以这里是我想做的。 我找到了总共有多少向量,然后创建了那么多空列表。我从vector
A
开始,然后将其与
vector
中的每个列表进行比较。如果
A
匹配任何列表,则我随机更改
A
元素的符号,然后再次对照
向量检查新列表
A
。如果
A
未找到匹配项,则它将替换
向量中的一个列表,而while循环将增加1。应继续进行此操作,直到找到并打印出所有可能的组合。
然而,我的代码只是吐出了
向量的第一个变化,然后继续循环,而没有向
向量添加任何新的
A
。任何人都能发现我的代码中有什么地方没有达到我的预期目的和/或为我指明正确的方向吗?谢谢

import random
numvec = 2**4 # number of macrostates for a 4 component 2-state system

vectors = [[0,0,0,0] for i in range(numvec)] #initializing the numvec vectors 
A = [1,1,1,1]
i = 0
while i < 16:
    if any(x == A for x in vectors):
        y = random.randrange(0, 3)
        A[y] = A[y] * -1
    else:
        vectors[i] = A
        print vectors[i] 
        i += 1

print vectors
随机导入
numvec=2**4——4组件2状态系统的宏状态数
向量=[[0,0,0,0]表示范围内的i(numvec)]#初始化numvec向量
A=[1,1,1,1]
i=0
而我<16:
如果有(x==A表示向量中的x):
y=random.randrange(0,3)
A[y]=A[y]*-1
其他:
向量[i]=A
打印向量[i]
i+=1
打印向量

哦,我再一次意识到这种方法效率极低,但由于这是一项家庭作业,我更关心的是能否让python完成我希望它完成的任务,然后使用一组内置函数为我完成这项工作。再次感谢。

我认为通过内置功能,您可以轻松做到这一点:

list(itertools.product([-1,1], repeat=4))
或者,你可以用一个列表把它全部写出来。这要详细得多,但它避免了使用
itertools
库:

list((i,j,k,l) for i in (-1,1) for j in (-1,1) for k in (-1,1) for l in (-1,1))

我认为通过内置功能,您可以轻松做到这一点:

list(itertools.product([-1,1], repeat=4))
或者,你可以用一个列表把它全部写出来。这要详细得多,但它避免了使用
itertools
库:

list((i,j,k,l) for i in (-1,1) for j in (-1,1) for k in (-1,1) for l in (-1,1))
这不会创建
a
的副本。更改
A
时,
vectors
的内容会更改,因为
vectors[0]
A
在第一次运行此行后是同一对象。要解决此问题,请每次创建一个新的
a
对象,或复制
a
并将副本插入列表

如果要制作副本,可以使用:

这不会创建
a
的副本。更改
A
时,
vectors
的内容会更改,因为
vectors[0]
A
在第一次运行此行后是同一对象。要解决此问题,请每次创建一个新的
a
对象,或复制
a
并将副本插入列表

如果要制作副本,可以使用:

出于教育目的(考虑到有类似问题的人可能正在寻找通用递归方法——一项经典任务)

通过示例说明其工作原理:

[1,-1], length=4
call get_possibilities([1,-1],4) (main)
call get_possibilities([1,-1],3) (from get_possibilities)
call get_possibilities([1,-1],2) (from get_possibilities)
call get_possibilities([1,-1],1) (from get_possibilities)
call get_possibilities([1,-1],0) (from get_possibilities)

return [[]] (from the [1,-1], 0 call)

merge any element (here 1,-1, as list) with any element of the prior return
return [[1],[-1]] (from the [1,-1], 1 call)

merge any element (here 1,-1, as list) with the prior return
return [[1, 1], [1, -1], [-1, 1], [-1, -1]] (from the [1,-1], 2 call)

merge any element (here 1,-1, as list) with the prior return
return [[1, 1, 1], [1, 1, -1], [1, -1, 1]...] (8 elements, 2**3) (from the [1,-1], 3 call)

merge any element (here 1,-1, as list) with the prior return
return [[1, 1, 1, 1], [1, 1, 1, -1], ...] (16 elements 2**4) (from the [1,-1], 4 call)
问候

PS:我猜itertools.product的工作原理类似;)

出于教育目的(考虑到有类似问题的人可能正在寻找通用递归方法-一项经典任务)

通过示例说明其工作原理:

[1,-1], length=4
call get_possibilities([1,-1],4) (main)
call get_possibilities([1,-1],3) (from get_possibilities)
call get_possibilities([1,-1],2) (from get_possibilities)
call get_possibilities([1,-1],1) (from get_possibilities)
call get_possibilities([1,-1],0) (from get_possibilities)

return [[]] (from the [1,-1], 0 call)

merge any element (here 1,-1, as list) with any element of the prior return
return [[1],[-1]] (from the [1,-1], 1 call)

merge any element (here 1,-1, as list) with the prior return
return [[1, 1], [1, -1], [-1, 1], [-1, -1]] (from the [1,-1], 2 call)

merge any element (here 1,-1, as list) with the prior return
return [[1, 1, 1], [1, 1, -1], [1, -1, 1]...] (8 elements, 2**3) (from the [1,-1], 3 call)

merge any element (here 1,-1, as list) with the prior return
return [[1, 1, 1, 1], [1, 1, 1, -1], ...] (16 elements 2**4) (from the [1,-1], 4 call)
问候


PS:我猜itertools.product的工作原理类似;)

是的,我有一种印象,我可以使用
itertools
,但我尽量不使用它,因为那太容易了。任务的重点是获得一些使用if statosok的经验谢谢,这很值得思考,我将大胆地看看
itertools
能为未来的项目做些什么。如果你没有注意到的话,第二个答案不会使用itertools。是的,我得到了这样的印象,我可以使用
itertools
,但我尽量不使用它,因为那太容易了。任务的重点是获得一些使用if StationSok的经验谢谢,这很值得思考,我将大胆地看看
itertools
可以为未来的项目做些什么。第二个答案不会使用itertools,以防你没有注意到。是的!就这样!这是唯一的问题,现在它起作用了。非常感谢你!就这样!这是唯一的问题,现在它起作用了。多谢各位