技巧 #1
字符串翻转
>>> a = "codementor">>> print "Reverse is",a[::-1]翻转后的结果为 rotnemedoc
技巧 #2
矩阵转置
>>> mat = [[1, 2, 3], [4, 5, 6]]>>> zip(*mat)[(1, 4), (2, 5), (3, 6)]
技巧 #3
a = [1,2,3]
将列表中的三个元素分拆成三个变量
>>> a = [1, 2, 3]>>> x, y, z = a>>> x1>>> y2>>> z3
技巧 #4
a = ["Code", "mentor", "Python", "Developer"]
将字符串列表拼接成一个字符串
>>> print " ".join(a)Code mentor Python Developer
技巧 #5
List 1 = ['a', 'b', 'c', 'd']
List 2 = ['p', 'q', 'r', 's']
编写 Python 代码,实现下面的输出
- ap
- bq
- cr
- ds
>>> for x, y in zip(list1,list2):... print x, y...a pb qc rd s
技巧 #6
仅用一行代码实现两个变量的交换