多行格式
In [305]:
Copied!
# 增加,减少,剪切,保存,删除等
# a, b, x, s, dd
# 增加,减少,剪切,保存,删除等
# a, b, x, s, dd
In [301]:
Copied!
# 合并,执行
# shift+M shift+Enter
# 合并,执行
# shift+M shift+Enter
In [304]:
Copied!
# 显示行数,切换markdown/code
# l, m/y
# 显示行数,切换markdown/code
# l, m/y
Bash命令¶
斜体
斜体2
加粗字体
import numpy as np # 单行代码
# 多行格式
{
"name": "Yam",
"age": 22
}
引用
列表:
- 第一个
- 第二个
TODO:
- 第一个
- 第二个
图片:
表格:
name | age |
---|---|
Yam | 22 |
In [117]:
Copied!
!ls
!ls
Basic.ipynb NumPy.ipynb data exit Notebook.ipynb __pycache__ demo.py tianchi
In [11]:
Copied!
!head data/table.txt
!head data/table.txt
organization established ceo Google 1998 Sundar Pichai Microsoft 1975 Satya Nadella Nokia 1865 Rajeev Suri
In [12]:
Copied!
!ls | wc -l
!ls | wc -l
8
In [13]:
Copied!
!cat demo.py
!cat demo.py
def add(a: int, b: int) -> int: return a + b a = 10 b = 10 res = add(a, b) print(res)
In [18]:
Copied!
!python3.8 demo.py
!python3.8 demo.py
20
In [17]:
Copied!
!which python
!which python
/usr/bin/python
In [ ]:
Copied!
!pip install ...
!pip install ...
Magic¶
In [108]:
Copied!
# 列出魔法方法
%lsmagic
# 列出魔法方法
%lsmagic
Out[108]:
Available line magics: %alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %conda %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics.
In [109]:
Copied!
# 设置环境变量
%env PROFILE=dev
# 设置环境变量
%env PROFILE=dev
env: PROFILE=dev
In [114]:
Copied!
!echo $PROFILE
!echo $PROFILE
dev
In [115]:
Copied!
import os
import os
In [116]:
Copied!
os.environ.get("PROFILE")
os.environ.get("PROFILE")
Out[116]:
'dev'
最有用/常用的是timeit,用来评估运行效率。接下来,我们用两个排序的例子来说明。
In [307]:
Copied!
def choose_sort(arr: list) -> list:
length = len(arr)
for i in range(length):
min_idx = i
for j in range(i, length):
if arr[j] < arr[min_idx]:
min_idx = j
if min_idx != i:
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
def quick_sort(arr: list) -> list:
def qs(arr, l, r):
if l < r:
p = partion(arr, l, r)
qs(arr, l, p-1)
qs(arr, p, r)
def partion(arr: list, l: int, r: int) -> int:
pivot = arr[l + (r-l)//2]
while l<= r:
while arr[l] < pivot:
l += 1
while arr[r] > pivot:
r -= 1
if l <= r:
arr[l], arr[r] = arr[r], arr[l]
l += 1
r -= 1
return l
l, r = 0, len(arr) - 1
qs(arr, l, r)
return arr
def choose_sort(arr: list) -> list:
length = len(arr)
for i in range(length):
min_idx = i
for j in range(i, length):
if arr[j] < arr[min_idx]:
min_idx = j
if min_idx != i:
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
def quick_sort(arr: list) -> list:
def qs(arr, l, r):
if l < r:
p = partion(arr, l, r)
qs(arr, l, p-1)
qs(arr, p, r)
def partion(arr: list, l: int, r: int) -> int:
pivot = arr[l + (r-l)//2]
while l<= r:
while arr[l] < pivot:
l += 1
while arr[r] > pivot:
r -= 1
if l <= r:
arr[l], arr[r] = arr[r], arr[l]
l += 1
r -= 1
return l
l, r = 0, len(arr) - 1
qs(arr, l, r)
return arr
In [308]:
Copied!
import random
import random
In [309]:
Copied!
lst = [random.randint(1, 1000) for i in range(1000)]
lst = [random.randint(1, 1000) for i in range(1000)]
In [288]:
Copied!
%timeit choose_sort(lst)
%timeit choose_sort(lst)
43 ms ± 2.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [289]:
Copied!
%timeit quick_sort(lst)
%timeit quick_sort(lst)
1.79 ms ± 51.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [290]:
Copied!
%timeit sorted(lst)
%timeit sorted(lst)
7.65 µs ± 270 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [315]:
Copied!
# 两个%表示多行,且 %%timeie 必须放在第一行
# 两个%表示多行,且 %%timeie 必须放在第一行
In [314]:
Copied!
%%timeit
choose_sort(lst)
sorted(lst)
%%timeit
choose_sort(lst)
sorted(lst)
44.8 ms ± 5.06 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
此外,还有按执行代码单行分析,内存分析等功能。具体可查看参考文献【2】。
笔记本 or 草稿纸?¶
我们可以随意执行任意代码,就像你在草稿纸上打草稿一样。
In [292]:
Copied!
1 + 2
1 + 2
Out[292]:
3
In [293]:
Copied!
3 ** 3
3 ** 3
Out[293]:
27
In [1]:
Copied!
x = [1, 2, 3]
y = [4, 5, 6]
x = [1, 2, 3]
y = [4, 5, 6]
In [2]:
Copied!
sorted(y + x)
sorted(y + x)
Out[2]:
[1, 2, 3, 4, 5, 6]
In [4]:
Copied!
# 随时随地可查看接口文档
x.append?
# 随时随地可查看接口文档
x.append?
In [ ]:
Copied!
# tab 补全
x.
# tab 补全
x.
再来点文本,注意,它支持Markdown格式。
也支持LaTeX格式的数学公式。
$$ P(A|B) = \frac{P(B|A)P(A)}{P(B)} \\ e^{i \pi} = 0 $$
再多一点¶
我们也可以将Notebook作为日常工作的工具,可以写各种模型、业务代码。
比如,我们读取 table.txt 的数据,并对结果按创建时间排序。对这个简单任务,可以先写一个读文本的函数,再写个关于这个任务的类。
In [1]:
Copied!
def read_file(file_path: str, num: int = -1, sep: str = "\t"):
"""
Parameters
-----------
file_path: The file path.
num: How many lines would read, default is -1, means all lines.
sep: seperator for the line.
Return
------
A generator of the splited item.
"""
n = 0
with open(file_path, "r") as f:
for i, line in enumerate(f):
if num == -1 or n < num:
yield line.strip().split(sep)
else:
break
n += 1
def read_file(file_path: str, num: int = -1, sep: str = "\t"):
"""
Parameters
-----------
file_path: The file path.
num: How many lines would read, default is -1, means all lines.
sep: seperator for the line.
Return
------
A generator of the splited item.
"""
n = 0
with open(file_path, "r") as f:
for i, line in enumerate(f):
if num == -1 or n < num:
yield line.strip().split(sep)
else:
break
n += 1
In [9]:
Copied!
class Task:
"""
A Company sort task.
"""
def __init__(self, file_path: str):
self.data = self._load_file(file_path)
def _load_file(self, file_path: str) -> list:
res = []
lines = read_file(file_path)
for i, item in enumerate(lines):
if i == 0:
continue
company = {
"org": item[0],
"est": int(item[1]),
"ceo": item[2]
}
res.append(company)
return res
def order_by(self, field: str = "est") -> list:
return sorted(self.data, key=lambda x: x.get(field, "est"))
class Task:
"""
A Company sort task.
"""
def __init__(self, file_path: str):
self.data = self._load_file(file_path)
def _load_file(self, file_path: str) -> list:
res = []
lines = read_file(file_path)
for i, item in enumerate(lines):
if i == 0:
continue
company = {
"org": item[0],
"est": int(item[1]),
"ceo": item[2]
}
res.append(company)
return res
def order_by(self, field: str = "est") -> list:
return sorted(self.data, key=lambda x: x.get(field, "est"))
In [10]:
Copied!
task = Task("data/table.txt")
task = Task("data/table.txt")
In [11]:
Copied!
task.order_by()
task.order_by()
Out[11]:
[{'org': 'Nokia', 'est': 1865, 'ceo': 'Rajeev Suri'}, {'org': 'Microsoft', 'est': 1975, 'ceo': 'Satya Nadella'}, {'org': 'Google', 'est': 1998, 'ceo': 'Sundar Pichai'}]
In [7]:
Copied!
task.order_by("org")
task.order_by("org")
Out[7]:
[{'org': 'Google', 'est': 1998, 'ceo': 'Sundar Pichai'}, {'org': 'Microsoft', 'est': 1975, 'ceo': 'Satya Nadella'}, {'org': 'Nokia', 'est': 1865, 'ceo': 'Rajeev Suri'}]
也可以导入外部的文件。
In [105]:
Copied!
from demo import add
from demo import add
20
In [106]:
Copied!
add(3, 4)
add(3, 4)
Out[106]:
7
练习¶
- 请自行尝试本节涉及到的和相关的命令,尝试修改其中的内容。
- 请将你的学习笔记(文本+代码)以Notebook记录。
参考和资料¶
- 【1】Built-in magic commands — IPython 8.1.1 documentation
- 【2】Jake VanderPlas,《Pythoon 数据科学手册》,人民邮电出版社,2018年2月
In [ ]:
Copied!
最后更新:
November 30, 2023
创建日期: November 30, 2023
创建日期: November 30, 2023