问题描述
next_permutation是一个C ++函数,按字典顺序给出字符串的下一个排列.可以从这篇真棒文章中获得有关其实现的详细信息. http://wordaligned.org/articles/next-permutation
next_permutation is a C++ function which gives the lexicographically next permutation of a string. Details about its implementation can be obtained from this really awesome post. http://wordaligned.org/articles/next-permutation
- 有人知道在Python中有类似的实现吗?
- 是否有STL迭代器的直接python等效项?
推荐答案
-
itertools.permutations 关闭;最大的区别是它将所有项目都视为唯一项,而不是将它们进行比较.它也不会就地修改序列.在Python中实现std :: next_permutation对您来说可能是一个好习惯(使用列表索引而不是随机访问迭代器).
itertools.permutations is close; the biggest difference is it treats all items as unique rather than comparing them. It also doesn't modify the sequence in-place. Implementing std::next_permutation in Python could be a good exercise for you (use indexing on a list rather than random access iterators).
否. Python迭代器可与输入迭代器相媲美,后者是STL类别,但仅是冰山一角.相反,您必须使用其他构造,例如输出迭代器的callable.这破坏了C ++迭代器的良好语法通用性.
No. Python iterators are comparable to input iterators, which are an STL category, but only the tip of that iceberg. You must instead use other constructs, such as a callable for an output iterator. This breaks the nice syntax generality of C++ iterators.
这篇关于STL中next_permutation的Python实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!