1. 如果不知道dict中是否有key的值,那么最好用dict.get(key)
如果用dict[key]这个读取会报KeyError异常,
dict.get方法主要是提供一个取不到对应key的value就返回默认值的功能,而dict[key]实际上是调用了__getitem__方法
2. 判断list是否为空:if not list 或者 len(list)==0
3. 判断空格:str[i].isspace():换行符、回车符、换页符(\f)均返回True
4. 提取字符串中的字母: filter(str.isalpha, tempStr)
filter(lambda ch: ch in ‘0123456789’, crazystring)
5. 使用bytearray(str, encoding)将字符串转换成字节数组
使用binascii.hexlify(str)将字节数组转换成用十六进制表示
使用bytearray.decode(encoding)将字节数组重新转换成字符串
6. IDLE快捷键:
Alt + P :取上一条命令
7. list去重:
list2 = list( set( list) )
或者:list2 = {}.fromkeys(list).keys()
如果去重后想保持原来的顺序,则再加上:
list2.sort( key = list.index)
8. 合并两个list: 直接 a + b
9. 一张图说明赋值、浅拷贝与深拷贝:
10. 创建默认值为0二维数组: x = [ ([0] * 10) ] * 10
11. python 3.x中,处理异常的时候应该是
except Exception as e:
而不是以前的except Exception, e:
12. 3.0版本中已经将urllib2、urlparse、和robotparser并入了urllib中,并且修改urllib模块,其中包含5个子模块,即是help()中看到的那五个名字
13. windows下安装setuptools: https://pypi.python.org/pypi/setuptools#windows-7-or-graphical-install
windows下安装ipython:http://ipython.org/install.html
安装pip:https://pip.pypa.io/en/latest/installing.html#python-os-support
安装numpy出现RuntimeError: Broken toolchain: cannot link a simple C program的错误:
http://bugs.python.org/issue16296
--- Lib/distutils/msvc9compiler.py 2013-08-03T16:17:08+04:00 +++ Lib/distutils/msvc9compiler.py 2014-03-17T18:36:50.078672+04:00 @@ -411,7 +411,11 @@ '/Z7', '/D_DEBUG'] self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO'] - if self.__version >= 7: + if self.__version >= 10: + self.ldflags_shared = [ + '/DLL', '/nologo', '/INCREMENTAL:NO', '/DEBUG', '/pdb:None', '/Manifest' + ] + elif self.__version >= 7: self.ldflags_shared_debug = [ '/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG', '/pdb:None' ]
但是后来又出现了其他的错误:numpy\core\src\npymath\npy_math_private.h(489) : error C2061: syntax error : identifier '_complex'
还是放弃用pip安装,直接在这上面找适合的版本安装:http://sourceforge.net/projects/numpy/files/?source=typ_redirect
14. Ubuntu 14.04, python2.7.6: 安装一些库的时候遇到下面的问题:
InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
解决方法:
sudo apt-get install python-dev libffi-dev libssl-dev
pip install requests[security]