Why does Python’s itertools.permutations contain duplicates? (When the original list has duplicates)

I can’t speak for the designer of itertools.permutations (Raymond Hettinger), but it seems to me that there are a couple of points in favour of the design: First, if you used a next_permutation-style approach, then you’d be restricted to passing in objects that support a linear ordering. Whereas itertools.permutations provides permutations of any kind of … Read more

Generate all binary strings of length n with k bits set

This method will generate all integers with exactly N ‘1’ bits. From https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation Compute the lexicographically next bit permutation Suppose we have a pattern of N bits set to 1 in an integer and we want the next permutation of N 1 bits in a lexicographical sense. For example, if N is 3 and the … Read more

All possible permutations of a set of lists in Python

You don’t need to know n in advance to use itertools.product >>> import itertools >>> s=[ [ ‘a’, ‘b’, ‘c’], [‘d’], [‘e’, ‘f’] ] >>> list(itertools.product(*s)) [(‘a’, ‘d’, ‘e’), (‘a’, ‘d’, ‘f’), (‘b’, ‘d’, ‘e’), (‘b’, ‘d’, ‘f’), (‘c’, ‘d’, ‘e’), (‘c’, ‘d’, ‘f’)]

JavaScript – Generating combinations from n arrays with m elements [duplicate]

Here is a quite simple and short one using a recursive helper function: function cartesian(…args) { var r = [], max = args.length-1; function helper(arr, i) { for (var j=0, l=args[i].length; j<l; j++) { var a = arr.slice(0); // clone arr a.push(args[i][j]); if (i==max) r.push(a); else helper(a, i+1); } } helper([], 0); return r; } … Read more

permutations with unique values

class unique_element: def __init__(self,value,occurrences): self.value = value self.occurrences = occurrences def perm_unique(elements): eset=set(elements) listunique = [unique_element(i,elements.count(i)) for i in eset] u=len(elements) return perm_unique_helper(listunique,[0]*u,u-1) def perm_unique_helper(listunique,result_list,d): if d < 0: yield tuple(result_list) else: for i in listunique: if i.occurrences > 0: result_list[d]=i.value i.occurrences-=1 for g in perm_unique_helper(listunique,result_list,d-1): yield g i.occurrences+=1 a = list(perm_unique([1,1,2])) print(a) result: [(2, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)