简体   繁体   中英

Python: How do I get a list of permutations of subsets?

Let's say I have the following list:

L = [1, 2, 3]

I want to get the following output:

[[1], [2],[3]]
[[1], [2, 3]]
[[2], [1, 3]]
[[3], [1, 2]]

Except I'm not even sure if I phrased the question correctly. It's basically like non-repeating groups of permutations?

It's not particularly clear to me how you came to your output (another example or a better description may help), but the answer probably lies in itertools , which contains a number of tools to work with combinations and permutations:

>>> data = [1, 2, 3]
>>> list(zip(reversed(data), itertools.combinations(data, 2)))
[(3, (1, 2)), (2, (1, 3)), (1, (2, 3))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM