摘要: import time as tfrom os import pathdef createFile(dest): date = t.localtime(t.time()) name = '%d_%d_%d.txt' %(date[1],date... 阅读全文
posted @ 2017-06-11 20:38 2021年的顺遂平安君 阅读(69) 评论(0) 推荐(0)
摘要: 查看当前目录下所要创建的 文件是否已经存在,如果不存在就创建,并写入30个换行( ) 要点: 所要读写的文件名可以是_变量_ 阅读全文
posted @ 2017-06-11 20:38 2021年的顺遂平安君 阅读(162) 评论(0) 推荐(0)
摘要: 基本用法: Dictionary 使用 不能返回一个list,需要使用 或`sorted(D)`,会返回 Dictionary 的 keys,并存入 list。 阅读全文
posted @ 2017-06-11 17:19 2021年的顺遂平安君 阅读(420) 评论(0) 推荐(0)
摘要: 基本用法: .keys.values.items >>> D = dict(a=1,b=2,c=3)>>> D{'a': 1, 'b': 2, 'c': 3}>>> D.keys>>> D.keys()dict_keys(['a', 'b', 'c']... 阅读全文
posted @ 2017-06-11 17:19 2021年的顺遂平安君 阅读(65) 评论(0) 推荐(0)
摘要: range 不是 iterator >>> R = range(3)>>> next(R)Traceback (most recent call last): File "", line 1, in TypeError: 'range' object ... 阅读全文
posted @ 2017-06-11 17:08 2021年的顺遂平安君 阅读(94) 评论(0) 推荐(0)
摘要: 1. _range_ 不是 iterator 2. _range_ 允许 multiple iterators,并且能记住每个iterator的位置。 ,`map filter`都不允许 multiple iterators。它们的 iter 就是它们本身,因为不需要专门使用 iter 函数。 阅读全文
posted @ 2017-06-11 17:08 2021年的顺遂平安君 阅读(945) 评论(0) 推荐(0)
摘要: 要注意 _zip_ 也是_one pass iterator !被遍历一次之后会自动_消失_。 zip类型支持list,next等: 阅读全文
posted @ 2017-06-11 17:00 2021年的顺遂平安君 阅读(177) 评论(0) 推荐(0)
摘要: >>> z = zip((2,3,4),(33,44,55))>>> z>>> list(z)[(2, 33), (3, 44), (4, 55)] 要注意 ** zip 也是_one-pass iterator** !被遍历一次之后会自动_消失_。 ... 阅读全文
posted @ 2017-06-11 17:00 2021年的顺遂平安君 阅读(59) 评论(0) 推荐(0)
摘要: Ctrl + a – go to the start of the command line Ctrl + e – go to the end of the command line Ctrl + k – delete from cursor to ... 阅读全文
posted @ 2017-06-11 16:25 2021年的顺遂平安君 阅读(66) 评论(0) 推荐(0)
摘要: Ctrl + a – go to the start of the command line Ctrl + e – go to the end of the command line Ctrl + k – delete from cursor to the end of the command li 阅读全文
posted @ 2017-06-11 16:25 2021年的顺遂平安君 阅读(235) 评论(0) 推荐(0)
摘要: Map map(function_to_apply, list_of_inputs) 设有以下代码: 这段代码实现的功能用map函数可以两行完成: 高级一点地,可以用一列函数作为输入: Filter _Filter_ creates a list of elements for which a fu 阅读全文
posted @ 2017-06-11 16:06 2021年的顺遂平安君 阅读(206) 评论(0) 推荐(0)
摘要: Map map(function_to_apply, list_of_inputs) 设有以下代码: >>> items = [1, 2, 3, 4, 5]>>> squared = []>>> for i in items:... s... 阅读全文
posted @ 2017-06-11 16:06 2021年的顺遂平安君 阅读(53) 评论(0) 推荐(0)
摘要: 在list comprehension中加入if条件判断: >>> lines = [line.rstrip() for line in open('script2.py') if line[0] == 'p']>>> lines['print(sys.... 阅读全文
posted @ 2017-06-11 15:56 2021年的顺遂平安君 阅读(75) 评论(0) 推荐(0)
摘要: 在list comprehension中加入if条件判断: 这段代码相当于: 在list comprehension中加入嵌套循环: 这段代码相当于: 阅读全文
posted @ 2017-06-11 15:56 2021年的顺遂平安君 阅读(491) 评论(0) 推荐(0)
摘要: list comprehension使代码变得简洁。下面这种常用的方法则显得过时: 事实上在使用list comprehension时,代码内部相当于执行下面操作: 在可以的使用的地方尽可能使用list comprehension,是一种地道的Python写法。另外,相比 循环, list comp 阅读全文
posted @ 2017-06-11 15:39 2021年的顺遂平安君 阅读(335) 评论(0) 推荐(0)
摘要: >>> L = [1,2,3,4,5]>>> L = [x+10 for x in L]>>> L[11, 12, 13, 14, 15] list comprehension使代码变得简洁。下面这种常用的方法则显得过时: >>> L = [1,2,3... 阅读全文
posted @ 2017-06-11 15:39 2021年的顺遂平安君 阅读(40) 评论(0) 推荐(0)
摘要: >>> E=enumerate('spam')>>> E>>> I=iter(E)>>> next(I)(0, 's')>>> next(I)(1, 'p')>>> list(enumerate('spam'))[(0, 's'), (1, 'p'), (... 阅读全文
posted @ 2017-06-11 15:32 2021年的顺遂平安君 阅读(67) 评论(0) 推荐(0)
摘要: 和`I__next__()`效果相同。 阅读全文
posted @ 2017-06-11 15:32 2021年的顺遂平安君 阅读(187) 评论(0) 推荐(0)
摘要: 和`iter(f)`类型相同,因为file object是它自身的iterator。 ` f.__next__() 'import sys\n' ` list则不是它自身的iterator,所以不能直接使用 ,必须使用一次 函数: 阅读全文
posted @ 2017-06-11 15:25 2021年的顺遂平安君 阅读(138) 评论(0) 推荐(0)
摘要: >>> f=open('script2.py')>>> f>>> iter(f) f和iter(f)类型相同,因为file object是它自身的iterator。 ` f.next() 'import sys\n' ` ... 阅读全文
posted @ 2017-06-11 15:25 2021年的顺遂平安君 阅读(60) 评论(0) 推荐(0)
摘要: >>> for line in open('script2.py'):... print(line.upper())... IMPORT SYSPRINT(SYS.PATH)X = 2PRINT(X ** 32) >>> for line in op... 阅读全文
posted @ 2017-06-11 15:17 2021年的顺遂平安君 阅读(32) 评论(0) 推荐(0)
摘要: 由于line strings每行本身都有 ,而 函数又会自动添加 ,所以加入参数 来suppress。 阅读全文
posted @ 2017-06-11 15:17 2021年的顺遂平安君 阅读(203) 评论(0) 推荐(0)
摘要: iterators generators 从输出结果可以看出,iterators和generators的 类型 不同。二者的作用相同,不同之处在于,generators是即用即抛的。geneartors被遍历之后就不再占用内存。 阅读全文
posted @ 2017-06-11 14:23 2021年的顺遂平安君 阅读(192) 评论(0) 推荐(0)
摘要: iterators >>> mylist=[x*x for x in range(3)]>>> mylist[0, 1, 4] generators >>> mygenerator = (x*x for x in range(3))>>> mygen... 阅读全文
posted @ 2017-06-11 14:23 2021年的顺遂平安君 阅读(52) 评论(0) 推荐(0)
摘要: def drop_first_last(grades): first,*middle,last=grades return middle 这段代码的作用是grades中的元素,第一个和最后一个分别被提取到first和last,而middle提取中间部... 阅读全文
posted @ 2017-06-11 09:55 2021年的顺遂平安君 阅读(86) 评论(0) 推荐(0)
摘要: 这段代码的作用是 中的元素,第一个和最后一个分别被提取到 和`last middle`提取中间部分的所有其他元素。 注意: 会报参数错误,正确的写法是 或`drop_first_last((1,2,3,4))`。 阅读全文
posted @ 2017-06-11 09:55 2021年的顺遂平安君 阅读(469) 评论(0) 推荐(0)