How to apply itertools.product to elements of a list of lists? [duplicate]

>>> list(itertools.product(*arrays)) [(-1, -2, -3), (-1, -2, 3), (-1, 2, -3), (-1, 2, 3), (1, -2, -3), (1, -2, 3), (1, 2, -3), (1, 2, 3)] This will feed all the pairs as separate arguments to product, which will then give you the cartesian product of them. The reason your version isn’t working is that … Read more

Zipped Python generators with 2nd one being shorter: how to retrieve element that is silently consumed

Right out of the box, zip() is hardwired to dispose of the unmatched item. So, you need a way to remember values before they get consumed. The itertool called tee() was designed for this purpose. You can use it to create a “shadow” of the first input iterator. If the second iterator terminates, you can … Read more

What is the difference between chain and chain.from_iterable in itertools?

The first takes 0 or more arguments, each an iterable, the second one takes one argument which is expected to produce the iterables: from itertools import chain chain(list1, list2, list3) iterables = [list1, list2, list3] chain.from_iterable(iterables) but iterables can be any iterator that yields the iterables: def gen_iterables(): for i in range(10): yield range(i) itertools.chain.from_iterable(gen_iterables()) … 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

All permutations of a Windows license key

Disclaimer: Yes, I know that this is not Python code. It just popped into my mind and I simply had to write it down. The simplest way is the use of shell expansion: $ echo MPP6R-09RXG-2H{8,B}MT-{B,8}K{H,N}M9-V{6,G}C8R MPP6R-09RXG-2H8MT-BKHM9-V6C8R MPP6R-09RXG-2H8MT-BKHM9-VGC8R MPP6R-09RXG-2H8MT-BKNM9-V6C8R MPP6R-09RXG-2H8MT-BKNM9-VGC8R MPP6R-09RXG-2H8MT-8KHM9-V6C8R MPP6R-09RXG-2H8MT-8KHM9-VGC8R MPP6R-09RXG-2H8MT-8KNM9-V6C8R MPP6R-09RXG-2H8MT-8KNM9-VGC8R MPP6R-09RXG-2HBMT-BKHM9-V6C8R MPP6R-09RXG-2HBMT-BKHM9-VGC8R MPP6R-09RXG-2HBMT-BKNM9-V6C8R MPP6R-09RXG-2HBMT-BKNM9-VGC8R MPP6R-09RXG-2HBMT-8KHM9-V6C8R MPP6R-09RXG-2HBMT-8KHM9-VGC8R MPP6R-09RXG-2HBMT-8KNM9-V6C8R MPP6R-09RXG-2HBMT-8KNM9-VGC8R

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