有時候我反問我自己,怎麼不知道在Python 3中用更簡單的方式做“這樣”的事,當我尋求答案時,隨著時間的推移,我當然發現更簡潔、有效並且bug更少的程式碼。總的來說(不僅僅是這篇文章),“那些”事情總共數量是超過我想象的,但這裡是第一批不明顯的特性,後來我尋求到了更有效的/簡單的/可維護的程式碼。
字典
字典中的keys()和items()
你能在字典的keys和items中做很多有意思的操作,它們類似於集合(set):
1 2 3 4 5 6 7 8 |
aa = {‘mike’: ‘male’, ‘kathy’: ‘female’, ‘steve’: ‘male’, ‘hillary’: ‘female’} bb = {‘mike’: ‘male’, ‘ben’: ‘male’, ‘hillary’: ‘female’} aa.keys() & bb.keys() # {‘mike’, ‘hillary’} # these are set-like aa.keys() - bb.keys() # {‘kathy’, ‘steve’} # If you want to get the common key-value pairs in the two dictionaries aa.items() & bb.items() # {(‘mike’, ‘male’), (‘hillary’, ‘female’)} |
太簡潔啦!
在字典中校驗一個key的存在
下面這段程式碼你寫了多少遍了?
1 2 3 4 5 |
dictionary = {} for k, v in ls: if not k in dictionary: dictionary[k] = [] dictionary[k].append(v) |
這段程式碼其實沒有那麼糟糕,但是為什麼你一直都需要用if語句呢?
1 2 3 4 |
from collections import defaultdict dictionary = defaultdict(list) # defaults to list for k, v in ls: dictionary[k].append(v) |
這樣就更清晰了,沒有一個多餘而模糊的if語句。
用另一個字典來更新一個字典
1 2 3 4 5 6 7 |
from itertools import chain a = {‘x’: 1, ‘y’:2, ‘z’:3} b = {‘y’: 5, ‘s’: 10, ‘x’: 3, ‘z’: 6} # Update a with b c = dict(chain(a.items(), b.items())) c # {‘y’: 5, ‘s’: 10, ‘x’: 3, ‘z’: 6} |
這樣看起來還不錯,但是不夠簡明。看看我們是否能做得更好:
1 2 |
c = a.copy() c.update(b) |
更清晰而且更有可讀性了!
從一個字典獲得最大值
如果你想獲取一個字典中的最大值,可能會像這樣直接:
1 2 3 |
aa = {k: sum(range(k)) for k in range(10)} aa # {0: 0, 1: 0, 2: 1, 3: 3, 4: 6, 5: 10, 6: 15, 7: 21, 8: 28, 9: 36} max(aa.values()) #36 |
這麼做是有效的,但是如果你需要key,那麼你就需要在value的基礎上再找到key。然而,我們可以用過zip來讓展現更扁平化,並返回一個如下這樣的key-value形式:
1 2 |
max(zip(aa.values(), aa.keys())) # (36, 9) => value, key pair |
同樣地,如果你想從最大到最小地去遍歷一個字典,你可以這麼幹:
1 2 |
sorted(zip(aa.values(), aa.keys()), reverse=True) # [(36, 9), (28, 8), (21, 7), (15, 6), (10, 5), (6, 4), (3, 3), (1, 2), (0, 1), (0, 0)] |
在一個list中開啟任意數量的items
我們可以運用*的魔法,獲取任意的items放到list中:
1 2 3 4 5 6 7 |
def compute_average_salary(person_salary): person, *salary = person_salary return person, (sum(salary) / float(len(salary))) person, average_salary = compute_average_salary([“mike”, 40000, 50000, 60000]) person # ‘mike’ average_salary # 50000.0 |
這不是那麼有趣,但是如果我告訴你也可以像下面這樣呢:
1 2 3 4 5 6 |
def compute_average_salary(person_salary_age): person, *salary, age = person_salary_age return person, (sum(salary) / float(len(salary))), age person, average_salary, age = compute_average_salary([“mike”, 40000, 50000, 60000, 42]) age # 42 |
看起來很簡潔嘛!
當你想到有一個字串型別的key和一個list的value的字典,而不是遍歷一個字典,然後順序地處理value,你可以使用一個更扁平的展現(list中套list),像下面這樣:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Instead of doing this for k, v in dictionary.items(): process(v) # we are separating head and the rest, and process the values # as a list similar to the above. head becomes the key value for head, *rest in ls: process(rest) # if not very clear, consider the following example aa = {k: list(range(k)) for k in range(5)} # range returns an iterator aa # {0: [], 1: [0], 2: [0, 1], 3: [0, 1, 2], 4: [0, 1, 2, 3]} for k, v in aa.items(): sum(v) #0 #0 #1 #3 #6 # Instead aa = [[ii] + list(range(jj)) for ii, jj in enumerate(range(5))] for head, *rest in aa: print(sum(rest)) #0 #0 #1 #3 #6 |
你可以把list解壓成head,*rest,tail等等。
Collections用作計數器
Collections是我在python中最喜歡的庫之一,在python中,除了原始的預設的,如果你還需要其他的資料結構,你就應該看看這個。
我日常基本工作的一部分就是計算大量而又不是很重要的詞。可能有人會說,你可以把這些詞作為一個字典的key,他們分別的值作為value,在我沒有接觸到collections中的Counter時,我可能會同意你的做法(是的,做這麼多介紹就是因為Counter)。
假設你讀的python語言的維基百科,轉化為一個字串,放到一個list中(標記好順序):
1 2 3 |
import re word_list = list(map(lambda k: k.lower().strip(), re.split(r’[;,:(.s)]s*’, python_string))) word_list[:10] # [‘python’, ‘is’, ‘a’, ‘widely’, ‘used’, ‘general-purpose’, ‘high-level’, ‘programming’, ‘language’, ‘[17][18][19]’] |
到目前為止看起來都不錯,但是如果你想計算這個list中的單詞:
1 2 3 4 |
from collections import defaultdict # again, collections! dictionary = defaultdict(int) for word in word_list: dictionary[word] += 1 |
這個沒有那麼糟糕,但是如果你有了Counter,你將會節約下你的時間做更有意義的事情。
1 2 3 4 5 6 7 8 9 |
from collections import Counter counter = Counter(word_list) # Getting the most common 10 words counter.most_common(10) [(‘the’, 164), (‘and’, 161), (‘a’, 138), (‘python’, 138), (‘of’, 131), (‘is’, 102), (‘to’, 91), (‘in’, 88), (‘’, 56)] counter.keys()[:10] # just like a dictionary [‘’, ‘limited’, ‘all’, ‘code’, ‘managed’, ‘multi-paradigm’, ‘exponentiation’, ‘fromosing’, ‘dynamic’] |
很簡潔吧,但是如果我們看看在Counter中包含的可用的方法:
1 2 3 4 5 6 7 8 |
dir(counter) [‘__add__’, ‘__and__’, ‘__class__’, ‘__cmp__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__dict__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__missing__’, ‘__module__’, ‘__ne__’, ‘__new__’, ‘__or__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__setitem__’, ‘__sizeof__’, ‘__str__’, ‘__sub__’, ‘__subclasshook__’, ‘__weakref__’, ‘clear’, ‘copy’, ‘elements’, ‘fromkeys’, ‘get’, ‘has_key’, ‘items’, ‘iteritems’, ‘iterkeys’, ‘itervalues’, ‘keys’, ‘most_common’, ‘pop’, ‘popitem’, ‘setdefault’, ‘subtract’, ‘update’, ‘values’, ‘viewitems’, ‘viewkeys’, ‘viewvalues’] |
你看到__add__和__sub__方法了嗎,是的,Counter支援加減運算。因此,如果你有很多文字想要去計算單詞,你不必需要Hadoop,你可以運用Counter(作為map)然後把它們加起來(相當於reduce)。這樣你就有構建在Counter上的mapreduce了,你可能以後還會感謝我。
扁平巢狀lists
Collections也有_chain函式,其可被用作扁平巢狀lists
1 2 3 |
from collections import chain ls = [[kk] + list(range(kk)) for kk in range(5)] flattened_list = list(collections._chain(*ls)) |
同時開啟兩個檔案
如果你在處理一個檔案(比如一行一行地),而且要把這些處理好的行寫入到另一個檔案中,你可能情不自禁地像下面這麼去寫:
1 2 3 4 |
with open(input_file_path) as inputfile: with open(output_file_path, ‘w’) as outputfile: for line in inputfile: outputfile.write(process(line)) |
除此之外,你可以在相同的一行裡開啟多個檔案,就像下面這樣:
1 2 3 |
with open(input_file_path) as inputfile, open(output_file_path, ‘w’) as outputfile: for line in inputfile: outputfile.write(process(line)) |
這樣就更簡潔啦!
從一堆資料中找到星期一
如果你有一個資料想去標準化(比如週一之前或是之後),你也許會像下面這樣:
1 2 3 4 |
import datetime previous_monday = some_date - datetime.timedelta(days=some_date.weekday()) # Similarly, you could map to next monday as well next_monday = some_date + date_time.timedelta(days=-some_date.weekday(), weeks=1) |
這就是實現方式。
處理HTML
如果你出於興趣或是利益要爬一個站點,你可能會一直面臨著html標籤。為了去解析各種各樣的html標籤,你可以運用html.parer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from html.parser import HTMLParser class HTMLStrip(HTMLParser): def __init__(self): self.reset() self.ls = [] def handle_data(self, d): self.ls.append(d) def get_data(self): return ‘’.join(self.ls) @staticmethod def strip(snippet): html_strip = HTMLStrip() html_strip.feed(snippet) clean_text = html_strip.get_data() return clean_text snippet = HTMLStrip.strip(html_snippet) |
如果你僅僅想避開html:
1 2 3 4 5 |
escaped_snippet = html.escape(html_snippet) # Back to html snippets(this is new in Python 3.4) html_snippet = html.unescape(escaped_snippet) # and so forth ... |