随笔分类 -  python

 
三目运算
摘要:#三目运算符a = 6b = 5result = (a+b) if a > b else (b - a) 阅读全文
posted @ 2019-12-04 10:33 FinnChan 阅读(179) 评论(0) 推荐(0)
枚举
摘要:from enum import Enum Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) for name, member in 阅读全文
posted @ 2019-12-03 17:39 FinnChan 阅读(115) 评论(0) 推荐(0)
类的定制
摘要:__iter__ 如果一个类想被用于for ... in循环,类似list或tuple那样,就必须实现一个__iter__()方法,该方法返回一个迭代对象,然后,Python的for循环就会不断调用该迭代对象的__next__()方法拿到循环的下一个值,直到遇到StopIteration错误时退出循 阅读全文
posted @ 2019-12-03 16:10 FinnChan 阅读(216) 评论(0) 推荐(0)
type() & dir()
摘要:type( ) >>> import types >>> def fn(): ... pass ... >>> type(fn)==types.FunctionType True >>> type(abs)==types.BuiltinFunctionType True >>> type(lambd 阅读全文
posted @ 2019-12-03 14:54 FinnChan 阅读(263) 评论(0) 推荐(0)
摘要:如果不同的人编写的模块名相同怎么办?为了避免模块名冲突,Python又引入了按目录来组织模块的方法,称为包(Package)。 举个例子,一个abc.py的文件就是一个名字叫abc的模块,一个xyz.py的文件就是一个名字叫xyz的模块。 现在,假设我们的abc和xyz这两个模块名字与其他模块冲突了 阅读全文
posted @ 2019-12-03 10:55 FinnChan 阅读(156) 评论(0) 推荐(0)
偏函数
摘要:functools.partial的作用就是,把一个函数的某些参数给固定住(也就是设置默认值),返回一个新的函数,调用这个新函数会更简单。 注意到上面的新的int2函数,仅仅是把base参数重新设定默认值为2,但也可以在函数调用时传入其他值: >>> int2('1000000', base=10) 阅读全文
posted @ 2019-12-03 10:42 FinnChan 阅读(192) 评论(0) 推荐(0)
Sorted
摘要:给sorted传入key函数,即可实现忽略大小写的排序: >>> sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower) ['about', 'bob', 'Credit', 'Zoo'] 要进行反向排序,不必改动key函数,可以传入第三个参 阅读全文
posted @ 2019-12-03 10:04 FinnChan 阅读(175) 评论(0) 推荐(0)
Filter
摘要:计算素数的一个方法是埃氏筛法,它的算法理解起来非常简单: 首先,列出从2开始的所有自然数,构造一个序列: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ... 取序列的第一个数2,它一定是素数,然后用2把序列的 阅读全文
posted @ 2019-12-03 09:42 FinnChan 阅读(146) 评论(0) 推荐(0)
map & reduce
摘要:map>>> def f(x): ... return x * x ... >>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> list(r) [1, 4, 9, 16, 25, 36, 49, 64, 81] map()传入的第一个参数是f,即函数对象本 阅读全文
posted @ 2019-12-02 17:55 FinnChan 阅读(79) 评论(0) 推荐(0)
Generator
摘要:要创建一个generator,有很多种方法。第一种方法很简单,只要把一个列表生成式的[]改成(),就创建了一个generator: >>> L = [x * x for x in range(10)] >>> L [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> g 阅读全文
posted @ 2019-12-02 15:46 FinnChan 阅读(591) 评论(0) 推荐(0)
切片
摘要:只写[:]就可以原样复制一个list: >>> L[:] [0, 1, 2, 3, ..., 99] tuple也是一种list,唯一区别是tuple不可变。因此,tuple也可以用切片操作,只是操作的结果仍是tuple: >>> (0, 1, 2, 3, 4, 5)[:3] (0, 1, 2) 字 阅读全文
posted @ 2019-12-02 14:40 FinnChan 阅读(92) 评论(0) 推荐(0)
函数参数
摘要:默认参数 先定义一个函数,传入一个list,添加一个END再返回: def add_end(L=[]): L.append('END') return L 当你正常调用时,结果似乎不错: >>> add_end([1, 2, 3]) [1, 2, 3, 'END'] >>> add_end(['x' 阅读全文
posted @ 2019-12-02 10:59 FinnChan 阅读(116) 评论(0) 推荐(0)
Dict & Set
摘要:Dict 要避免key不存在的错误,有两种办法,一是通过in判断key是否存在: >>> 'Thomas' in d False 二是通过dict提供的get()方法,如果key不存在,可以返回None,或者自己指定的value: >>> d.get('Thomas') >>> d.get('Tho 阅读全文
posted @ 2019-11-29 18:42 FinnChan 阅读(98) 评论(0) 推荐(0)
list,tuple
摘要:list >>> classmates.append('Adam') >>> classmates.insert(1, 'Jack') >>> classmates.pop() >>> classmates.pop(1) tuple tuple和list非常类似,但是tuple一旦初始化就不能修改 阅读全文
posted @ 2019-11-29 16:26 FinnChan 阅读(100) 评论(0) 推荐(0)
字符串格式化
摘要:>>> 'Hello,{0}!Your grade is {1:.2f}.That\'s a {2:.2f}% improvement from last time.'.format('Finn',89.9999,(89.9999-75)/75*100)"Hello,Finn!Your grade 阅读全文
posted @ 2019-11-29 15:46 FinnChan 阅读(76) 评论(0) 推荐(0)
字符串编码
摘要:在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码。 用记事本编辑的时候,从文件读取的UTF-8字符被转换为Unicode字符到内存里,编辑完成后,保存的时候再把Unicode转换为UTF-8保存到文件 由于Python的字符串类型是str,在内存 阅读全文
posted @ 2019-11-29 15:39 FinnChan 阅读(331) 评论(0) 推荐(0)
numpy深浅复制
摘要:In [1]: import numpy as np #Simple assignments make no copy of array objects or of their data. a = np.arange(12) b = a # a and b are two names for the 阅读全文
posted @ 2019-09-30 20:43 FinnChan 阅读(186) 评论(0) 推荐(0)
matplotlib基础学习
摘要:// <![CDATA[ MathJax.Hub.Config({ tex2jax: { inlineMath: [ ['$','$'], ["\\(","\\)"] ], displayMath: [ ['$$','$$'], ["\\[","\\]"] ], processEscapes: tr 阅读全文
posted @ 2019-09-28 21:17 FinnChan 阅读(172) 评论(0) 推荐(0)
pandas基础学习
摘要:// <![CDATA[ MathJax.Hub.Config({ tex2jax: { inlineMath: [ ['$','$'], ["\\(","\\)"] ], displayMath: [ ['$$','$$'], ["\\[","\\]"] ], processEscapes: tr 阅读全文
posted @ 2019-09-28 21:16 FinnChan 阅读(198) 评论(0) 推荐(0)
numpy基础学习
摘要:array.flattern() 展开成一维数组 array.dtype array.astype() 改变数组类型 array.where(条件,true,false) array.clip(min,max) array.mean() 均值 array.std() 标准差 np.round(arr 阅读全文
posted @ 2019-09-28 21:16 FinnChan 阅读(142) 评论(0) 推荐(0)