10個易被忽視但應掌握的Python基本用法

來源:互聯網
上載者:User
我一輩子都在寫代碼,但從來沒有掌握編碼的精髓。大部分情況下使用Visual Basic,因為我用VB最舒服。同時還略微瞭解一點其他語言(R、C、JavaScript、Applescript、Hypertext和1979年學習的BASIC)。幾年前,我決定只用Python,以此來提高我的編碼能力。在此過程中重複發明了許多輪子,但我並不介意,因為我享受解決問題的樂趣。同時有時能發現更有效、Python式的解決方案。時間長了以後,會有頓悟的時刻,意識到根本沒必要用困難且冗長的方式處理問題。下面列出10條Python用法,如果我早點發現,也許能節省很多時間。

這裡沒有列表推導和lambda函數。雖然這兩個用法都是Python式的,效率高也非常酷,但由於經常在StackOverflow或其他地方碰到,所以學Python的應該都知道這兩個東西。同時也沒有三元運算子、裝飾器和產生器,因為我很少用到。

本文還有一個IPython notebook nbviewer版本。
1. 在Python 2中使用Python 3式的輸出

Python 2與Python 3不相容,這讓我不知道該選擇哪個版本的Python。最終我選擇了Python 2,因為當時許多我需要用的庫都與Python 3不相容。

但實際上,日常使用中最大的版本差異是輸出(print)和除法行為。現在我在Python 2的代碼中都用import from future來匯入Python 3的輸出和除法。現在我用到的幾乎所有庫都支援Python 3,因此會很快遷移到Python 3中。

mynumber = 5 print "Python 2:"print "The number is %d" % (mynumber)print mynumber / 2,print mynumber // 2 from __future__ import print_functionfrom __future__ import division print('nPython 3:')print("The number is {}".format(mynumber))print(mynumber / 2, end=' ')print(mynumber // 2) Python 2:The number is 52 2 Python 3:The number is 52.5 2

對了,對於C系的那些更喜歡括弧而不是縮排的開發人員,這裡還有一個彩蛋:

from __future__ import bracesFile "", line 1from __future__ import bracesSyntaxError: not a chance

2. enumerate(list)

很明顯,迭代列表時,應該同時迭代其中的元素及其索引,但在很長一段時間內,我都尷尬的使用計數變數或切片。

mylist = ["It's", 'only', 'a', 'model'] for index, item in enumerate(mylist):  print(index, item) 0 It's1 only2 a3 model

3. 鏈式比較操作符

由於我以前使用的是靜態語言(在這些語言中該用法有二義性),從來沒有將兩個比較操作符放在一個運算式中。在許多語言中,4 > 3 > 2會返回False,因為4 > 3的結果是布爾值,而True > 2將得出False。

mynumber = 3 if 4 > mynumber > 2:  print("Chained comparison operators work! n" * 3) Chained comparison operators work!Chained comparison operators work!Chained comparison operators work!

4. collections.Counter

Python的集合庫看上去是最好的。在計算需要集合中元素的個數時,StackOverflow找到的答案是建立有序字典,但我堅持使用一個程式碼片段來建立字典,計算結果中元素出現的頻率。直到有一天,我發現可以用collections.deque。

from collections import Counterfrom random import randrangeimport pprint mycounter = Counter() for i in range(100):  random_number = randrange(10)  mycounter[random_number] += 1 for i in range(10):  print(i, mycounter[i]) 

0 101 102 133 64 65 116 107 148 129 8

5. 字典推導

Python開發人員的一個重要標誌就是理解列表推導,但最終我發現字典推導也很有用,特別是在交換字典的鍵和值的時候。

my_phrase = ["No", "one", "expects", "the", "Spanish", "Inquisition"]my_dict = {key: value for value, key in enumerate(my_phrase)}print(my_dict)reversed_dict = {value: key for key, value in my_dict.items()}print(reversed_dict)

{'Inquisition': 5, 'No': 0, 'expects': 2, 'one': 1, 'Spanish': 4, 'the': 3}{0: 'No', 1: 'one', 2: 'expects', 3: 'the', 4: 'Spanish', 5: 'Inquisition'}

6. 用subprocess執行shell命令

以前,我使用os庫調用外部命令處理檔案,而現在我可以在Python中以編碼的方式執行諸如ffmpeg這樣的複雜命令進行視頻編輯。

(是的,我和我的客戶都使用Windows,如果你們因此鄙視我,我會大方地接受!)

注意,用os庫完成這個特定命令比用subprocess更好。我只想有一個大家都熟悉的命令。同時,一般來說,在subprocess中使用shell=True參數是非常糟糕的主意,在這裡使用這個參數僅僅是為了能在一個IPython notebook單元中放置命令的輸出。不要自己使用這個參數!

import subprocessoutput = subprocess.check_output('dir', shell=True)print(output)

Volume in drive C is OSVolume Serial Number is [REDACTED]Directory of C:UsersDavidDocuments[REDACTED] 2014-11-26 06:04 AM       .2014-11-26 06:04 AM       ..2014-11-23 11:47 AM       .git2014-11-26 06:06 AM       .ipynb_checkpoints2014-11-23 08:59 AM       CCCma2014-09-03 06:58 AM      19,450 colorbrewdict.py2014-09-03 06:58 AM      92,175 imagecompare.ipynb2014-11-23 08:41 AM       Japan_Earthquakes2014-09-03 06:58 AM       1,100 LICENSE2014-09-03 06:58 AM       5,263 monty_monte.ipynb2014-09-03 06:58 AM      31,082 pocket_tumblr_reddit_api.ipynb2014-11-26 06:04 AM       3,211 README.md2014-11-26 06:14 AM      19,898 top_10_python_idioms.ipynb2014-09-03 06:58 AM       5,813 tree_convert_mega_to_gexf.ipynb2014-09-03 06:58 AM       5,453 tree_convert_mega_to_json.ipynb2014-09-03 06:58 AM       1,211 tree_convert_newick_to_json.py2014-09-03 06:58 AM      55,970 weather_ML.ipynb       11 File(s)    240,626 bytes        6 Dir(s) 180,880,490,496 bytes free

7. 字典的.get()和.iteritems()方法

字典的get()方法可以設定預設值,當用get()尋找的鍵不存在時,返回方法中的預設值參數是很有用的。與列表中的enumerate()相同,可以用索引值元組迭代字典中的元素。

my_dict = {'name': 'Lancelot', 'quest': 'Holy Grail', 'favourite_color': 'blue'} print(my_dict.get('airspeed velocity of an unladen swallow', 'African or European?n')) for key, value in my_dict.iteritems():  print(key, value, sep=": ") African or European? quest: Holy Grailname: Lancelotfavourite_color: blue

8. 用於交換元素的元組解包

在VB中,每當需要交換兩個變數時,都要用要一個愚蠢的臨時變數:c = a; a = b; b = c

a = 'Spam'b = 'Eggs' print(a, b) a, b = b, a print(a, b) Spam EggsEggs Spam

9. 內省工具Introspection tools

我知道dir()方法,我本以為help()方法和IPython中的?魔法命令是一樣的,但help()的功能更強大。

my_dict = {'That': 'an ex-parrot!'} help(my_dict)

Help on dict object: class dict(object) | dict() -> new empty dictionary | dict(mapping) -> new dictionary initialized from a mapping object's | (key, value) pairs | dict(iterable) -> new dictionary initialized as if via: | d = {} | for k, v in iterable: | d[k] = v | dict(**kwargs) -> new dictionary initialized with the name=value pairs | in the keyword argument list. For example: dict(one=1, two=2) | | Methods defined here: | | __cmp__(...) | x.__cmp__(y) <==> cmp(x,y) | | __contains__(...) | D.__contains__(k) -> True if D has a key k, else False | | __delitem__(...) | x.__delitem__(y) <==> del x[y] | | __eq__(...) | x.__eq__(y) <==> x==y | [TRUNCATED FOR SPACE]  | | update(...) | D.update([E, ]**F) -> None. Update D from dict/iterable E and F. | If E present and has a .keys() method, does: for k in E: D[k] = E[k] | If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v | In either case, this is followed by: for k in F: D[k] = F[k] | | values(...) | D.values() -> list of D's values | | viewitems(...) | D.viewitems() -> a set-like object providing a view on D's items | | viewkeys(...) | D.viewkeys() -> a set-like object providing a view on D's keys | | viewvalues(...) | D.viewvalues() -> an object providing a view on D's values | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __hash__ = None | | __new__ = | T.__new__(S, ...) -> a new object with type S, a subtype of T

10. PEP-8相容的字串串連

PEP8是Python編碼樣式指南。撇開其他的不看,PEP8要求每行不能超過80個字元,超過的部分要換行並縮排。

可以通過反斜線、帶逗號“,”的圓括弧“()”、或者額外的加號“+”來完成換行。但對於多行字串,這些解決方案都不夠優雅。Python有個多行字串記號,即三個引號,但這樣無法換行後保持縮排。

還有一個方法,那就是不帶逗號的圓括弧。我不知道為什麼這種方式能工作,但能用就行。

my_long_text = ("We are no longer the knights who say Ni! "        "We are now the knights who say ekki-ekki-"        "ekki-p'tang-zoom-boing-z'nourrwringmm!")print(my_long_text)

we are no longer the knights who say Ni! We are now the knights who say ekki-ekki-ekki-p'tang-zoom-boing-z'nourrwringmm!
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.