Skip to content

Commit c0c994b

Browse files
committed
added cartesian product
1 parent 7f1645a commit c0c994b

File tree

3 files changed

+299
-0
lines changed

3 files changed

+299
-0
lines changed
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:ade2566ab5c7ca9cb880a39b507a20f21513d98bcb1fdd1be99ec7fe882c3646"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Cartesian Product\n",
16+
"\n",
17+
"This snippit was written by [Chris R. Albon](http://www.chrisralbon.com/) and is part of his collection of [well-documented Python snippits](https://github.com/chrisalbon/code_py). All code is written in Python 3 in iPython notebook and offered under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/\"><img alt=\"Creative Commons License)."
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"collapsed": false,
23+
"input": [
24+
"# import pandas as pd\n",
25+
"import pandas as pd"
26+
],
27+
"language": "python",
28+
"metadata": {},
29+
"outputs": [],
30+
"prompt_number": 10
31+
},
32+
{
33+
"cell_type": "code",
34+
"collapsed": false,
35+
"input": [
36+
"# Create two lists\n",
37+
"i = [1,2,3,4,5]\n",
38+
"j = [1,2,3,4,5]"
39+
],
40+
"language": "python",
41+
"metadata": {},
42+
"outputs": [],
43+
"prompt_number": 11
44+
},
45+
{
46+
"cell_type": "code",
47+
"collapsed": false,
48+
"input": [
49+
"# List every single x in i with every single y (i.e. Cartesian product)\n",
50+
"[(x, y) for x in i for y in j]"
51+
],
52+
"language": "python",
53+
"metadata": {},
54+
"outputs": [
55+
{
56+
"metadata": {},
57+
"output_type": "pyout",
58+
"prompt_number": 14,
59+
"text": [
60+
"[(1, 1),\n",
61+
" (1, 2),\n",
62+
" (1, 3),\n",
63+
" (1, 4),\n",
64+
" (1, 5),\n",
65+
" (2, 1),\n",
66+
" (2, 2),\n",
67+
" (2, 3),\n",
68+
" (2, 4),\n",
69+
" (2, 5),\n",
70+
" (3, 1),\n",
71+
" (3, 2),\n",
72+
" (3, 3),\n",
73+
" (3, 4),\n",
74+
" (3, 5),\n",
75+
" (4, 1),\n",
76+
" (4, 2),\n",
77+
" (4, 3),\n",
78+
" (4, 4),\n",
79+
" (4, 5),\n",
80+
" (5, 1),\n",
81+
" (5, 2),\n",
82+
" (5, 3),\n",
83+
" (5, 4),\n",
84+
" (5, 5)]"
85+
]
86+
}
87+
],
88+
"prompt_number": 14
89+
},
90+
{
91+
"cell_type": "code",
92+
"collapsed": false,
93+
"input": [
94+
"# An alternative way to do the cartesian product\n",
95+
"\n",
96+
"# import itertools\n",
97+
"import itertools\n",
98+
"\n",
99+
"# for two sets, find the the cartisan product\n",
100+
"for i in itertools.product([1,2,3,4,5], [1,2,3,4,5]):\n",
101+
" # and print it\n",
102+
" print(i)"
103+
],
104+
"language": "python",
105+
"metadata": {},
106+
"outputs": [
107+
{
108+
"output_type": "stream",
109+
"stream": "stdout",
110+
"text": [
111+
"(1, 1)\n",
112+
"(1, 2)\n",
113+
"(1, 3)\n",
114+
"(1, 4)\n",
115+
"(1, 5)\n",
116+
"(2, 1)\n",
117+
"(2, 2)\n",
118+
"(2, 3)\n",
119+
"(2, 4)\n",
120+
"(2, 5)\n",
121+
"(3, 1)\n",
122+
"(3, 2)\n",
123+
"(3, 3)\n",
124+
"(3, 4)\n",
125+
"(3, 5)\n",
126+
"(4, 1)\n",
127+
"(4, 2)\n",
128+
"(4, 3)\n",
129+
"(4, 4)\n",
130+
"(4, 5)\n",
131+
"(5, 1)\n",
132+
"(5, 2)\n",
133+
"(5, 3)\n",
134+
"(5, 4)\n",
135+
"(5, 5)\n"
136+
]
137+
}
138+
],
139+
"prompt_number": 16
140+
}
141+
],
142+
"metadata": {}
143+
}
144+
]
145+
}

Diff for: Untitled0.ipynb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:24d12ab64e5e93c82e0acf22e043d94d7006b033acc4160a5a99e702b6873929"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": []
9+
}

Diff for: cartesian_product.ipynb

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:ade2566ab5c7ca9cb880a39b507a20f21513d98bcb1fdd1be99ec7fe882c3646"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Cartesian Product\n",
16+
"\n",
17+
"This snippit was written by [Chris R. Albon](http://www.chrisralbon.com/) and is part of his collection of [well-documented Python snippits](https://github.com/chrisalbon/code_py). All code is written in Python 3 in iPython notebook and offered under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/\"><img alt=\"Creative Commons License)."
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"collapsed": false,
23+
"input": [
24+
"# import pandas as pd\n",
25+
"import pandas as pd"
26+
],
27+
"language": "python",
28+
"metadata": {},
29+
"outputs": [],
30+
"prompt_number": 10
31+
},
32+
{
33+
"cell_type": "code",
34+
"collapsed": false,
35+
"input": [
36+
"# Create two lists\n",
37+
"i = [1,2,3,4,5]\n",
38+
"j = [1,2,3,4,5]"
39+
],
40+
"language": "python",
41+
"metadata": {},
42+
"outputs": [],
43+
"prompt_number": 11
44+
},
45+
{
46+
"cell_type": "code",
47+
"collapsed": false,
48+
"input": [
49+
"# List every single x in i with every single y (i.e. Cartesian product)\n",
50+
"[(x, y) for x in i for y in j]"
51+
],
52+
"language": "python",
53+
"metadata": {},
54+
"outputs": [
55+
{
56+
"metadata": {},
57+
"output_type": "pyout",
58+
"prompt_number": 14,
59+
"text": [
60+
"[(1, 1),\n",
61+
" (1, 2),\n",
62+
" (1, 3),\n",
63+
" (1, 4),\n",
64+
" (1, 5),\n",
65+
" (2, 1),\n",
66+
" (2, 2),\n",
67+
" (2, 3),\n",
68+
" (2, 4),\n",
69+
" (2, 5),\n",
70+
" (3, 1),\n",
71+
" (3, 2),\n",
72+
" (3, 3),\n",
73+
" (3, 4),\n",
74+
" (3, 5),\n",
75+
" (4, 1),\n",
76+
" (4, 2),\n",
77+
" (4, 3),\n",
78+
" (4, 4),\n",
79+
" (4, 5),\n",
80+
" (5, 1),\n",
81+
" (5, 2),\n",
82+
" (5, 3),\n",
83+
" (5, 4),\n",
84+
" (5, 5)]"
85+
]
86+
}
87+
],
88+
"prompt_number": 14
89+
},
90+
{
91+
"cell_type": "code",
92+
"collapsed": false,
93+
"input": [
94+
"# An alternative way to do the cartesian product\n",
95+
"\n",
96+
"# import itertools\n",
97+
"import itertools\n",
98+
"\n",
99+
"# for two sets, find the the cartisan product\n",
100+
"for i in itertools.product([1,2,3,4,5], [1,2,3,4,5]):\n",
101+
" # and print it\n",
102+
" print(i)"
103+
],
104+
"language": "python",
105+
"metadata": {},
106+
"outputs": [
107+
{
108+
"output_type": "stream",
109+
"stream": "stdout",
110+
"text": [
111+
"(1, 1)\n",
112+
"(1, 2)\n",
113+
"(1, 3)\n",
114+
"(1, 4)\n",
115+
"(1, 5)\n",
116+
"(2, 1)\n",
117+
"(2, 2)\n",
118+
"(2, 3)\n",
119+
"(2, 4)\n",
120+
"(2, 5)\n",
121+
"(3, 1)\n",
122+
"(3, 2)\n",
123+
"(3, 3)\n",
124+
"(3, 4)\n",
125+
"(3, 5)\n",
126+
"(4, 1)\n",
127+
"(4, 2)\n",
128+
"(4, 3)\n",
129+
"(4, 4)\n",
130+
"(4, 5)\n",
131+
"(5, 1)\n",
132+
"(5, 2)\n",
133+
"(5, 3)\n",
134+
"(5, 4)\n",
135+
"(5, 5)\n"
136+
]
137+
}
138+
],
139+
"prompt_number": 16
140+
}
141+
],
142+
"metadata": {}
143+
}
144+
]
145+
}

0 commit comments

Comments
 (0)