Sure, it's possible to filter the output of itertools.permutations through a set to eliminate the duplicates, but it still wastes time generating those duplicates, and if there are several repeated elements in the base sequence there will be lots of duplicates. That's because internally it permutes the sequence indices and is oblivious to the sequence item values. Itertools.permutations is good, but it doesn't deal nicely with sequences that contain repeated elements. Thanks to for pointing out that this is not what we'd traditionally think of as a type cast, but more of a call to the set() constructor. If you find yourself troubled by duplicates, try fitting your data into a structure with no duplicates like a set: > perms = You'll have to join your permuted letters as strings though. Iterable is sorted, the permutation tuples will be produced in sorted Permutations are emitted in lexicographic sort order. Iterable and all possible full-length permutations are generated. If r is not specified or is None, then r defaults to the length of the Return successive r length permutations of elements in the iterable. The itertools module has a useful method called permutations().
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |