1 当我们使用循环是 for i in range(n): 对内存消耗很大
可以使用for i in xrange(n) 这个对内存消耗很小,因为它返回式一个迭代对象
def fab(max):
n, a, b = 0, 0, 1
while n < max:
yield b
# print b
a, b = b, a + b
n = n + 1
函数返回的是一个迭代器
- # -*- coding: utf-8 -*- 对中文的支持
- os.path.exists(dir_name) 这个函数是判断是否存在该目录,在windows下,如果dir_name 有中文,可能函数无法正常识别,因为编码的原因,你可以通过dir_name.encode('gbk') 来进行编码,这样应该就能执行成功。在linux下可以用dir_name.encode('utf-8')
-
ConfigParser模块教程配置文件的解析和写入操作
4. 配置cmd终端的编码方式
import sys
reload(sys)
sys.setfaultencoding('utf-8')
对于函数返回值比较多时,可以返回一个字典一样的字典
def get_numbers():
return 1, 2, 3
a, b, c = get_numbers()
class Person(object):
def __init__(self, name, gender, age, weight):
self.name = name
self.gender = gender
self.age = age
self.weight = weight
or
import collections
Person = collections.namedtuple('Person', 'name gender age weight')
def get_person_info():
return Person('jjp', 'MALE', 30, 130)
person = get_person_info()
yield的使用:
#返回n以内的奇数
def odds(n):
for i in xrange(1, n + 1):
if i % 2 == 1:
yield i
for i in odds(1000):
print i
安装Pip:
wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-1.4.1.tar.gz
tar zxvf pip-1.4.1.tar.gz
cd pip-1.4.1
python setup.py install
os.system("taskkill -F -MI {0}" % (application_name))
h5py: 对于大文件的读取问题,我们可以通过h5py 这个包,达到动态读取,自动进行内存替换策略