Generate all combinations from multiple lists

You need recursion: Let’s say all your lists are in lists, which is a list of lists. Let result be the list of your required permutations. You could implement it like this: void generatePermutations(List<List<Character>> lists, List<String> result, int depth, String current) { if (depth == lists.size()) { result.add(current); return; } for (int i = 0; … Read more

Iterate over all combinations of values in multiple lists in Python [duplicate]

itertools.product should do the trick. >>> import itertools >>> list(itertools.product([1, 5, 8], [0.5, 4])) [(1, 0.5), (1, 4), (5, 0.5), (5, 4), (8, 0.5), (8, 4)] Note that itertools.product returns an iterator, so you don’t need to convert it into a list if you are only going to iterate over it once. eg. for x … Read more

Is there a multi-dimensional version of arange/linspace in numpy?

You can use np.mgrid for this, it’s often more convenient than np.meshgrid because it creates the arrays in one step: import numpy as np X,Y = np.mgrid[-5:5.1:0.5, -5:5.1:0.5] For linspace-like functionality, replace the step (i.e. 0.5) with a complex number whose magnitude specifies the number of points you want in the series. Using this syntax, … Read more

Operation on every pair of element in a list

Check out product() in the itertools module. It does exactly what you describe. import itertools my_list = [1,2,3,4] for pair in itertools.product(my_list, repeat=2): foo(*pair) This is equivalent to: my_list = [1,2,3,4] for x in my_list: for y in my_list: foo(x, y) Edit: There are two very similar functions as well, permutations() and combinations(). To illustrate … Read more

cartesian product in pandas

In recent versions of Pandas (>= 1.2) this is built into merge so you can do: from pandas import DataFrame df1 = DataFrame({‘col1′:[1,2],’col2’:[3,4]}) df2 = DataFrame({‘col3′:[5,6]}) df1.merge(df2, how=’cross’) This is equivalent to the previous pandas < 1.2 answer but is easier to read. For pandas < 1.2: If you have a key that is repeated … Read more

Cartesian product of multiple arrays in JavaScript

2020 Update: 1-line (!) answer with vanilla JS Original 2017 Answer: 2-line answer with vanilla JS: (see updates below) All of the answers here are overly complicated, most of them take 20 lines of code or even more. This example uses just two lines of vanilla JavaScript, no lodash, underscore or other libraries: let f … Read more

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