Finding all possible permutations of a given string in python

The itertools module has a useful method called permutations(). The documentation says: itertools.permutations(iterable[, r]) Return successive r length permutations of elements in the iterable. If r is not specified or is None, then r defaults to the length of the iterable and all possible full-length permutations are generated. Permutations are emitted in lexicographic sort order. So, if … Read more

Shuffle DataFrame rows

The idiomatic way to do this with Pandas is to use the .sample method of your dataframe to sample all rows without replacement: The frac keyword argument specifies the fraction of rows to return in the random sample, so frac=1 means return all rows (in random order). Note: If you wish to shuffle your dataframe in-place and reset the index, you could … Read more

How to generate all permutations of a list?

There’s a function in the standard-library for this: itertools.permutations. If for some reason you want to implement it yourself or are just curious to know how it works, here’s one nice approach, taken from http://code.activestate.com/recipes/252178/: A couple of alternative approaches are listed in the documentation of itertools.permutations. Here’s one: And another, based on itertools.product: