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 = ['A', 'B', 'C']
并从该列表中,如何获得所有 2 乘 2 组合?像这样:
A, B A, C B, A B, C C, A C, B
使用itertools.permutations:
>>> from itertools import permutations >>> a = ['A', 'B', 'C'] >>> list(permutations(a,2)) # see second argument which specifies permutation length [('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]