List 更改itertools的输出
输出是List 更改itertools的输出,list,python-3.x,List,Python 3.x,输出是 import itertools def x(a,b): x = [[i] for i in itertools.product(a, repeat=b)] return [x] print (x({0,1},3)) 我想要的是它看起来像这样,所有的()都被删除,开始和结束[]都被删除 [[[(0, 0, 0)], [(0, 0, 1)], [(0, 1, 0)], [(0, 1, 1)], [(1, 0, 0)], [(1, 0, 1)], [(1, 1, 0)
import itertools
def x(a,b):
x = [[i] for i in itertools.product(a, repeat=b)]
return [x]
print (x({0,1},3))
我想要的是它看起来像这样,所有的()都被删除,开始和结束[]都被删除
[[[(0, 0, 0)], [(0, 0, 1)], [(0, 1, 0)], [(0, 1, 1)], [(1, 0, 0)], [(1, 0, 1)], [(1, 1, 0)], [(1, 1, 1)]]]
为什么不简单
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]
或者,如果希望所有元素都是列表
return list(itertools.product(a, repeat=b))
另外,要小心从名为
x
的函数中写入名为x
的变量;这将给递归带来麻烦。因为我需要输出[[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,0],[1,0,1],[1,1,1,0]]您给出的代码与我想要的代码有相同的问题(0,0,0),第二段代码在python 2.7中工作得非常好,但不是在Python3中,您知道我必须做哪些更改才能在Python3中工作吗
return map(list, itertools.product(a, repeat=b))