一、字符串翻转
>>> a = "codementor">>> print "Reverse is",a[::-1]翻转后的结果为 rotnemedoc
二、矩阵转置
>>> mat = [[1, 2, 3], [4, 5, 6]]>>> zip(*mat)[(1, 4), (2, 5), (3, 6)]
三、拆分变量
a = [1,2,3] 将列表中的三个元素分拆成三个变量
>>> a = [1, 2, 3]>>> x, y, z = a>>> x1>>> y2>>> z3
四、字符串拼接
a = [“Code”, “mentor”, “Python”, “Developer”]
将字符串列表拼接成一个字符串
>>> print " ".join(a)Code mentor Python Developer
五、规则的字符串拼接
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
六、两个变量交换
>>> a=7>>> b=5>>> b, a =a, b>>> a5>>> b7
七、不使用循环,输出 “codecodecodecode mentormentormentormentormentor”
>>> print "code"*4+' '+"mentor"*5
八、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]
九、检查一个单词和另一个单词是否只是字母顺序不同
def is_anagram(word1, word2): """检查一个单词和另一个单词是否只是字母顺序不同 word1: string word2: string returns: boolean """
from collections import Counterdef is_anagram(str1, str2): return Counter(str1) == Counter(str2)>>> is_anagram('abcd','dbca')True>>> is_anagram('abcd','dbaa')False
十、从字符串输入中获取值 对于输入数据 1 2 3 4 我们希望得到列表 [1, 2, 3, 4]
>>> result = map(lambda x:int(x) ,raw_input().split())1 2 3 4>>> result[1, 2, 3, 4]