cartesian-product

Algorithm to produce Cartesian product of arrays in depth-first order

送分小仙女□ 提交于 2020-01-02 18:56:19
问题 I'm looking for an example of how, in Ruby, a C like language, or pseudo code, to create the Cartesian product of a variable number of arrays of integers, each of differing length, and step through the results in a particular order: So given, [1,2,3],[1,2,3],[1,2,3]: [1, 1, 1] [2, 1, 1] [1, 2, 1] [1, 1, 2] [2, 2, 1] [1, 2, 2] [2, 1, 2] [2, 2, 2] [3, 1, 1] [1, 3, 1] etc. Instead of the typical result I've seen (including the example I give below): [1, 1, 1] [2, 1, 1] [3, 1, 1] [1, 2, 1] [2, 2,

Algorithm to produce Cartesian product of arrays in depth-first order

北慕城南 提交于 2020-01-02 18:56:01
问题 I'm looking for an example of how, in Ruby, a C like language, or pseudo code, to create the Cartesian product of a variable number of arrays of integers, each of differing length, and step through the results in a particular order: So given, [1,2,3],[1,2,3],[1,2,3]: [1, 1, 1] [2, 1, 1] [1, 2, 1] [1, 1, 2] [2, 2, 1] [1, 2, 2] [2, 1, 2] [2, 2, 2] [3, 1, 1] [1, 3, 1] etc. Instead of the typical result I've seen (including the example I give below): [1, 1, 1] [2, 1, 1] [3, 1, 1] [1, 2, 1] [2, 2,

itertools: Cartesian product of permutations

ⅰ亾dé卋堺 提交于 2020-01-01 15:36:09
问题 Using pythons itertools , I'd like to create an iterator over the outer product of all permutations of a bunch of lists. An explicit example: import itertools A = [1,2,3] B = [4,5] C = [6,7] for x in itertools.product(itertools.permutations(A),itertools.permutations(B),itertools.permutations(C)): print x While this works, I'd like to generalize it to an arbitrary list of lists. I tried: for x in itertools.product(map(itertools.permutations,[A,B,C])): print x but it did not do what I intended.

itertools: Cartesian product of permutations

限于喜欢 提交于 2020-01-01 15:36:04
问题 Using pythons itertools , I'd like to create an iterator over the outer product of all permutations of a bunch of lists. An explicit example: import itertools A = [1,2,3] B = [4,5] C = [6,7] for x in itertools.product(itertools.permutations(A),itertools.permutations(B),itertools.permutations(C)): print x While this works, I'd like to generalize it to an arbitrary list of lists. I tried: for x in itertools.product(map(itertools.permutations,[A,B,C])): print x but it did not do what I intended.

Cartesian products of tables that contains same columns

随声附和 提交于 2019-12-31 03:59:06
问题 I am really confused about this. I searched a lot of tutorials but i couldnot find a clear answer. A B B D 1 X x 5 2 x y 6 x 4 I want to cross this two tables.A , B, B,d are attributes. A B B D 1 X x 5 2 x x 5 1 X y 6 2 x y 6 1 X x 4 2 x x 4 This should be answer normally according to rule of cartesian. Cross all rows. But i am confused about same column B. Same column will seem twice? 回答1: Some relational query language/algebra relations have ordered column names . There is a way to

LINQ implementation of Cartesian Product with pruning

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 03:03:12
问题 I hope someone is able to help me with what is, at least to me, quite a tricky algorithm. The Problem I have a List ( 1 <= size <= 5 , but size unknown until run-time) of Lists ( 1 <= size <= 2 ) that I need to combine. Here is an example of what I am looking at:- ListOfLists = { {1}, {2,3}, {2,3}, {4}, {2,3} } So, there are 2 stages to what I need to do:- (1). I need to combine the inner lists in such a way that any combination has exactly ONE item from each list, that is, the possible

Scala: cross (cartesian) product with multiple sources and heterogeneous types

故事扮演 提交于 2019-12-30 23:50:11
问题 I'm trying to construct multiple cross products of traversables of different (but each homogeneous) types. The desired return type is a traversable of a tuple with the type matching the types in the input traversables. For example: List(1, 2, 3) cross Seq("a", "b") cross Set(0.5, 7.3) This should give a Traversable[(Int, String, Double)] with all possible combinations from the three sources. The case of combining only two sources was nicely answered here. The given idea is: implicit class

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

久未见 提交于 2019-12-28 06:46:04
问题 I have an unknown number of buckets(collections), and each bucket having an unknown number of entities I need to produce a cartesian product of all the entities, so that I endup with a single COLLECTION that has ARRAYS of entities and in each array, there is 1 representetive from EVERY bucket. So that if I have 5 buckets (B1..B5), and buckets B1, B2 have 1 item each, and bucket B3, B4 and B5 have 4, 8 and 10 items each, I'll have a collection of 320 arrays, and each array will have 5 items.

copying iterators and producing unordered self-cartesian product

走远了吗. 提交于 2019-12-24 12:27:39
问题 Say I have a list, and I want to produce a list of all unique pairs of elements without considering the order. One way to do this is: mylist = ['W','X','Y','Z'] for i in xrange(len(mylist)): for j in xrange(i+1,len(mylist)): print mylist[i],mylist[j] W X W Y W Z X Y X Z Y Z I want to do this with iterators, I thought of the following, even though it doesn't have brevity: import copy it1 = iter(mylist) for a in it1: it2 = copy.copy(it1) for b in it2: print a,b But this doesn't even work. What

Relational Algebra: Natural Join having the same result as Cartesian product

社会主义新天地 提交于 2019-12-24 09:57:46
问题 I am trying to understand what will be the result of performing a natural join between two relations R and S, where they have no common attributes. By following the below definition, I thought the answer might be an empty set: Natural Join definition. My line of thought was because the condition in the 'Select' symbol is not met, the projection of all of the attributes won't take place. When I asked my lecturer about this, he said that the output will be the same as doing a cartezian product