python有一些技巧對你來說是新知識,但是還有一些技巧會讓你的**效率大幅提公升。
a=[1,2,3]
[x*x for x in a if x>1]
[4, 9]
a=[1,2,3]
s =
s
type(s)
set
a=[1,2,3]
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()
c.most_common(3)
[('c', 6), ('d', 6), ('e', 6)]
from random import randint
keys = 'abcdefg'
d =
d
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 =
dd2 =
dd3 =
dd1
dd2
dd3
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)
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
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'
re.sub(r'(\d)-(\d)-(\d)', r'\2-\1-\3', s)
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()
for t in ts:
t.join()
print('main thread over')
0 0
1 12 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()
for t in ts:
t.join()
print('main thread over')
0 0
1 12 3 27
845 64
6 216
1257 343
8 512
9 729
main thread over
python高階程式設計技巧
個人部落格點這裡 方法1 通過迭代來進行判斷篩選 解決方案 函式式程式設計 解決方案 使用collections.counter物件 將序列傳入counter的構造器,得到counter物件是元素頻度的字典 counter.most common n 方法得到頻度最高的n個元素的列表 解決方案 使用...
Python高階程式設計特性和技巧
昨今兩天把python高階程式設計過了遍,程式語言通用的部分都比較熟悉了,就跳過了。總結下 1 列表推導 def treatment pos,element return d,s pos,element 列表推導 print i for i in range 0,10,1 if i 2 0 seq ...
Moss 幾個程式設計技巧
1 提公升許可權執行的 spsecurity.runwithelevatedprivileges delegate 需要提公升許可權執行的 應用場景 當前使用者可能沒有許可權執行的操作,但系統需要執行這段 這樣如果不提公升 許可權的話 系統將會報出 拒絕訪問錯誤 用上述 可以將其所轄範圍內的 許可權...