前记
一直以来隐隐中总有担忧,忧其技术不精,而误人子弟,而慌慌前行。
扪心自问,是否可以写free package了?是否了解了python的所以然?是否跟随了业界大咖的pythonical? 为何内心总认为Paper是负担?
究其内在原因,仍是基础不足,急迫前行,古人云“智者务其实,愚者争其名”,若想大成,须有大成的潜伏。
因此从基础开始翻阅起,记录所有Python的相关知识,抱着重新复盘的心态。
Flag:
四部曲
《introducing python 1st edition-Bill Lubanovic》
《Python data science hand book-Jake VanderPlas》
《Fluent python-Luciano Ramalho》
《Dive in pytorch》
节奏:
每日一更
2022年5月3日16:22:13,五一节的尾声,这几天始终在看《introducing python 1st edition》,一本比较简单的外文书。即便书的内容简单,但是仍有很多是我们平时未有效掌握的。
为什么要去了解帮助?
很多时候我们在写代码时的基本状态均为不停地网络上查找各种资料,百度是最好的帮助文档。然而,随着我们copy多了之后,也会有很多的困惑,或者针对部分难以找到信息的内容,无从下手,此时,系统地了解帮助的几种查看方式,可以帮我们打开一片新的空间。这是系统性学习的第一步。
几种帮助的形式与使用途径
方式1:dir() build-in-function
获取该对象的大部分相关属性、函数。
这种方式的好处是可以看到包的全貌,但是看不到详细的信息,一般用得少,推荐指数❤。
方式2:help() build-in-function
与__doc__基本一致。
这种方式用于看docstring,不足之处在于要在python console里面输入信息,推荐指数❤❤
方式3:*.doc 属性
将这些描述性文字信息(docstring)输出。
这种方式用于看docstring,不足之处在于要在python console里面输入信息,推荐指数❤❤
方式4:ipython magic operator ??
需要专门启动一个ipython console or jupyter notebook。
这种方式的不足是在使用jupyter notebook时方便,而在集成开发的时候效果一般,推荐指数❤❤
方式5:启动本地在线帮助服务
python自带的帮助文档
在cmd下输入:
python -m pydoc -p 4444
【注】cmd窗口不要关闭
然后,访问这个url即可,每个模块里面都有帮助信息,比起命令行下的帮助信息看起来更加美观,使用起来也很方便一条命令的事情。
转载于:https://blog.csdn.net/weixin_30649859/article/details/96141922
该方式的搜索还是有些慢的,可以作为查看当前包信息的一种途径,使用起来便捷度有限,推荐指数❤。
方式6:pycharm中直接使用
pycharm中的documentation内置显示(默认快捷键为Ctrl+Q)
External documetation,快捷键为Shift+F1
亲测,该方式最便捷,推荐指数❤❤❤❤❤。
实用python技巧
输出dir查看的对象信息
# 查看math包中有的对象和属性
import math
print(dir(math))
输出结果
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh',……]
以简洁方式过滤掉dir() build-in-function中的double underline属性和方法
import pprint
import math
# 注意如果在lambda中使用if进行条件判断,则else是必须声明的,否则会引起报错。如果不返回结果可以用 else None 表示。
pprint.pp(list(filter(lambda x: x if "_" not in x else None,dir(math))) )
输出结果
['acos',
'acosh',
'asin',
'asinh',
'atan',
'atan2',
'atanh',
'ceil',
'comb',
'copysign',
'cos',
'cosh',
'degrees',
'dist',
'e',
]
pycharm python console清空的方法
右击 clear all
map reduce filter for list object
map
def suqare(x):
return x*x
list_a=[i*2 for i in range(10)]
list_a_square=list(map(suqare,list_a))
print(list_a_square)
list_a_squared2 = list(map(lambda x: x**2, list_a)) # 方式2,用lambda表达式
print(list_a_squared2)
输出结果
[0, 4, 16, 36, 64, 100, 144, 196, 256, 324]
[0, 4, 16, 36, 64, 100, 144, 196, 256, 324]
reduce
from functools import reduce
def accumulate(x,y):
return x*y
list_b=range(1,5)
# reduce的工作过程是 :在迭代序列的过程中,首先把 前两个元素(只能两个)传给 函数,函数加工后,然后把 得到的结果和第三个元素 作为两个参数传给函数参数, 函数加工后得到的结果又和第四个元素 作为两个参数传给函数参数,依次类推。
result=reduce(accumulate,list_b)
print(result)
# lambda 方式
result_b_2=reduce(lambda x,y:x*y,list_b)
print(result_b_2)
输出结果
24
24
filter
list_c=['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
def is_having_score_line(x):
if "_" in x: # 等价于x.contains("_")
pass
else:
return x
list_c_without_score_lines=list(filter(is_having_score_line,list_c))
print(list_c_without_score_lines)
import pprint
#注意如果在lambda中使用if进行条件判断,则else是必须声明的,否则会引起报错。如果不返回结果可以用 else None 表示。
pprint.pp(list(filter(lambda x: x if "_" not in x else None,list_c)) )
输出结果
['acos', 'acosh', 'asin', 'asinh', 'atan',……]
['acos',
'acosh',
'asin',
'asinh',
'atan',
'atan2',
'atanh',
'ceil',
'comb',……
]
https://blog.csdn.net/DQ_DM/article/details/45672623?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165156775816781667847537%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=165156775816781667847537&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~hot_rank-1-45672623.142v9pc_search_result_cache,157v4new_style&utm_term=python+++%E5%B8%AE%E5%8A%A9&spm=1018.2226.3001.4187 ↩︎
https://blog.csdn.net/lglfa/article/details/80466449?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165156775816781667847537%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=165156775816781667847537&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~hot_rank-5-80466449.142v9pc_search_result_cache,157v4new_style&utm_term=python+++%E5%B8%AE%E5%8A%A9&spm=1018.2226.3001.4187 ↩︎