331 lines
6.5 KiB
Plaintext
331 lines
6.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# namedtuple"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## namedtuple 使用示例"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Point 1: Point(x=1, y=1), Point 2: Point(x=2, y=3)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from collections import namedtuple\n",
|
|
"\n",
|
|
"Point = namedtuple(\"Point\", [\"x\", \"y\"])\n",
|
|
"p1 = Point(x=1, y=1)\n",
|
|
"p2 = Point(2, 3)\n",
|
|
"print(f\"Point 1: {p1}, Point 2: {p2}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'x': 3, 'y': 4}"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"p3 = Point(p1.x + p2.x, p1.y + p2.y)\n",
|
|
"p3._asdict()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 自定义 `Point` 类"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class Point:\n",
|
|
" def __init__(self, x: int, y: int):\n",
|
|
" self.x = x\n",
|
|
" self.y = y\n",
|
|
"\n",
|
|
" def __repr__(self):\n",
|
|
" return f\"{self.__class__.__name__}(x={self.x}, y={self.y})\"\n",
|
|
"\n",
|
|
"\n",
|
|
"p = Point(x=3, y=4)\n",
|
|
"p\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 使用 `NamedTuple` 类"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"Point(x=0, y=0)"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from typing import NamedTuple\n",
|
|
"\n",
|
|
"\n",
|
|
"class Point(NamedTuple):\n",
|
|
" x: int = 0\n",
|
|
" y: int = 0\n",
|
|
"\n",
|
|
" def __repr__(self):\n",
|
|
" return f\"{self.__class__.__name__}(x={self.x}, y={self.y})\"\n",
|
|
"\n",
|
|
"\n",
|
|
"p = Point()\n",
|
|
"p"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Counter"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[('is', 10),\n",
|
|
" ('better', 8),\n",
|
|
" ('than', 8),\n",
|
|
" ('to', 5),\n",
|
|
" ('the', 5),\n",
|
|
" ('of', 3),\n",
|
|
" ('Although', 3),\n",
|
|
" ('be', 3),\n",
|
|
" ('should', 2),\n",
|
|
" ('never', 2)]"
|
|
]
|
|
},
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from collections import Counter\n",
|
|
"from itertools import chain\n",
|
|
"\n",
|
|
"poem = \"\"\"\n",
|
|
"The Zen of Python, by Tim Peters\n",
|
|
"\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",
|
|
"split = [line.split() for line in poem.splitlines()]\n",
|
|
"words = list(chain(*[elem for elem in split if elem]))\n",
|
|
"words[1:10]\n",
|
|
"\n",
|
|
"counter = Counter(words)\n",
|
|
"counter.most_common(10)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# deque "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"deque([])"
|
|
]
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from collections import deque\n",
|
|
"\n",
|
|
"queue = deque()\n",
|
|
"queue"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"deque(['foo'])"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"queue.append(\"foo\")\n",
|
|
"queue"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"deque(['bar', 'bar', 'foo'])"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"queue.appendleft(\"bar\")\n",
|
|
"queue"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"queue.extendleft([\"zoo\", \"fuzz\"])\n",
|
|
"queue"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 19,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"deque(['foo', 'bar', 'baz'])"
|
|
]
|
|
},
|
|
"execution_count": 19,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"queue.rotate(1)\n",
|
|
"queue"
|
|
]
|
|
},
|
|
{
|
|
"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
|
|
}
|