Merging two Bash arrays into key:value pairs via a Cartesian product

If you don’t care about having duplicates, or maintaining indexes, then you can concatenate the two arrays in one line with: NEW=(“${OLD1[@]}” “${OLD2[@]}”) Full example: Unix=(‘Debian’ ‘Red hat’ ‘Ubuntu’ ‘Suse’ ‘Fedora’ ‘UTS’ ‘OpenLinux’); Shell=(‘bash’ ‘csh’ ‘jsh’ ‘rsh’ ‘ksh’ ‘rc’ ‘tcsh’); UnixShell=(“${Unix[@]}” “${Shell[@]}”) echo ${UnixShell[@]} echo ${#UnixShell[@]} Credit: http://www.thegeekstuff.com/2010/06/bash-array-tutorial/

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

Generate a matrix containing all combinations of elements taken from n vectors

The ndgrid function almost gives the answer, but has one caveat: n output variables must be explicitly defined to call it. Since n is arbitrary, the best way is to use a comma-separated list (generated from a cell array with ncells) to serve as output. The resulting n matrices are then concatenated into the desired … Read more

Cartesian product of an arbitrary number of sets

Edit: Previous solutions for two sets removed. See edit history for details. Here is a way to do it recursively for an arbitrary number of sets: public static Set<Set<Object>> cartesianProduct(Set<?>… sets) { if (sets.length < 2) throw new IllegalArgumentException( “Can’t have a product of fewer than two sets (got ” + sets.length + “)”); return … Read more

tech