Files
sspai-100-hours-series-python/code/newsletter/N3/itertools.ipynb

305 lines
6.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# chain"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['The', 'Zen', 'of', 'Python,', 'by', 'Tim', 'Peters'],\n",
" ['Beautiful', 'is', 'better', 'than', 'ugly.'],\n",
" ['Explicit', 'is', 'better', 'than', 'implicit.'],\n",
" ['Simple', 'is', 'better', 'than', 'complex.'],\n",
" ['Complex', 'is', 'better', 'than', 'complicated.'],\n",
" ['Flat', 'is', 'better', 'than', 'nested.'],\n",
" ['Sparse', 'is', 'better', 'than', 'dense.'],\n",
" ['Readability', 'counts.'],\n",
" ['Special',\n",
" 'cases',\n",
" \"aren't\",\n",
" 'special',\n",
" 'enough',\n",
" 'to',\n",
" 'break',\n",
" 'the',\n",
" 'rules.']]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import chain\n",
"poem = \"\"\"\n",
"The Zen of Python, by Tim Peters\n",
"Beautiful is better than ugly.\n",
"Explicit is better than implicit.\n",
"Simple is better than complex.\n",
"Complex is better than complicated.\n",
"Flat is better than nested.\n",
"Sparse is better than dense.\n",
"Readability counts.\n",
"Special cases aren't special enough to break the rules.\n",
"Although practicality beats purity.\n",
"Errors should never pass silently.\n",
"Unless explicitly silenced.\n",
"In the face of ambiguity, refuse the temptation to guess.\n",
"There should be one-- and preferably only one --obvious way to do it.\n",
"Although that way may not be obvious at first unless you're Dutch.\n",
"Now is better than never.\n",
"Although never is often better than *right* now.\n",
"If the implementation is hard to explain, it's a bad idea.\n",
"If the implementation is easy to explain, it may be a good idea.\n",
"Namespaces are one honking great idea -- let's do more of those!\n",
"\"\"\"\n",
"\n",
"split = [line.split() for line in poem.splitlines()]\n",
"split[1:10]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Zen', 'of', 'Python,', 'by', 'Tim', 'Peters', 'Beautiful', 'is', 'better']"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"words = list(chain(*[elem for elem in split if elem]))\n",
"words[1:10]"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Zen', 'of', 'Python,', 'by', 'Tim', 'Peters', 'Beautiful', 'is', 'better']"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"words = list(chain.from_iterable(split))\n",
"words[1:10]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# permutations、product"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1320"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import permutations\n",
"uppers = \"ABCD\"\n",
"lowers = uppers.lower()\n",
"numbers = \"1234\"\n",
"elements = [*uppers, *lowers, *numbers]\n",
"\n",
"pairs = list(permutations(elements, 3))\n",
"len(pairs)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('A', 'B', 'D'),\n",
" ('A', 'B', 'a'),\n",
" ('A', 'B', 'b'),\n",
" ('A', 'B', 'c'),\n",
" ('A', 'B', 'd'),\n",
" ('A', 'B', '1'),\n",
" ('A', 'B', '2'),\n",
" ('A', 'B', '3'),\n",
" ('A', 'B', '4')]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pairs[1:10]"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('A', 'b'),\n",
" ('A', 'c'),\n",
" ('A', 'd'),\n",
" ('B', 'a'),\n",
" ('B', 'b'),\n",
" ('B', 'c'),\n",
" ('B', 'd'),\n",
" ('C', 'a'),\n",
" ('C', 'b')]"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import product\n",
"\n",
"pairs = list(product(uppers, lowers))\n",
"pairs[1:10]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## repeat、cycle"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['ABCD', 'ABCD', 'ABCD']"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import repeat\n",
"uppers = \"ABCD\"\n",
"\n",
"more = list(repeat(uppers, times=3))\n",
"more"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['A', 'B', 'C', 'D', 'A', 'B']\n"
]
}
],
"source": [
"from itertools import cycle\n",
"\n",
"uppers = \"ABCD\"\n",
"more = cycle(uppers)\n",
"counter = 0\n",
"result = []\n",
"\n",
"for letter in more:\n",
" if counter <= 5:\n",
" result.append(letter)\n",
" counter += 1\n",
" continue\n",
" break\n",
"\n",
"print(result)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.0 ('pandas-startup')",
"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.9.0"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "13977d4cc82dee5f9d9535ceb495bd0ab12a43c33c664e5f0d53c24cf634b67f"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}