Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created February 21, 2018 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aparrish/d6a81d539abafe781d1a25bd644f2aa3 to your computer and use it in GitHub Desktop.
Save aparrish/d6a81d539abafe781d1a25bd644f2aa3 to your computer and use it in GitHub Desktop.
rwet notes 2018-02-16
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# notes 2018-02-16"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 10, 15, 20, 25]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[5, 10, 15, 20, 25]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 10, 15, 20, 25]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[5, 2*5, 3*5, 4*5, 5*5]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[5]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type([1, 2, 3])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"my_numbers = [5, 10, 15, 20, 25]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"25"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_numbers[4]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_numbers[0]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-72aecccf47fe>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_numbers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m47\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"my_numbers[47]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[4, 8, 16, 20, 24][2]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(my_numbers)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(my_numbers[0])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len([1, 2, 3])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len([123])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len([\"hello\"])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len([])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 in my_numbers"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"13 in my_numbers"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = [\"hello\", \"there\", \"hi\"]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"hello\" in x"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"her\" in x"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"her\" in \"there\""
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"her\" in x[1]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['there']"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[item for item in x if \"her\" in item]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[-6, 4, 12, 19, 45]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted([4, 19, -6, 12, 45])"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['baboon', 'elephant', 'mammoth', 'yak', 'zebra']"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted([\"zebra\", \"mammoth\", \"yak\", \"baboon\", \"elephant\"])"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['mammoth', 'yak', 'baboon', 'zebra', 'elephant']"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted([\"zebra\", \"mammoth\", \"yak\", \"baboon\", \"elephant\"],\n",
" key=lambda x: x[1])"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 10, 15, 20, 25]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_numbers"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 15, 20]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_numbers[1:4]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 10]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_numbers[:2]"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[20, 25]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_numbers[-2:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"this kind of cell!"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 10, 15, 20, 25]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_numbers"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"25"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_numbers[-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"THE LIST COMPREHENSION."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"BEFORE LIST COMPREHENSIONS: LISTS of LISTS 😀"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"z = [[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(z)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(z[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"BACK TO LIST COMPREHENSIONS!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"schema for list comprehension:\n",
"\n",
" [expr for var in source if cond]\n"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 10, 15, 20, 25]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_numbers"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[25, 50, 75, 100, 125]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[item * 5 for item in my_numbers]"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[25, 50, 75, 100, 125]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[blah * 5 for blah in my_numbers]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2.5, 5.0, 7.5, 10.0, 12.5]"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x / 2 for x in my_numbers]"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[100, 100, 100, 100, 100]"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[100 for x in my_numbers]"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱',\n",
" '🐱']"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[\"🐱\" for i in range(100)]"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 10, 15, 20, 25]"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_numbers"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 10, 15, 20]"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x for x in my_numbers if x < 25]"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"11 % 2"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 20]"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[x for x in my_numbers if x % 2 == 0]"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[25, 225, 625]"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[beluga * beluga for beluga in my_numbers if beluga % 2 == 1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"SPLITTING STRINGS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \"this is a test\" -> [\"this\", \"is\", \"a\", \"test\"]"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['this', 'is', 'a', 'test']"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"this is a test\".split()"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['th', 's ', 's a test']"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"this is a test\".split(\"i\")"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"message = \"it was the best of times, it was the worst of times\""
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['it was ', ' best of times, it was ', ' worst of times']"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"message.split(\"the\")"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"stats = \"45,0,434,36,74,.486,0,0,36,74,.486,.486,32,65,.492,51,104,155,7,8,41,32,57,104\""
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'45,0,434,36,74,.486,0,0,36,74,.486,.486,32,65,.492,51,104,155,7,8,41,32,57,104'"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stats"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(stats)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum([1, 2, 3, 4, 5])"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'int' and 'str'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-74-a4bc76cd7de7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstats\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
]
}
],
"source": [
"sum(stats)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"stats_fixed = stats.split(\",\")"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'int' and 'str'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-76-ae9e38ad904f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstats_fixed\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
]
}
],
"source": [
"sum(stats_fixed)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" list of strings -> ???? -> list of numbers"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[45.0,\n",
" 0.0,\n",
" 434.0,\n",
" 36.0,\n",
" 74.0,\n",
" 0.486,\n",
" 0.0,\n",
" 0.0,\n",
" 36.0,\n",
" 74.0,\n",
" 0.486,\n",
" 0.486,\n",
" 32.0,\n",
" 65.0,\n",
" 0.492,\n",
" 51.0,\n",
" 104.0,\n",
" 155.0,\n",
" 7.0,\n",
" 8.0,\n",
" 41.0,\n",
" 32.0,\n",
" 57.0,\n",
" 104.0]"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[float(x) for x in stats_fixed]"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1356.9499999999998"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum([float(x) for x in stats_fixed])"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"123\".isdigit()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"JOINING STRINGS"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"elements = [\"hydrogen\", \"helium\", \"lithium\", \"beryllium\", \"boron\"]"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"glue = \", and \""
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hydrogen, and helium, and lithium, and beryllium, and boron'"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"glue.join(elements)"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hydrogen helium lithium beryllium boron'"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\" \".join(elements)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"RANDOMNESS in PYTHON: HOW to DO IT"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['hydrogen', 'helium', 'lithium', 'beryllium', 'boron']"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"elements"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"random.shuffle(elements)"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['helium', 'beryllium', 'boron', 'lithium', 'hydrogen']"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"elements"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"text = \"it was a dark and stormy night\""
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"words = text.split()\n",
"random.shuffle(words)"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['a', 'and', 'dark', 'it', 'stormy', 'night', 'was']"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"words"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'a and dark it stormy night was'"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\" \".join(words)"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"it and stormy a dark was night\n"
]
}
],
"source": [
"text = \"it was a dark and stormy night\"\n",
"words = text.split()\n",
"random.shuffle(words)\n",
"print(\" \".join(words))"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"stormy it a night dark was and\n"
]
}
],
"source": [
"print(\" \".join(random.sample(words, len(words))))"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.choice([1, 2, 3, 4, 6])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"text files as lists of lines"
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Rose, harsh rose, \\n',\n",
" 'marred and with stint of petals, \\n',\n",
" 'meagre flower, thin, \\n',\n",
" 'spare of leaf,\\n',\n",
" '\\n',\n",
" 'more precious \\n',\n",
" 'than a wet rose \\n',\n",
" 'single on a stem -- \\n',\n",
" 'you are caught in the drift.\\n',\n",
" '\\n',\n",
" 'Stunted, with small leaf, \\n',\n",
" 'you are flung on the sand, \\n',\n",
" 'you are lifted \\n',\n",
" 'in the crisp sand \\n',\n",
" 'that drives in the wind.\\n',\n",
" '\\n',\n",
" 'Can the spice-rose \\n',\n",
" 'drip such acrid fragrance \\n',\n",
" 'hardened in a leaf?\\n']"
]
},
"execution_count": 177,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[line for line in open(\"sea_rose.txt\")]"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"poem = [line.strip() for line in open(\"sea_rose.txt\")]"
]
},
{
"cell_type": "code",
"execution_count": 180,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 180,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(poem)"
]
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 181,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(poem[0])"
]
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"19"
]
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(poem)"
]
},
{
"cell_type": "code",
"execution_count": 183,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['spare of leaf,',\n",
" '',\n",
" 'more precious',\n",
" 'than a wet rose',\n",
" 'single on a stem --',\n",
" 'you are caught in the drift.',\n",
" '',\n",
" 'Stunted, with small leaf,']"
]
},
"execution_count": 183,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"poem[3:11]"
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hardened in a leaf?'"
]
},
"execution_count": 184,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"poem[-1]"
]
},
{
"cell_type": "code",
"execution_count": 192,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'more precious'"
]
},
"execution_count": 192,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.choice(poem)"
]
},
{
"cell_type": "code",
"execution_count": 193,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['than a wet rose', 'Can the spice-rose', 'drip such acrid fragrance']"
]
},
"execution_count": 193,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.sample(poem, 3)"
]
},
{
"cell_type": "code",
"execution_count": 204,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['',\n",
" 'more precious',\n",
" 'you are lifted',\n",
" 'you are flung on the sand,',\n",
" 'marred and with stint of petals,',\n",
" 'single on a stem --',\n",
" '',\n",
" 'than a wet rose',\n",
" 'hardened in a leaf?',\n",
" 'in the crisp sand',\n",
" 'drip such acrid fragrance',\n",
" 'spare of leaf,',\n",
" 'Stunted, with small leaf,',\n",
" '',\n",
" 'Can the spice-rose',\n",
" 'you are caught in the drift.',\n",
" 'meagre flower, thin,',\n",
" 'Rose, harsh rose,',\n",
" 'that drives in the wind.']"
]
},
"execution_count": 204,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.sample(poem, len(poem))"
]
},
{
"cell_type": "code",
"execution_count": 206,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"poem_clean = [line for line in poem if line != \"\"]"
]
},
{
"cell_type": "code",
"execution_count": 207,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Rose, harsh rose,',\n",
" 'marred and with stint of petals,',\n",
" 'meagre flower, thin,',\n",
" 'spare of leaf,',\n",
" 'more precious',\n",
" 'than a wet rose',\n",
" 'single on a stem --',\n",
" 'you are caught in the drift.',\n",
" 'Stunted, with small leaf,',\n",
" 'you are flung on the sand,',\n",
" 'you are lifted',\n",
" 'in the crisp sand',\n",
" 'that drives in the wind.',\n",
" 'Can the spice-rose',\n",
" 'drip such acrid fragrance',\n",
" 'hardened in a leaf?']"
]
},
"execution_count": 207,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"poem_clean"
]
},
{
"cell_type": "code",
"execution_count": 209,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['ROSE, HARSH ROSE,',\n",
" 'MARRED AND WITH STINT OF PETALS,',\n",
" 'MEAGRE FLOWER, THIN,',\n",
" 'SPARE OF LEAF,',\n",
" '',\n",
" 'MORE PRECIOUS',\n",
" 'THAN A WET ROSE',\n",
" 'SINGLE ON A STEM --',\n",
" 'YOU ARE CAUGHT IN THE DRIFT.',\n",
" '',\n",
" 'STUNTED, WITH SMALL LEAF,',\n",
" 'YOU ARE FLUNG ON THE SAND,',\n",
" 'YOU ARE LIFTED',\n",
" 'IN THE CRISP SAND',\n",
" 'THAT DRIVES IN THE WIND.',\n",
" '',\n",
" 'CAN THE SPICE-ROSE',\n",
" 'DRIP SUCH ACRID FRAGRANCE',\n",
" 'HARDENED IN A LEAF?']"
]
},
"execution_count": 209,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[line.upper() for line in poem]"
]
},
{
"cell_type": "code",
"execution_count": 211,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Rose, haaaaarsh rose,',\n",
" 'maaaaarred aaaaand with stint of petaaaaals,',\n",
" 'meaaaaagre flower, thin,',\n",
" 'spaaaaare of leaaaaaf,',\n",
" '',\n",
" 'more precious',\n",
" 'thaaaaan aaaaa wet rose',\n",
" 'single on aaaaa stem --',\n",
" 'you aaaaare caaaaaught in the drift.',\n",
" '',\n",
" 'Stunted, with smaaaaall leaaaaaf,',\n",
" 'you aaaaare flung on the saaaaand,',\n",
" 'you aaaaare lifted',\n",
" 'in the crisp saaaaand',\n",
" 'thaaaaat drives in the wind.',\n",
" '',\n",
" 'Caaaaan the spice-rose',\n",
" 'drip such aaaaacrid fraaaaagraaaaance',\n",
" 'haaaaardened in aaaaa leaaaaaf?']"
]
},
"execution_count": 211,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[line.replace(\"a\", \"aaaaa\") for line in poem]"
]
},
{
"cell_type": "code",
"execution_count": 212,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['marred and with stint of petals,',\n",
" 'meagre flower, thin,',\n",
" 'more precious',\n",
" 'single on a stem --',\n",
" 'Stunted, with small leaf,']"
]
},
"execution_count": 212,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[line for line in poem if 'm' in line]"
]
},
{
"cell_type": "code",
"execution_count": 213,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Rose,rose,',\n",
" 'marretals,',\n",
" 'meagrthin,',\n",
" 'spareleaf,',\n",
" '',\n",
" 'more cious',\n",
" 'than rose',\n",
" 'singlem --',\n",
" 'you arift.',\n",
" '',\n",
" 'Stuntleaf,',\n",
" 'you asand,',\n",
" 'you aifted',\n",
" 'in th sand',\n",
" 'that wind.',\n",
" '',\n",
" 'Can t-rose',\n",
" 'drip rance',\n",
" 'hardeleaf?']"
]
},
"execution_count": 213,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[line[:5] + line[-5:] for line in poem]"
]
},
{
"cell_type": "code",
"execution_count": 222,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Rose, harsh rose,',\n",
" 'marred and with stint of petals,',\n",
" 'meagre flower, thin,',\n",
" 'single on a stem --',\n",
" 'you are caught in the drift.',\n",
" 'Stunted, with small leaf,',\n",
" 'you are flung on the sand,',\n",
" 'in the crisp sand',\n",
" 'that drives in the wind.',\n",
" 'Can the spice-rose',\n",
" 'drip such acrid fragrance',\n",
" 'hardened in a leaf?']"
]
},
"execution_count": 222,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[line for line in poem if len(line) > 15]"
]
},
{
"cell_type": "code",
"execution_count": 223,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"poem_a = [line.strip() for line in open(\"sea_rose.txt\")]"
]
},
{
"cell_type": "code",
"execution_count": 224,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"poem_b = [line.strip() for line in open(\"frost.txt\")]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"adding lists together gives you a new list with all of their elements!"
]
},
{
"cell_type": "code",
"execution_count": 247,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"all_lines = poem_a + poem_b"
]
},
{
"cell_type": "code",
"execution_count": 248,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"random.shuffle(all_lines)"
]
},
{
"cell_type": "code",
"execution_count": 249,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['meagre flower, thin,',\n",
" 'Then took the other, as just as fair,',\n",
" 'In leaves no step had trodden black.',\n",
" '',\n",
" 'that drives in the wind.',\n",
" 'I took the one less travelled by,',\n",
" 'And looked down one as far as I could',\n",
" 'hardened in a leaf?',\n",
" 'spare of leaf,',\n",
" 'And be one traveler, long I stood',\n",
" 'Two roads diverged in a yellow wood,',\n",
" 'Can the spice-rose',\n",
" 'And both that morning equally lay',\n",
" 'Stunted, with small leaf,',\n",
" 'drip such acrid fragrance',\n",
" 'more precious',\n",
" 'Rose, harsh rose,',\n",
" 'Had worn them really about the same,',\n",
" 'I doubted if I should ever come back.',\n",
" 'Though as for that the passing there',\n",
" 'Because it was grassy and wanted wear;',\n",
" 'marred and with stint of petals,',\n",
" 'And sorry I could not travel both',\n",
" 'Somewhere ages and ages hence:',\n",
" '',\n",
" '',\n",
" '',\n",
" 'Oh, I kept the first for another day!',\n",
" 'in the crisp sand',\n",
" 'Two roads diverged in a wood, and I---',\n",
" 'you are flung on the sand,',\n",
" 'you are lifted',\n",
" 'single on a stem --',\n",
" 'I shall be telling this with a sigh',\n",
" 'Yet knowing how way leads on to way,',\n",
" '',\n",
" 'you are caught in the drift.',\n",
" 'And having perhaps the better claim,',\n",
" 'than a wet rose',\n",
" 'And that has made all the difference.',\n",
" 'To where it bent in the undergrowth;',\n",
" '']"
]
},
"execution_count": 249,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all_lines"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"you can also get a list of words from a file!"
]
},
{
"cell_type": "code",
"execution_count": 238,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"words = open(\"sea_rose.txt\").read().split()"
]
},
{
"cell_type": "code",
"execution_count": 240,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['single',\n",
" 'sand,',\n",
" 'that',\n",
" 'are',\n",
" 'more',\n",
" 'drip',\n",
" 'you',\n",
" 'on',\n",
" 'with',\n",
" 'spare']"
]
},
"execution_count": 240,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.sample(words, 10)"
]
},
{
"cell_type": "code",
"execution_count": 242,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"all_words = (open(\"sea_rose.txt\").read() + open(\"frost.txt\").read()).split()"
]
},
{
"cell_type": "code",
"execution_count": 245,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Because',\n",
" 'Stunted,',\n",
" 'Can',\n",
" 'travelled',\n",
" 'ages',\n",
" 'stood',\n",
" 'ages',\n",
" 'claim,',\n",
" 'the',\n",
" 'drives',\n",
" 'them',\n",
" 'was',\n",
" 'kept',\n",
" 'how',\n",
" 'just',\n",
" 'as',\n",
" 'made',\n",
" 'trodden',\n",
" 'stint',\n",
" 'looked',\n",
" 'hardened',\n",
" 'meagre',\n",
" 'one',\n",
" 'on',\n",
" 'first',\n",
" 'the',\n",
" 'stem',\n",
" 'thin,',\n",
" 'traveler,',\n",
" 'crisp',\n",
" 'other,',\n",
" 'more',\n",
" 'the',\n",
" 'flower,',\n",
" 'less',\n",
" 'on',\n",
" 'I',\n",
" 'way,',\n",
" 'sand,',\n",
" 'leaf,',\n",
" 'a',\n",
" 'down',\n",
" 'with',\n",
" 'the',\n",
" 'had',\n",
" 'this',\n",
" 'a',\n",
" 'one',\n",
" 'I',\n",
" 'And']"
]
},
"execution_count": 245,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random.sample(all_words, 50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"format that list in a nice way!"
]
},
{
"cell_type": "code",
"execution_count": 253,
"metadata": {
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"sonnet = random.sample(all_lines, 14)"
]
},
{
"cell_type": "code",
"execution_count": 254,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"than a wet rose\n",
"Somewhere ages and ages hence:\n",
"you are flung on the sand,\n",
"I doubted if I should ever come back.\n",
"in the crisp sand\n",
"you are caught in the drift.\n",
"And that has made all the difference.\n",
"I took the one less travelled by,\n",
"Though as for that the passing there\n",
"meagre flower, thin,\n",
"Had worn them really about the same,\n",
"that drives in the wind.\n",
"And both that morning equally lay\n",
"\n"
]
}
],
"source": [
"print('\\n'.join(sonnet))"
]
},
{
"cell_type": "code",
"execution_count": 255,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"paragraph = random.sample(all_words, 100)"
]
},
{
"cell_type": "code",
"execution_count": 257,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I And as that Stunted, drip and be all was took a perhaps single and doubted both in by, traveler, just way in first wet are how it of same, the of yellow spare drift. on bent that And Though ever to I kept I stint with another really the claim, wanted that be it the come in Two a knowing I lifted on having I the way, wood, there the wood, stem long travel you you harsh equally could not a leaf, Can took where marred one Then sand, as as And a -- stood passing a drives shall\n"
]
}
],
"source": [
"print(\" \".join(paragraph))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"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