最新初学者应该了解的10个Python技巧
以下是三零网为大家整理的最新初学者应该了解的10个Python技巧的文章,希望大家能够喜欢!
今天要给大家分享一些对于初学者十分有帮助的Python技巧,以提高自己的编程能力。所以,欢迎大家收藏哟!!!
技巧1
字符串倒置
>>> a = "codementor" >>> print "Reverse is",a[::-1] |
倒置之后的结果是“rotnemedoc”。
技巧2
转置矩阵(transposing a matrix)(译者注:把矩阵A的行换成相应的列,得到的新矩阵称为A的转置矩阵)
>>> mat = [[1, 2, 3], [4, 5, 6]] >>> zip(*mat) [(1, 4), (2, 5), (3, 6)] |
技巧3
a = [1,2,3]
将上述列表中的三个值分别存储在3个新变量中。
>>> a = [1, 2, 3] >>> x, y, z = a >>> x 1 >>> y 2 >>> z 3 |
技巧4
a = ["Code", "mentor", "Python", "Developer"]
利用上述列表中的所有元素,创建一个字符串。
>>> print " ".join(a) Code mentor Python Developer |
技巧5
list1 = ['a', 'b', 'c', 'd']
list2 = ['p', 'q', 'r', 's']
编写可以打印出下面结果的代码
ap bq cr ds >>> for x, y in zip(list1,list2): |
技巧6
一行代码交换两个变量的值
>>> a=7 >>> b=5 >>> b, a =a, b >>> a 5 >>> b 7 |
技巧7
不使用循环打印出“codecodecodecode mentormentormentormentormentor”
>>> print "code"*4+' '+"mentor"*5 codecodecodecode mentormentormentormentormentor |
技巧8
a = [[1, 2], [3, 4], [5, 6]]
不使用任何循环,将上面的嵌套列表转换成单一列表(即组成元素不是列表)
输出结果应为: [1, 2, 3, 4, 5, 6]
>>> import itertools >>> list(itertools.chain.from_iterable(a)) [1, 2, 3, 4, 5, 6] |
技巧9
判断两个单词是否是回文单词(anagram)?
def is_anagram(word1, word2): """Checks whether the words are anagrams. word1: string word2: string returns: boolean """ |
完成上面的函数
from collections import Counter def is_anagram(str1, str2): return Counter(str1) == Counter(str2) >>> is_anagram('abcd','dbca') True >>> is_anagram('abcd','dbaa') False |
联系客服