我需要在不使用itertools
的情况下创建一个函数,该函数将使用给定的任何一组元素创建元组的排列列表。在
示例:
perm({1,2,3}, 2)
应返回[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
我得到的是:
def permutacion(conjunto, k):
a, b = list(), list()
for i in conjunto:
if len(b) < k and i not in b:
b.append(i)
b = tuple(b)
a.append(b)
return a
我知道这不会有任何作用,它只会添加第一个组合,而不会添加其他任何内容。在
正如@John在评论中提到的,^{} 的代码是:
在您的示例中,不使用外部导入或递归调用:
^{pr2}$我对@Hooked的回答有异议。。。在
首先,对于py来说,我是一个完全的新手,但是我正在寻找类似上面代码的东西。我现在在Repl.it输入
我的第一个问题是争论
返回以下错误
^{pr2}$我是这样修好的
但现在得到了一个错误:
现在,我不知道去哪里调试代码。但是,我看到很多人指出itertools在文档中包含代码。我复制了它,它起作用了。代码如下:
相关问题 更多 >
编程相关推荐