10 个 Python 初学者必知编码小技巧

10 个 Python 初学者必知编码小技巧(已翻译完成)

 

技巧 #1编码

字符串翻转翻译

>>> a = "codementor">>> print "Reverse is",a[::-1]翻转后的结果为 rotnemedoc

技巧 #2code

矩阵转置blog

>>> mat = [[1, 2, 3], [4, 5, 6]]>>> zip(*mat)[(1, 4), (2, 5), (3, 6)]

技巧 #3ip

a = [1,2,3]字符串

将列表中的三个元素分拆成三个变量变量

>>> a = [1, 2, 3]>>> x, y, z = a>>> x1>>> y2>>> z3

技巧 #4List

a = ["Code", "mentor", "Python", "Developer"]技巧

将字符串列表拼接成一个字符串im

>>> 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

仅用一行代码实现两个变量的交换

相关文章
相关标签/搜索