Skip to content
Advertisement

Tag: algorithm

List sorting/modify problem

Initially, I wasn’t sure whether this was the appropriate place for this question, but after reading through the FAQ I feel fairly confident the topic is acceptable…. Furthermore I wasn’t sure if this would be classified as a particular type of problem (eg. a knapsack problem), thus, the title is rather vague. I’m sorry for that. Anyways. As both practice

How do I generate all permutations of a list?

How do I generate all the permutations of a list? For example: Answer Use itertools.permutations from the standard library: Adapted from here is a demonstration of how itertools.permutations might be implemented: A couple of alternative approaches are listed in the documentation of itertools.permutations. Here’s one: And another, based on itertools.product:

Advertisement