2013年4月18日
摘要: 直接上代码:片段1:>>> def func(x): print(','.join(str(i) for i in range(1,x+1))) >>> func(5)1,2,3,4,5>>> func(10)1,2,3,4,5,6,7,8,9,10片段2:>>> def func(x): for i in range(1,x+1): print(','.join(str([j,'password'][j==i]) for j in range(1,x+1))) >&g 阅读全文
posted @ 2013-04-18 15:08 101010 阅读(226) 评论(0) 推荐(0) 编辑
摘要: 2个list:a =[1,2,3,4,5]b =['a','b','c','d','e']形成一个字典:c = {}构造规则:a中的元素用作key,b中的元素用作value,字典c={1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}。>>> a = [1,2,3,4,5]>>> b = ['a','b','c','d 阅读全文
posted @ 2013-04-18 11:28 101010 阅读(183) 评论(0) 推荐(0) 编辑
摘要: 初步想法:>>> a = [1,2,4,1,2,3,5,2,6,8,7,8,8]>>> a[1, 2, 4, 1, 2, 3, 5, 2, 6, 8, 7, 8, 8]>>> for i in a: print(i,a.count(i)) 1 22 34 11 22 33 15 12 36 18 37 18 38 3但是,结果出现了重复的值。有以下三种解决方案:1.使用集合Set>>> for i in set(a): print(i,a.count(i)) 1 22 33 14 15 16 17 18 32.使用字典Di 阅读全文
posted @ 2013-04-18 11:18 101010 阅读(11232) 评论(0) 推荐(0) 编辑
摘要: >>> l = [1,2,3,1,2,4,5]1.列表推导式:>>> [x for x in l if x > min(l)][2, 3, 2, 4, 5]2.filter()函数:>>> list(filter(lambda x: x>min(l), l))[2, 3, 2, 4, 5] 阅读全文
posted @ 2013-04-18 10:33 101010 阅读(863) 评论(0) 推荐(0) 编辑
  2013年4月17日
摘要: shelve模块的详细信息见Python库参考:http://docs.python.org/3/library/shelve.html直接看代码: 1 # database.py 2 import sys, shelve 3 4 def store_person(db): 5 ''' 6 Query user for data and store it in the shelf object 7 ''' 8 pid = input('Enter unique ID number:') 9 person = {}10 person 阅读全文
posted @ 2013-04-17 23:02 101010 阅读(264) 评论(0) 推荐(0) 编辑
摘要: 要求:1.需要备份的文件和目录由一个列表指定。2.备份应该保存在主备份目录中。3.文件备份成一个zip文件。4.zip存档的名称是当前的日期和时间。初始实现版本: 1 # Filename:backup_v1.py 2 3 import os 4 import time 5 6 # 1.需要备份的源文件列表 7 # 问题:暂时无法解决源文件名中带有空格的情况(如C:\test 1目录) 8 # Linux 9 #source = ['/root/test1','/root/test2']10 # Windows11 #source = ['C:\\tes 阅读全文
posted @ 2013-04-17 00:18 101010 阅读(794) 评论(0) 推荐(0) 编辑
  2013年4月16日
摘要: Py***_Type这些都是结构体PyTypeObject的实例。以PyLong_Type为例来说明:(1)变量声明在Include/longobject.h中:PyAPI_DATA(PyTypeObject) PyLong_Type;注意:PyAPI_DATA是一个宏,定义在Include/pyport.h,用来隐藏动态库平台的差异,片段如下:#define PyAPI_FUNC(RTYPE) __declspec(dllexport) RTYPE#define PyAPI_DATA(RTYPE) extern __declspec(dllexport) RTYPE(2)变量定义在Objec 阅读全文
posted @ 2013-04-16 21:34 101010 阅读(1170) 评论(1) 推荐(0) 编辑
摘要: Include/object.h:摘录 1 /* Object and type object interface */ 2 3 /* 4 Objects are structures allocated on the heap. Special rules apply to 5 the use of objects to ensure they are properly garbage-collected. 6 Objects are never allocated statically or on the stack; they must be 7 accessed through sp. 阅读全文
posted @ 2013-04-16 18:33 101010 阅读(2886) 评论(0) 推荐(0) 编辑
摘要: 地址:http://docs.python.org/3.3/c-api/structures.htmlThere are a large number of structures which are used in the definition of object types for Python. This section describes these structures and how they are used.All Python objects ultimately share a small number of fields at the beginning of the ob 阅读全文
posted @ 2013-04-16 17:40 101010 阅读(362) 评论(0) 推荐(0) 编辑
摘要: 阅读全文
posted @ 2013-04-16 14:21 101010 阅读(967) 评论(0) 推荐(0) 编辑