python模块等同于C库吗?(示例) Python是C++背景下的新手,我通过编写一些程序来适应它,在阅读了这个之后,这个问题浮现在我的脑海中。
在本页其他用户的帮助下,我们最终使用了该程序中的排列和组合功能python模块等同于C库吗?(示例) Python是C++背景下的新手,我通过编写一些程序来适应它,在阅读了这个之后,这个问题浮现在我的脑海中。,python,module,itertools,Python,Module,Itertools,在本页其他用户的帮助下,我们最终使用了该程序中的排列和组合功能 from itertools import permutations, combinations A = (1,2,3,4) B = (4,3,2,1) n = len(A) print("Starter point") print(A) print("Final Point") print(B) def permutaciones(inicio, objetivo): perms = list(perm
from itertools import permutations, combinations
A = (1,2,3,4)
B = (4,3,2,1)
n = len(A)
print("Starter point")
print(A)
print("Final Point")
print(B)
def permutaciones(inicio, objetivo):
perms = list(permutations(inicio))
perms2 = list(combinations(A, n))
if tuple(objetivo) not in perms:
return []
Var = perms[: perms.index(tuple(objetivo)) + 1]
return Var
print permutaciones(A, B)
print("--------------")
据我所知,我从库(模块)itertools调用函数排列和组合
其中,bot功能描述为以下2个代码块,从组合中引用可描述为
def combinations(iterable, r):
pool = tuple(iterable)
n = len(pool)
for indices in permutations(range(n), r):
if sorted(indices) == list(indices):
yield tuple(pool[i] for i in indices)
置换()的代码也可以表示为product()的子序列,经过过滤以排除具有重复元素(输入池中相同位置的元素)的条目:
那么,我是否可以只取出这两个函数,并将它们应用到我的代码中,而无需从itertools导入它们
是的,您可以在自己的代码中编写相应的代码,但请注意,“product”是itertools版本,而不是数学模块中的任何内容 但也要注意,他们之所以说“等效”,是因为itertools模块是用C编写的。因此,在这个特定的实例中,如果使用等效的Python,您的性能会受到很大的影响 一些标准库和许多附加包是纯Python的,您也可以从中剪切和粘贴 要在您自己的代码中使用等效的示例代码,只需剪切并粘贴即可。例如,这里我获取了
product()
代码,并在解释器中执行它:
>>> def product(*args, **kwds):
... # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
... # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
... pools = map(tuple, args) * kwds.get('repeat', 1)
... result = [[]]
... for pool in pools:
... result = [x+[y] for x in result for y in pool]
... for prod in result:
... yield tuple(prod)
...
>>> list(product('abc', 'def'))
[('a', 'd'), ('a', 'e'), ('a', 'f'), ('b', 'd'), ('b', 'e'), ('b', 'f'), ('c', 'd'), ('c', 'e'), ('c', 'f')]
解释器(或IDE)非常适合简单地尝试这样的东西。是的,您可以在自己的代码中编写等价的东西,但请注意,“产品”是itertools版本,而不是数学模块中的任何东西 但也要注意,他们之所以说“等效”,是因为itertools模块是用C编写的。因此,在这个特定的实例中,如果使用等效的Python,您的性能会受到很大的影响 一些标准库和许多附加包是纯Python的,您也可以从中剪切和粘贴 要在您自己的代码中使用等效的示例代码,只需剪切并粘贴即可。例如,这里我获取了
product()
代码,并在解释器中执行它:
>>> def product(*args, **kwds):
... # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
... # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
... pools = map(tuple, args) * kwds.get('repeat', 1)
... result = [[]]
... for pool in pools:
... result = [x+[y] for x in result for y in pool]
... for prod in result:
... yield tuple(prod)
...
>>> list(product('abc', 'def'))
[('a', 'd'), ('a', 'e'), ('a', 'f'), ('b', 'd'), ('b', 'e'), ('b', 'f'), ('c', 'd'), ('c', 'e'), ('c', 'f')]
解释器(或IDE)非常适合简单地尝试这样的东西。是的,您可以在自己的代码中编写等价的东西,但请注意,“产品”是itertools版本,而不是数学模块中的任何东西 但也要注意,他们之所以说“等效”,是因为itertools模块是用C编写的。因此,在这个特定的实例中,如果使用等效的Python,您的性能会受到很大的影响 一些标准库和许多附加包是纯Python的,您也可以从中剪切和粘贴 要在您自己的代码中使用等效的示例代码,只需剪切并粘贴即可。例如,这里我获取了
product()
代码,并在解释器中执行它:
>>> def product(*args, **kwds):
... # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
... # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
... pools = map(tuple, args) * kwds.get('repeat', 1)
... result = [[]]
... for pool in pools:
... result = [x+[y] for x in result for y in pool]
... for prod in result:
... yield tuple(prod)
...
>>> list(product('abc', 'def'))
[('a', 'd'), ('a', 'e'), ('a', 'f'), ('b', 'd'), ('b', 'e'), ('b', 'f'), ('c', 'd'), ('c', 'e'), ('c', 'f')]
解释器(或IDE)非常适合简单地尝试这样的东西。是的,您可以在自己的代码中编写等价的东西,但请注意,“产品”是itertools版本,而不是数学模块中的任何东西 但也要注意,他们之所以说“等效”,是因为itertools模块是用C编写的。因此,在这个特定的实例中,如果使用等效的Python,您的性能会受到很大的影响 一些标准库和许多附加包是纯Python的,您也可以从中剪切和粘贴 要在您自己的代码中使用等效的示例代码,只需剪切并粘贴即可。例如,这里我获取了
product()
代码,并在解释器中执行它:
>>> def product(*args, **kwds):
... # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
... # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
... pools = map(tuple, args) * kwds.get('repeat', 1)
... result = [[]]
... for pool in pools:
... result = [x+[y] for x in result for y in pool]
... for prod in result:
... yield tuple(prod)
...
>>> list(product('abc', 'def'))
[('a', 'd'), ('a', 'e'), ('a', 'f'), ('b', 'd'), ('b', 'e'), ('b', 'f'), ('c', 'd'), ('c', 'e'), ('c', 'f')]
解释器(或IDE)非常适合简单地尝试这样的东西
os.path
,而C倾向于为其库提供更平坦的名称空间
有趣的旁注:所有Python程序都可以作为模块使用。这就是为什么您经常在底部看到if\uuuu name\uuuu=='\uuuu main\uuuu':
,指定在代码像主程序一样运行时以及何时运行代码。当代码被其他程序导入时,该部分将不会运行itertools
中的product
是一个组合概念(“笛卡尔积”),而不是一个数学概念<代码>来自itertools进口产品就足够了;不需要数学函数from itertools import product, combinations, permutations
在代码中(主模块或使用这些函数的任何模块)。或者,如果您希望将这些函数的定义复制并粘贴到代码中,您可以这样做,并且只导入它们所依赖的产品
函数。这种剪切粘贴没有什么好处,