# 10个Python技巧

1

``````>>> a =  "codementor"
>>> print "Reverse is",a[::-1]``````

``````>>> 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
>>> x
1
>>> y
2
>>> z
3``````

``a = ["Code", "mentor", "Python", "Developer"]``

``````>>> print " ".join(a)
Code mentor Python Developer``````

``````list1 = ['a', 'b', 'c', 'd']
list2 = ['p', 'q', 'r', 's']``````

ap
bq
cr
ds

``````>>> for x, y in zip(list1,list2):
...    print x, y
...
a p
b q
c r
d s``````

``````>>> a=7
>>> b=5
>>> b, a =a, b
>>> a
5
>>> b
7``````

``````>>> print "code"*4+' '+"mentor"*5
codecodecodecode mentormentormentormentormentor``````

``a = [[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):
"""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``````

``````>>> result = map(lambda x:int(x) ,raw_input().split())
1 2 3 4
>>> result
[1, 2, 3, 4]``````
1 条评论

python cookbook里面有