cartesian-product

Cartesian product of javascript object with different types of values like string, object and array

ε祈祈猫儿з 提交于 2020-06-17 05:37:48
问题 I am working on an assignment. I have the following object form. { name: "name here", skills: [ "cash", "shares" ], subjects: [ { subName: "subject1", remark: ['remark1', 'remark2'] }, { subName: "subject2", remark: ['remark1', 'Hockey'] } ] } I want to generate a Cartesian product of the properties so that the output is an array of the following form: [ { "name": "name here", "skills": "cash", "subjects": { "subName": "subject1", “remark”: “remark2” }}, { "name": "name here", "skills": "cash

Concatenating Strings: “Multiplication” of two list of strings

我怕爱的太早我们不能终老 提交于 2020-06-12 15:29:39
问题 For list of strings, define the multiplication operation in as concatenating here: l1 = ['aa', 'bb', 'cc'] l2 = ['11', '22'] l3 = l1 op l2 Expected output: l3 = ['aa11', 'aa22', 'bb11', 'bb22', 'cc11', 'cc22'] Simply we can use for l in l1: for ll in l2: l3.append(l+ll) But I'd be grateful to hear a pythonic solution. 回答1: from itertools import product l1 = ['aa', 'bb', 'cc'] l2 = ['11', '22'] l3 = [x+y for (x,y) in product(l1,l2)] print(l3) But it's effectively the same thing as what you're

Concatenating Strings: “Multiplication” of two list of strings

若如初见. 提交于 2020-06-12 15:27:25
问题 For list of strings, define the multiplication operation in as concatenating here: l1 = ['aa', 'bb', 'cc'] l2 = ['11', '22'] l3 = l1 op l2 Expected output: l3 = ['aa11', 'aa22', 'bb11', 'bb22', 'cc11', 'cc22'] Simply we can use for l in l1: for ll in l2: l3.append(l+ll) But I'd be grateful to hear a pythonic solution. 回答1: from itertools import product l1 = ['aa', 'bb', 'cc'] l2 = ['11', '22'] l3 = [x+y for (x,y) in product(l1,l2)] print(l3) But it's effectively the same thing as what you're

Concatenate strings in 2 different lists [duplicate]

余生颓废 提交于 2020-05-22 07:58:43
问题 This question already has answers here : Iterate over all combinations of values in multiple lists in Python (2 answers) Closed 4 years ago . I need to concatenate 2 different lists of strings in python. for example: list1 = ['A','B','C'] list2 = ['D', 'E'] I want to obtain list3 = ['AD', 'AE', 'BD', 'BE', 'CD', 'CE'] I've tried: list3 = zip(list1,list2) And it returns list3 = [('A','D'), ('B','E')] I've also tried: list(itertools.product(list1, list2)) But it returns [('A','D'),('A','E'),...

Multiplying every element of one array by every element of another array

送分小仙女□ 提交于 2020-03-20 06:35:48
问题 Say I have two arrays, import numpy as np x = np.array([1, 2, 3, 4]) y = np.array([5, 6, 7, 8]) What's the fastest, most Pythonic, etc., etc. way to get a new array, z , with a number of elements equal to x.size * y.size , in which the elements are the products of every pair of elements (x_i, y_j) from the two input arrays. To rephrase, I'm looking for an array z in which z[k] is x[i] * y[j] . A simple but inefficient way to get this is as follows: z = np.empty(x.size * y.size) counter = 0

Multiplying every element of one array by every element of another array

眉间皱痕 提交于 2020-03-20 06:35:05
问题 Say I have two arrays, import numpy as np x = np.array([1, 2, 3, 4]) y = np.array([5, 6, 7, 8]) What's the fastest, most Pythonic, etc., etc. way to get a new array, z , with a number of elements equal to x.size * y.size , in which the elements are the products of every pair of elements (x_i, y_j) from the two input arrays. To rephrase, I'm looking for an array z in which z[k] is x[i] * y[j] . A simple but inefficient way to get this is as follows: z = np.empty(x.size * y.size) counter = 0

Makefile permutation

这一生的挚爱 提交于 2020-01-30 08:45:11
问题 Bash can produce permutations (cartesian product): $ echo {1,2}{a,b} 1a 1b 2a 2b I would like to do something similar with a makefile. Here is an example makefile: all: 1a 1b 2a 2b I would like something like this if possible: NOV = 1 2 OSC = a b all: $(NOV)$(OSC) However when I use an example like that it just creates "1 2a b" instead of combining them. Is this possible? 回答1: No need for any complex loops or borrowing from shell. It is much simpler than that. $(foreach p, $(NOV), $(addprefix

Makefile permutation

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-30 08:45:05
问题 Bash can produce permutations (cartesian product): $ echo {1,2}{a,b} 1a 1b 2a 2b I would like to do something similar with a makefile. Here is an example makefile: all: 1a 1b 2a 2b I would like something like this if possible: NOV = 1 2 OSC = a b all: $(NOV)$(OSC) However when I use an example like that it just creates "1 2a b" instead of combining them. Is this possible? 回答1: No need for any complex loops or borrowing from shell. It is much simpler than that. $(foreach p, $(NOV), $(addprefix

Generating a n-ary Cartesian product example

旧街凉风 提交于 2020-01-20 06:58:27
问题 I found that Eric Lippert's post here suits a particular problem I have. The problem is I can't wrap my head around how I should be using it with a 2+ amount of collections. Having var collections = new List<List<MyType>>(); foreach(var item in somequery) { collections.Add( new List<MyType> { new MyType { Id = 1} .. n } ); } How do I apply the cartesian product linq query on the collections variabile ? The extension method is this one: static IEnumerable<IEnumerable<T>> CartesianProduct<T>

Generating a n-ary Cartesian product example

房东的猫 提交于 2020-01-20 06:57:22
问题 I found that Eric Lippert's post here suits a particular problem I have. The problem is I can't wrap my head around how I should be using it with a 2+ amount of collections. Having var collections = new List<List<MyType>>(); foreach(var item in somequery) { collections.Add( new List<MyType> { new MyType { Id = 1} .. n } ); } How do I apply the cartesian product linq query on the collections variabile ? The extension method is this one: static IEnumerable<IEnumerable<T>> CartesianProduct<T>