python之列表和元组
列表 mutable
操作符:
标准类型操作符都适用
序列类型操作符:
slice 、in 略
+ :
>>> lis1 = ['hello'] >>> lis2= ['world'] >>> lis1 + lis2 ['hello', 'world']
*:
>>> lis = ['hello', 'world'] >>> lis * 2 ['hello', 'world', 'hello', 'world']
内建函数
标准类型内建函数都适用
序列内建函数介绍几个:
sum:
>>> sum([3, 4]) 7 >>> sum([3, 4], 5) 12
max and min:
>>> max([3, 4]) 4 >>> min([3, 4]) 3
zip:
>>> x = [1, 2, 3] >>> y = [4, 5, 6] >>> zipped = zip(x, y) >>> zipped [(1, 4), (2, 5), (3, 6)] >>> x2, y2 = zip(*zipped) >>> x == list(x2) and y == list(y2) True
enumerate:
>>> for i, j in enumerate(['hello', 'world']): print i, j 0 hello 1 world
list and tuple:
>>> tuple([3, 4]) (3, 4) >>> list((3, 4)) [3, 4]
列表内建函数
list.append(x) list.extend(lis) list.insert(i, x) list.remove(x) list.pop(i) #会同时返回移除的值,如果没有设置i,则返回最后一个值 del list[i] del list[i:j] list.index(x) #返回x第一次出现的位置 list.count(x) #返回x出现的次数 list.sort() #sorted() 见 sort高级用法 list.reverse() #list快速创建: lis = [x**2 for x in range(10)] [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] ----------------------------------- [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] [[row[i] for row in matrix] for i in range(4)] ------------------------------------ [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
sort高级用法
both list.sort() and sorted() added a key parameter to specify a FUNCTION to be called on each list element prior to making comparisons.
而且通过通过设置reverse可以颠倒排序结果
#注意key后的是function,不是function call的值, 所以常用lambda表达式 sorted("This is a test string".split(), key=str.lower) ---------------------------------------- ['This', 'test', 'string', 'is', 'a'] #A common pattern is to sort complex objects using some of the object’s indices as keys student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10),] sorted(student_tuples, key=lambda student: student[2]) ---------------------------------------- [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)] #The same technique works for objects with named attributes class Student(object): def __init__(self, name, grade, age): self.name = name self.grade = grade self.age = age def __repr__(self): return repr((self.name, self.grade, self.age)) student_objects = [Student('John', 'A', 15), Student('Jane', 'B', 12), Student('Dave', 'B', 10),] sorted(student_objects, key=lambda student: student.age) ------------------------------------------ [('Dave', 'B', 10), ('Jane', 'B', 12), ('John', 'A', 15)] #上面都可以一些python内置的函数 from operator import itemgetter, attrgetter sorted(student_tuples, key=itemgetter(2)) sorted(student_objects, key=attrgetter('age')) sorted(student_tuples, key=itemgetter(1,2)) sorted(student_objects, key=attrgetter('grade', 'age'))
浅拷贝与深拷贝
浅拷贝只拷贝壳,并没有拷贝内容
import copy class Foo(object): def __init__(self, val): self.val = val def __repr__(self): return str(self.val) foo = Foo(1) a = ['foo', foo] b = a[:] c = list(a) d = copy.copy(a) e = copy.deepcopy(a) # edit orignal list and instance a.append('baz') foo.val = 5 print "original: %r\n slice: %r\n list(): %r\n copy: %r\n deepcopy: %r" \ % (a, b, c, d, e) ----------------------------------------- original: ['foo', 5, 'baz'] slice: ['foo', 5] list(): ['foo', 5] copy: ['foo', 5] deepcopy: ['foo', 1]
元组 immutable
因为列表mutable,元组immutable, 所有元组可作字典key,而列表不行。
2015-05-25