Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我目前正在关注如何获取列表元素的所有可能组合?. 推荐的解决方案实现了有序的解决方案,即如果您有 A、B,那么组合是 A、B、AB。
不过,我想包括任何可能的元素排序,即 A、B、BA、AB。有没有办法在 Python 中做到这一点?
谢谢你。
使用itertools.permutations:
import itertools xs = 'a', 'b' for n in range(1, len(xs)+1): for ys in itertools.permutations(xs, n): print(ys)
印刷
('a',) ('b',) ('a', 'b') ('b', 'a')