Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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
Python 计算一个(或多个)值未知/通配符时列表的排列数_Python_Itertools - Fatal编程技术网

Python 计算一个(或多个)值未知/通配符时列表的排列数

Python 计算一个(或多个)值未知/通配符时列表的排列数,python,itertools,Python,Itertools,计算一个(或多个)值未知/通配符时列表的排列数 如果我有一系列的选择,比如说3。每个选项可以是1或0 我可以使用itertools获得可能的组合 但是,如果已知其中两个选项,例如[0,1,“?”]-如何计算可能的剩余排列,例如(0,1,0)或(0,1,1) 我有三个以上的选择,所以需要一个可以扩展的方法(并允许更多的未知数) 导入itertools 从pprint导入pprint 输入_数据=[] 对于范围(0,3)内的i: 输入_data.append([0,1]) 打印(输入数据) #得到所

计算一个(或多个)值未知/通配符时列表的排列数

如果我有一系列的选择,比如说3。每个选项可以是1或0

我可以使用
itertools
获得可能的组合

但是,如果已知其中两个选项,例如
[0,1,“?”]
-如何计算可能的剩余排列,例如
(0,1,0)或(0,1,1)

我有三个以上的选择,所以需要一个可以扩展的方法(并允许更多的未知数)

导入itertools
从pprint导入pprint
输入_数据=[]
对于范围(0,3)内的i:
输入_data.append([0,1])
打印(输入数据)
#得到所有可能的组合
结果=列表(itertools.product(*输入数据))
打印(“可能的排列:,len(结果))
pprint(结果)
输出:

[[0, 1], [0, 1], [0, 1]]
possible permutations: 8
[(0, 0, 0),
 (0, 0, 1),
 (0, 1, 0),
 (0, 1, 1),
 (1, 0, 0),
 (1, 0, 1),
 (1, 1, 0),
 (1, 1, 1)]

编辑: 尝试使用SimonN建议的方法- 正在尝试用列表选项替换

如果我有
[0,1,“?”,“?”]
,我如何使用
[0,1]
替换
“?”
的第一个实例为
0
,下一个实例为
1

def gen_options(current):
    unks = current.count("?")
    input_data = []
    for i in range(0, unks):
        input_data.append([0, 1])

    permutatons = list(itertools.product(*input_data))
    options = []
    for perm in permutatons:

        option = [perm if x == "?" else x for x in current]
        options.append(option)
    return options

test = [0, 1, "?", "?"]
options = gen_options(test)
print (options)
给予


如果你知道一些选项,那么你只需要计算出未知选项的排列。因此,如果您有
[0,“?”,1,1,0,“?”,“?”]
您只需要生成三个值的所有可能排列,并插入它们来代替?人物。因此,在本例中,将有八个选项,其中三个?替换为您在问题中提供的排列

编辑:这里有一些代码
我认为,
itertools.product
可以满足您的所有需求

来自itertools导入产品的
>>
>>>x=[0,“?”,1,1,0,“?”,“?”]
>>>a=[(0,1)如果e='?'否则(e,)表示x中的e]
>>>印刷品(a)
[(0,), (0, 1), (1,), (1,), (0,), (0, 1), (0, 1)]
>>>对于产品(*a)中的v:
...    印刷品(五)
...
(0, 0, 1, 1, 0, 0, 0)
(0, 0, 1, 1, 0, 0, 1)
(0, 0, 1, 1, 0, 1, 0)
(0, 0, 1, 1, 0, 1, 1)
(0, 1, 1, 1, 0, 0, 0)
(0, 1, 1, 1, 0, 0, 1)
(0, 1, 1, 1, 0, 1, 0)
(0, 1, 1, 1, 0, 1, 1)

nb的排列是2^(数组的len),这里2^3=8,那么,你的问题是什么?你的方法和第一个一样:乘以每个位置的自由度。在给定的情况下,您有2x2x2=8和1x1x2=2。对不起,我不清楚。我正在尝试生成选项,而不是仅仅计算它们好的,这是有意义的。因此,如果我想用列表中的选项替换
“?
”,那么最好的方法是什么?试图想出一种方法,但这一行是错误的:
选项=[perm if x==”?“else x for x in current]
如何传递一个类似
[0,1]
的列表,并将列表中的第一个
替换为
0
,第二个替换为
1
?添加了一个解决方案。我对它不是很满意,因为它的循环比我觉得它需要的要多,但它很有效。
[[0, 1, (0, 0), (0, 0)], [0, 1, (0, 1), (0, 1)], [0, 1, (1, 0), (1, 0)], [0, 1, (1, 1), (1, 1)]]
import itertools
from copy import copy

inp=[1,"?",1,"?",0]
output=[]

for p in itertools.product([0,1], repeat=inp.count("?")):
    working = copy(inp)
    p_list=list(p)
    for i, x in enumerate(working):
        if x == "?":
            working[i] = p_list.pop()
    output.append(working)
print(output)