Python有一些技巧對你來講是新知識,可是還有一些技巧會讓你的代碼效率大幅提高。python
本文總結了一下本身用到的一些Python高級編程技巧,但願對你們有幫助。編程
a=[1,2,3] [x*x for x in a if x>1]
[4, 9]
a=[1,2,3] s = {x*x for x in a if x>1} s
{4, 9}
type(s)
set
a=[1,2,3] {str(x):x+1 for x in a if x>1}
{'2': 3, '3': 4}
list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(3,10))
[3, 4, 5, 6, 7, 8, 9]
list(filter(lambda x:x%3==0, range(10)))
[0, 3, 6, 9]
from collections import namedtuple Point = namedtuple('Point', ['x', 'y']) p = Point(11, 22)
p.x
11
p.y
22
from random import randint randint(1,10)
1
from collections import Counter c = Counter('aaabbbbccccccddddddeeeeee')
c
Counter({'a': 3, 'b': 4, 'c': 6, 'd': 6, 'e': 6})
c.most_common(3)
[('c', 6), ('d', 6), ('e', 6)]
from random import randint keys = 'abcdefg' d = {x:randint(90,100) for x in keys}
d
{'a': 90, 'b': 98, 'c': 100, 'd': 97, 'e': 95, 'f': 93, 'g': 92}
d.items()
dict_items([('a', 90), ('b', 98), ('c', 100), ('d', 97), ('e', 95), ('f', 93), ('g', 92)])
sorted(d.items(), key=lambda x : x[1])
[('a', 90), ('g', 92), ('f', 93), ('e', 95), ('d', 97), ('b', 98), ('c', 100)]
from random import randint, sample dd1 = {x:randint(90,100) for x in sample('abcdefghij', 5)} dd2 = {x:randint(90,100) for x in sample('abcdefghij', 5)} dd3 = {x:randint(90,100) for x in sample('abcdefghij', 5)}
dd1
{'h': 99, 'f': 94, 'c': 91, 'i': 99, 'b': 95}
dd2
{'b': 95, 'g': 91, 'h': 98, 'f': 100, 'd': 92}
dd3
{'h': 95, 'g': 99, 'a': 100, 'd': 96, 'i': 92}
mp = map(dict.keys, (dd1, dd2, dd3))
list(mp)
[dict_keys(['h', 'f', 'c', 'i', 'b']), dict_keys(['b', 'g', 'h', 'f', 'd']), dict_keys(['h', 'g', 'a', 'd', 'i'])]
from functools import reduce reduce(lambda x,y: x&y, mp)
{'h'}
from collections import OrderedDict d = OrderedDict() d['x'] = 1 d['y'] = 2 d['a'] = 2 d['b'] = 2 d
OrderedDict([('x', 1), ('y', 2), ('a', 2), ('b', 2)])
from collections import deque d = deque([], 3) d.append(1) d.append(2) d.append(3) d.append(4) d
deque([2, 3, 4])
names = [x for x in 'abcdefg'] ages = [x for x in range(21, 28)] scores = [randint(90,100) for x in range(7)]
for name,age,score in zip(names, ages, scores): print(name,age,score)
a 21 95 b 22 99 c 23 94 d 24 95 e 25 100 f 26 96 g 27 95
lista = (randint(1,10) for x in range(10)) listb = [randint(90,100) for x in range(20)]
from itertools import chain
for x in chain(lista, listb): print(x, end=',')
5,10,3,1,8,7,6,5,6,8,92,95,91,98,95,93,96,95,94,98,92,90,91,91,99,96,90,100,94,99,
s = 'a,b;c/d' import re re.sub(r'[,;/]', '-', s)
'a-b-c-d'
s = 'things happend in 2017-08-09, it is a sunddy' re.sub(r'(\d{4})-(\d{2})-(\d{2})', r'\2-\1-\3', s)
'things happend in 08-2017-09, it is a sunddy'
print('\t'.join([str(x) for x in ['a','b',33,4.0,'e']]))
a b 33 4.0 e
from threading import Thread def func(x): print(x, x*x*x) ts = [] for x in range(10): t = Thread(target=func, args=(x,)) t.start() ts.append(t) for t in ts: t.join() print('main thread over')
0 0 1 1 2 8 3 27 4 64 5 125 6 216 7 343 8 512 9 729 main thread over
如下的輸出錯亂,是正常的,由於多個線程同時print就錯亂了多線程
from threading import Thread class MyThread(Thread): def __init__(self, x): Thread.__init__(self) self.x = x def run(self): print(self.x, self.x*self.x*self.x) ts = [] for x in range(10): t = MyThread(x) t.start() ts.append(t) for t in ts: t.join() print('main thread over')
0 0 1 1 2 3 27 8 45 64 6 216 125 7 343 8 512 9 729 main thread over
關注我,學習更多Python基礎、數據分析、大數據、推薦系統知識;app