Created
April 25, 2017 11:38
-
-
Save vlad-bezden/dcd4ba24782976d70531a3fda56c86d2 to your computer and use it in GitHub Desktop.
Example of Chaining and Permutation/Crossproduct using list comprehension in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"cars = [['Mustang', 'GT'],\n", | |
" ['Explorer', 'Expedition'], \n", | |
" ['F-150', 'Super Duty']]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# chaining example\n", | |
"chaining = [car.upper()\n", | |
" for category in cars\n", | |
" for car in category]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['MUSTANG', 'GT', 'EXPLORER', 'EXPEDITION', 'F-150', 'SUPER DUTY']\n" | |
] | |
} | |
], | |
"source": [ | |
"print(chaining)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# permutation/crossproduct\n", | |
"permutations = [(car, year)\n", | |
" for car in chaining\n", | |
" for year in range(2015, 2017)]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[('MUSTANG', 2015), ('MUSTANG', 2016), ('GT', 2015), ('GT', 2016), ('EXPLORER', 2015), ('EXPLORER', 2016), ('EXPEDITION', 2015), ('EXPEDITION', 2016), ('F-150', 2015), ('F-150', 2016), ('SUPER DUTY', 2015), ('SUPER DUTY', 2016)]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(permutations)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# ordering in permutation matters\n", | |
"permutations2 = [(car, year)\n", | |
" for year in range(2015, 2017)\n", | |
" for car in chaining]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[('MUSTANG', 2015), ('GT', 2015), ('EXPLORER', 2015), ('EXPEDITION', 2015), ('F-150', 2015), ('SUPER DUTY', 2015), ('MUSTANG', 2016), ('GT', 2016), ('EXPLORER', 2016), ('EXPEDITION', 2016), ('F-150', 2016), ('SUPER DUTY', 2016)]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(permutations2)" | |
] | |
} | |
], | |
"metadata": { | |
"celltoolbar": "Raw Cell Format", | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment