python
文章平均质量分 65
ayw215
这个作者很懒,什么都没留下…
展开
-
python生成某区间内不重复的随机数
需求:生成3个不同的随机数,随机数大于100001小于100200import randomrandomdata = range(0,97281)randomlist = random.sample(randomdata,500)print(len(randomlist))print len(randomlist)打印大致如下:[1,2,........]原创 2011-05-10 11:23:00 · 7218 阅读 · 1 评论 -
从文件里随机抽取XX行
<br />#! /usr/bin/env python#coding=utf-8import codecsimport randomrandomdata = range(0,97281)randomlist = random.sample(randomdata,500)print(len(randomlist))FileRead = codecs.open('result.txt','r','utf-8')FileWrite = codecs.open('result.原创 2011-05-10 11:57:00 · 2023 阅读 · 0 评论 -
python读写ini文件的模块
<br />使用dict4ini<br />http://code.google.com/p/dict4ini/<br /> <br />ExamplesExample 1 Create a ini file import dict4ini<br /><br /> x = dict4ini.DictIni('test.ini')<br /> x.common.name ='limodou'<br /> x.common.bool=1<br /> x.common.list =[原创 2011-05-26 14:57:00 · 4243 阅读 · 0 评论 -
转:Python 正则式学习笔记
<br /><br />1. Python正则式的基本用法<br />1.1基本规则<br />1.2重复<br />1.2.1最小匹配与精确匹配<br />1.3前向界定与后向界定<br />1.4组的基本知识<br />2. re模块的基本函数<br />2.1使用compile加速<br />2.2 match和search<br />2.3 finditer<br />2.4 字符串的修改与替换<br />3. 更深入的了解re的组与对象<br />3.1编译后的Pattern对象<br />原创 2011-05-25 18:17:00 · 962 阅读 · 0 评论 -
bisect模块二分插入
<br /><br />比如说:我想插入一个事件(函数)到一个队列中去就可以用这个函数哦!<br /><br />示例:<br />import bisect<br />list = [10,20,30]<br />bisect.insort(list,25)<br />bisect.insort(list,15)<br />print list<br />输出:[10, 15, 20, 25, 30]<br />哦!一切都明白了。<br />说明白一点:这个模块的作用可以往一个序列对象中插入数据。不影响其转载 2011-05-27 16:11:00 · 988 阅读 · 0 评论 -
集合的交集并集差集
<br /><br />x = ["a","b","c"]y = ["a","d","g"]<br />要求返回两者中没有重复的数据,即: <br />["b","d","c","g"]<br />此问题的常规解答为: <br />result = []for item in x: if item not in y: result.append( item ) for item in y: if item not in x: result.append( item )原创 2011-05-27 16:17:00 · 1079 阅读 · 0 评论 -
set
Python 从 2.4 版引入了一种集合类型 --- set 。Python 2.4 also introduced a new collection type, the set—an unordered collection ofunique and immutable objects that supports operations corresponding to mathemat转载 2011-06-09 13:48:00 · 825 阅读 · 0 评论 -
python的加密模块
使用:https://www.dlitz.net/software/pycrypto/pycrypto 模块使用AES的一个范例:#! /usr/bin/env python#coding=utf-8from Crypto.Cipher import AESimport structimport hashlibimport randomdef GenerageKe原创 2011-12-02 19:33:03 · 9376 阅读 · 0 评论 -
python的c/c++扩展内存泄露bug fix
近日需要使用到某同学使用c++封装的python扩展模块,搭配好环境,写上代码,跑起来,top一下,发现内存一直在涨。。。于是找到源代码,大致瞅了下,发现貌似是一些引用计数的问题py的封装最不好控制的就是引用计数了,很多时候不知道啥时候Py_INCREF,啥时候Py_DECREF,这也是为什么会存在 boost.python,pycxx的原因,诸如boost.python,pycxx的模块原创 2011-12-28 14:09:08 · 5647 阅读 · 0 评论