反转字符串
>>> a = "codementor" >>> print "Reverse is",a[::-1]
矩阵转置
>>> mat = [[1, 2, 3], [4, 5, 6]] >>> zip(*mat) [(1, 4), (2, 5), (3, 6)]
列表转字符串
a = ["Code", "mentor", "Python", "Developer"] >>> print " ".join(a)
列表拉链
>>> for x, y in zip(list1,list2): ... print x, y ... a p b q c r d s
多重嵌套列表转单一列表
>>> import itertools >>> list(itertools.chain.from_iterable(a)) [1, 2, 3, 4, 5, 6]
检查两个单词是否是相互颠倒的
from collections import Counter def is_anagram(str1, str2): return Counter(str1) == Counter(str2) >>> is_anagram('abcd','dbca') True >>> is_anagram('abcd','dbaa') False
一行代码获取用户输入并放到列表中
>>> result = map(lambda x:int(x) ,raw_input().split()) 1 2 3 4 >>> result [1, 2, 3, 4]
文章来源: python语法糖