# 序列: 在 python 里,元组,列表,字符串都称作序列,都可以进行切片操作. # 元组(): 一组不可修改的元素的集合 # 列表[]: 一组可以任意修改的元素的集合 # 字典{}: 一组具有索引的可变的元素的集合
#元组(): 一组不可修改的元素的集合 ########################################## #使用range()循环遍历 tuple = (("apple", "banana"),("grape", "orange"),("watermelon",),("grapefruit",)) for i in range(len(tuple)): print "tuple[%d] :" % i, "" , for j in range(len(tuple[i])): print tuple[i][j], "" , print
# 列表[]: 一组可以任意修改的元素的集合 ########################################## lst = ['a', 'b', 'c', 'd']; print lst lst.append("e"); print lst lst.insert(1, "f"); print lst lst.remove("b"); print lst print "弹出的元素:", list.pop() lst.index('c'); print lst lst.sort(); print lst # 排序 lst.reverse(); print lst # 反转
lst1=['aa', 'bb', 'cc', 'd', 'e'] lst2=['a', 'b', 'c', 'd', 'e'] lst3=[i for i in lst1 if i not in lst2] # 合并 lst4=lst1 + lst2 lst2 = lst2 + ['hello'] lst2+ = ['world']
#使用函数sorted排序,返回一个新的列表,不改变原列表的顺序 list = ["banana", "apple", "orange", "grape"] for li in sorted(set(list)): print li, "" ,
#堆栈的实现 (先进后出) list = ["apple", "banana", "grape"] list.append("orange") print list print "弹出的元素:", list.pop() print list
#队列的实现 (先进先出) list = ["apple", "banana", "grape"] list.append("orange") print list print "弹出的元素:", list.pop(0) print list
去除重复元素: >>> myList = ['a', 'c', 'b' ,'c','b', 'z', 'mpilgrim'] >>> list(set(myList)) ['a', 'mpilgrim', 'c', 'b', 'z']
# 字典{}: 一组具有索引的可变的元素的集合 ########################################## #使用字母作为索引 dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"} print dict # 排列有字典内部结构排序,不一定与上面顺序相同 print dict["a"]
#使用数字作为索引 dict = {1 : "apple", 2 : "banana", 3 : "grape", 4 : "orange"} print dict print dict[2]
#使用元组作为索引 dict = {} dict[("a","p","p","l","e")] = "apple" dict[("b","a","n","a","n","a")] = "banana" print dict print dict[("a","p","p","l","e")]
#字典的添加、删除、更新 dict = {1 : "apple", 2 : "banana", 3 : "grape", 4 : "orange"} del dict[2] dict[2] = 'zhaohang' dict[2] = 'hello' print dict.pop(4) dict.clear() print dict print dict[2]
#字典的遍历 dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"} for k in dict: print "dict[%s] =" % k,dict[k]
#字典items()的使用 dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"} print dict.keys() print dict.values() print dict.items()
#调用items()实现字典的遍历 dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"} for (k, v) in dict.items(): print "dict[%s] =" % k, v
#使用列表、字典作为字典的值 dict = {"a" : ("apple",), "bo" : {"b" : "banana", "o" : "orange"}, "g" : ["grape","grapefruit"]}
# 字典的合并更新: dict = {"a" : "apple", "b" : "banana"} dict2 = {"c" : "grape", "d" : "orange"} dict.update(dict2)
同: D = {"key1" : "value1", "key2" : "value2"} E = {"key3" : "value3", "key4" : "value4"} for k in E: D[k] = E[k]
#调用sorted()排序 dict = {"a" : "apple", "b" : "grape", "c" : "orange", "d" : "banana"} print sorted(dict.items(), key=lambda d: d[0]) #按照key排序 print sorted(dict.items(), key=lambda d: d[1]) #按照value排序
#字典的浅拷贝 dict = {"a" : "apple", "b" : "grape"} dict2 = {"c" : "orange", "d" : "banana"} dict2 = dict.copy() print dict2
# 系统模块查询 import sys print sys.modules.keys() print sys.modules.values() pritn sys.modules.items() print sys.modules["os"]
|