Archive note: Recovered from the Wayback Machine. 28 historical images referenced by the original page were not captured; their positions are marked below.
-
文件处理
-
读取网络上的文件,使用Python中的urlib模块中的urlopen函数。(Python3.X和Python2.X的urlib模块改变比较大,具体变化见相关博文。)
比如要打开这个文件:
[Historical image unavailable in the recovered snapshot:
032714_1642_1.png].编写的代码如下:(在Python2.7.6编译通过)
[Historical image unavailable in the recovered snapshot:
032714_1642_2.png].结果如下:
[Historical image unavailable in the recovered snapshot:
032714_1642_3.png]. -
集合是一个由唯一元素组成的非排序集合体(collection)。是内置类型。可以直接用工厂函数定义一个集合:set((2,3,5))这样的代码,这里的初始值是在单个的参数中定义的(即元组),当然也可以使用列表(set([2,3,5])),但无论如何也不能将初始值一个一个的穿进去,比如set(2,3.5).
关于集合及其方法的详细介绍:
集合分类:可变集合、不可变集合
可变集合(set):可添加和删除元素,非可哈希的,不能用作字典的键,也不能做其他集合的元素
不可变集合(frozen set):与上面恰恰相反
集合操作符与关系符号:
[Historical image unavailable in the recovered snapshot:
032714_1642_4.png].*集合的相关操作*
1、创建集合
由于集合没有自己的语法格式,只能通过集合的工厂方法set()和frozenset()创建
[Historical image unavailable in the recovered snapshot:
032714_1642_5.png].*2、访问集合*
由于集合本身是无序的,所以不能为集合创建索引或切片操作,只能循环遍历或使用in、not in来访问或判断集合元素。
[Historical image unavailable in the recovered snapshot:
032714_1642_6.png].[Historical image unavailable in the recovered snapshot:
032714_1642_7.png].*3、更新集合*
可使用以下内建方法来更新:
s.add() s.update() s.remove()
注意只有可变集合才能更新:
[Historical image unavailable in the recovered snapshot:
032714_1642_8.png].del:删除集合本身
*四、集合类型操作符*
*1、in ,not in 2、集合等价与不等价(==, !=) 3、子集、超集(见上表)*
*4、联合(|)*
联合(union)操作与集合的OR操作其实等价的,联合符号有个等价的方法,union()。
[Historical image unavailable in the recovered snapshot:
032714_1642_9.png].*5、交集(&)*
与集合AND等价,交集符号的等价方法是intersection()
*6、查补(-)* 等价方法是difference()
*7、对称差分(^)*
对称差分是集合的XOR(‘异或’),取得的元素属于s1,s2但不同时属于s1和s2.其等价方法symmetric_difference()
*注意:集合之间and,or*
[Historical image unavailable in the recovered snapshot:
032714_1642_10.png].*****五、集合、列表、元组、字符串之间转换*
[Historical image unavailable in the recovered snapshot:
032714_1642_11.png].
应用:
”’最简单的去重方式”’
lis = [1,2,3,4,1,2,3,4]
print list(set(lis)) #[1, 2, 3, 4]
*六、关于集合的内建函数、内建方法*
*1、len():返回集合元素个数*
*2、set()、frozenset()工厂函数*
*3、所有集合方法:*
[Historical image unavailable in the recovered snapshot:
032714_1642_12.png].*****4、仅适合可变集合*
[Historical image unavailable in the recovered snapshot:
032714_1642_13.png].****[Historical image unavailable in the recovered snapshot:
032714_1642_14.png].*****其他:*
*只有add, remove, clear方法是修改当前的集合而不会创建一个新的集合。*
*用于存储集合的数据结构成为散列码(hash code)。每当有元素加入集合的时候,Python就会计算该元素的散列码。散列码是一个整数,拥有等值的元素的散列码肯定是相同的。*
[Historical image unavailable in the recovered snapshot:
032714_1642_15.png].所以为了判断某个元是否在某个集合中,Python只需要计算该值的哈希吗并检查散列表中的相应位置即可。
某个元素放入散列表之后,其散列码就不会再改变了。
-
Python的一种内置数据结构——字典(Dictionary),也称为映射(map),由键值对构成的非排序的可变集合体.
*1.创建*
*方法一:*
[Historical image unavailable in the recovered snapshot:
032714_1642_16.png].*方法二:*
从Python 2.2 版本起,可以使用一个工厂方法,传入一个元素是列表的元组作为参数
[Historical image unavailable in the recovered snapshot:
032714_1642_17.png].*方法三:*
从Python 2.3 版本起, 可以用一个很方便的内建方法fromkeys() 来创建一个”默认”字典, 字典中元素具有相同的值 (如果没有给出, 默认为None,这个有点像我框架的oneObject方法):
[Historical image unavailable in the recovered snapshot:
032714_1642_18.png].*2.访问字典中的值*
想遍历一个字典(一般用键), 你只需要循环查看它的键, 像这样:
[Historical image unavailable in the recovered snapshot:
032714_1642_19.png].从Python 2.2 开始,可以直接在 for 循环里遍历字典
[Historical image unavailable in the recovered snapshot:
032714_1642_20.png].[Historical image unavailable in the recovered snapshot:
032714_1642_21.png].[Historical image unavailable in the recovered snapshot:
032714_1642_22.png].一个字典中混用数字和字符串的例子:
[Historical image unavailable in the recovered snapshot:
032714_1642_23.png].*3.更新字典*
采取覆盖更新
上例中 dict2[‘name’]=’earth’;
更新 dict2[‘name’]=’abc’;
*4.删除字典元素和字典*
del dict2[‘name’] # 删除键为”name”的条目
dict2.clear() # 删除dict2 中所有的条目
del dict2 # 删除整个dict2 字典
dict2.pop(‘name’) # 删除并返回键为”name”的条目
[Historical image unavailable in the recovered snapshot:
032714_1642_24.png].update()方法可以用来将一个字典的内容添加到另外一个字典中
[Historical image unavailable in the recovered snapshot:
032714_1642_25.png].映射类型相关的函数
[Historical image unavailable in the recovered snapshot:
032714_1642_26.png].*字典内建方法*
方法名字 操作 dict.clear() 删除字典中所有元素 dict.copy() 返回字典(浅复制)的一个副本 dict.fromkeysc(seq,val=None) 创建并返回一个新字典,以seq 中的元素做该字典的键,val 做该字典中所有键对应的初始值(如果不提供此值,则默认为None) dict.get(key,default=None) 对字典dict 中的键key,返回它对应的值value,如果字典中不存在此键,则返回default 的值(注意,参数default 的默认值为None) dict.has_key(key) 如果键(key)在字典中存在,返回True,否则返回False. 在Python2.2版本引入in 和not in 后,此方法几乎已废弃不用了,但仍提供一个 可工作的接口。 dict.items() 返回一个包含字典中(键, 值)对元组的列表 dict.keys() 返回一个包含字典中键的列表 dict.values() 返回一个包含字典中所有值的列表 dict.iter() 方法iteritems(), iterkeys(), itervalues()与它们对应的非迭代方法一样,不同的是它们返回一个迭代子,而不是一个列表。 dict.pop(key[, default]) 和方法get()相似,如果字典中key 键存在,删除并返回dict[key],如果key 键不存在,且没有给出default 的值,引发KeyError 异常。 dict.setdefault(key,default=None) 和方法set()相似,如果字典中不存在key 键,由dict[key]=default 为它赋值。 dict.setdefault(key,default=None) 和方法set()相似,如果字典中不存在key 键,由dict[key]=default 为它赋值。 其他:items方法常用语对字典的键值进行迭代:
-
[Historical image unavailable in the recovered snapshot: 032714_1642_27.png].
[Historical image unavailable in the recovered snapshot: 032714_1642_28.png].