Skip to content

Instantly share code, notes, and snippets.

@saboyutaka
Last active February 6, 2017 06:33
Show Gist options
  • Save saboyutaka/7c88f1d2ccf24f0e74d36a2ca6255f7c to your computer and use it in GitHub Desktop.
Save saboyutaka/7c88f1d2ccf24f0e74d36a2ca6255f7c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python 3日目\n",
"\n",
"## List, Tuple, Dictionaly"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### List\n",
"* Pythonの世界の配列。Listは可変長配列。追加や削除が行える。\n",
"* 他のプログラミング言語と同じで順序を持つ。\n",
"* あるプログラムの中で配列の要素が変化するものはこちら。\n",
"* [] で表現される"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fruits = [\"Apple\", \"Banana\", \"Lemon\"]\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'Apple'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fruits[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tuple\n",
"* こちらも配列。配列をさらに追加・削除を行えなくした固定長配列。\n",
"* コンピューターの世界ではこちらの方が演算効率・メモリ効率が良いので、\n",
"* Listと同じで順序を持つ。\n",
"* あるプログラムの中で配列の要素が変化しないものはこちらを使ってあげるとコンピューターフレンドリー。\n",
"* あまり気にしないのであればList使っても良い。\n",
"* `()`で表現"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# 例えば、都道府県の配列。都道府県はそう簡単に増えたり減ったりしないのでtupleでok。\n",
"# 都道府県コードと合わせるために1つ目の要素はNone\n",
"prefecture = (None, \"北海道\", \"青森県\", \"岩手県\", \"宮城県\", \"秋田県\", \"山形県\", \"福島県\", \"茨城県\", \"栃木県\", \n",
" \"群馬県\", \"埼玉県\", \"千葉県\", \"東京都\", \"神奈川県\", \"新潟県\", \"富山県\", \"石川県\", \"福井県\", \n",
" \"山梨県\", \"長野県\", \"岐阜県\", \"静岡県\", \"愛知県\", \"三重県\", \"滋賀県\", \"京都府\", \"大阪府\", \n",
" \"兵庫県\", \"奈良県\", \"和歌山県\", \"鳥取県\", \"島根県\", \"岡山県\", \"広島県\", \"山口県\", \"徳島県\", \n",
" \"香川県\", \"愛媛県\", \"高知県\", \"福岡県\", \"佐賀県\", \"長崎県\", \"熊本県\", \"大分県\", \"宮崎県\", \n",
" \"鹿児島県\", \"沖縄\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"東京都\n",
"沖縄\n"
]
}
],
"source": [
"print(prefecture[13])\n",
"print(prefecture[47])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionaly\n",
"* Pythonの世界の連想配列(HashTable)。\n",
" * Javascriptの世界ではObjectと呼ばれてたモノ。Rubyの世界ではHash。\n",
"* KeyとValueで構成される。\n",
"* 順序を持たない。"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"person = {\n",
" \"name\": \"tarou\",\n",
" \"age\": 18,\n",
" \"hoby\": [\"Basketball\", \"Music\", \"Dance\"]\n",
"}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'tarou'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"person['name']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## リスト\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['append',\n",
" 'clear',\n",
" 'copy',\n",
" 'count',\n",
" 'extend',\n",
" 'index',\n",
" 'insert',\n",
" 'pop',\n",
" 'remove',\n",
" 'reverse',\n",
" 'sort']\n"
]
}
],
"source": [
"## dirで__がついてないものだけを表示\n",
"\n",
"import pprint as pp\n",
"\n",
"tuple_methods = []\n",
"for method in dir(list):\n",
" if not method.startswith(\"__\"):\n",
" tuple_methods.append(method)\n",
" \n",
"pp.pprint(tuple_methods)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c']\n",
"['a', 'b', 'c', 'd']\n"
]
}
],
"source": [
"# append\n",
"\n",
"x = [\"a\", \"b\", \"c\"]\n",
"print(x)\n",
"x.append(\"d\")\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c']\n",
"['a', 'b', 'c', 'd', 'e', 'f']\n"
]
}
],
"source": [
"# extend\n",
"# 複数追加、引数がstrか[]を受け付ける\n",
"\n",
"x = [\"a\", \"b\", \"c\"]\n",
"print(x)\n",
"x.extend([\"d\", \"e\", \"f\"])\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"remove method\n",
"['a', 'b', 'c']\n",
"['a', 'c']\n",
"del method\n",
"['あ', 'い', 'う']\n",
"['あ', 'う']\n"
]
}
],
"source": [
"# remove\n",
"\n",
"print(\"remove method\")\n",
"x = [\"a\", \"b\", \"c\"]\n",
"print(x)\n",
"x.remove(\"b\")\n",
"print(x)\n",
"\n",
"# del\n",
"\n",
"print(\"del method\")\n",
"y = [\"あ\", \"い\", \"う\"]\n",
"print(y)\n",
"del y[1]\n",
"print(y)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 3, 5, 10]\n",
"[10, 5, 3, 3, 2]\n"
]
}
],
"source": [
"# sort, reverse\n",
"x = [10, 3, 5, 2, 3]\n",
"x.sort()\n",
"print(x)\n",
"\n",
"x.reverse()\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## range\n",
"\n",
"Python3ではrangeがrange classを返すように変わってる"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"range(0, 5)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"range(5)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(range(5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## タプル"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['count', 'index']\n"
]
}
],
"source": [
"## dirで__がついてないものだけを表示\n",
"\n",
"import pprint as pp\n",
"\n",
"tuple_methods = []\n",
"for method in dir(tuple):\n",
" if not method.startswith(\"__\"):\n",
" tuple_methods.append(method)\n",
" \n",
"pp.pprint(tuple_methods)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"_人人人人人人人人人人人人人_\n",
"> countとindexしかない! <\n",
" ̄Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y ̄\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"('Hello', 'World')"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 勝手にTupleに変換されてる\n",
"\"Hello\", \"World\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ディクショナリ"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['clear',\n",
" 'copy',\n",
" 'fromkeys',\n",
" 'get',\n",
" 'items',\n",
" 'keys',\n",
" 'pop',\n",
" 'popitem',\n",
" 'setdefault',\n",
" 'update',\n",
" 'values']\n"
]
}
],
"source": [
"# dir \n",
"import pprint as pp\n",
"\n",
"dict_methods = []\n",
"for method in dir({}):\n",
" if not method.startswith(\"__\"):\n",
" dict_methods.append(method)\n",
" \n",
"pp.pprint(dict_methods)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"person = {\n",
" \"name\": \"tarou\",\n",
" \"age\": 18,\n",
" \"hoby\": [\"Basketball\", \"Music\", \"Dance\"]\n",
"}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tarou\n"
]
}
],
"source": [
"print(person[\"name\"])"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['hoby', 'age', 'name'])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"person.keys()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['hoby', 'age', 'name']"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(person.keys())"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_items([('hoby', ['Basketball', 'Music', 'Dance']), ('age', 18), ('name', 'tarou')])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"person.items()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"key: hoby value: ['Basketball', 'Music', 'Dance']\n",
"key: age value: 18\n",
"key: name value: tarou\n"
]
}
],
"source": [
"for k in person:\n",
" print(\"key: %s value: %s\" % (k, person[k]))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"key: hoby value: ['Basketball', 'Music', 'Dance']\n",
"key: age value: 18\n",
"key: name value: tarou\n"
]
}
],
"source": [
"for k in person.keys():\n",
" print(\"key: %s value: %s\" % (k, person[k]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### has_key [deprecated]\n",
"\n",
"Python3ではhas_keyがなくなってる。\n",
"代わりに in を使う\n",
"\n",
"```python\n",
"# Python2\n",
"person.has_key('name') # True\n",
"\n",
"# Python3\n",
"'name' in person # True\n",
"```\n",
"\n",
"[参考]http://sscrisk.hatenablog.com/entry/20100502/1272784776"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'name' in person\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda root]",
"language": "python",
"name": "conda-root-py"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment