Dynamically cross-join multiple different-size collections together in Linq (C#)

You could create an extension method like the following: public static class EnumerableExtensions { public static IEnumerable<TValue []> Permutations<TKey, TValue>(this IEnumerable<TKey> keys, Func<TKey, IEnumerable<TValue>> selector) { var keyArray = keys.ToArray(); if (keyArray.Length < 1) yield break; TValue [] values = new TValue[keyArray.Length]; foreach (var array in Permutations(keyArray, 0, selector, values)) yield return array; } static … 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