新手必備的Python實用技巧和工具

2022-09-25 14:39:14 字數 3252 閱讀 7174

x = 6

y = 5

x, y = y, x

print x

>>> 5

print y

>>> 6

print "hello" if true else "world"

>>> hello

下面的最後一種方式在繫結兩個不同型別的物件時顯得很cool程式設計客棧。

nfc = ["packers", "49ers"]

afc = ["r**ens", "patriots"]

print nfc + afc

>>> ['packers', '49ers', 'r**ens', 'patriots']

print str(1) + " world"

>>> 1 world

print `1` + " world"

>>> 1 world

print 1, "world"

>>> 1 world

print nfc, 1

>>> ['packers', '49ers'] 1

#除后向下取整程式設計客棧

print 5.0//2

>>> 2

# 2的5次方

print 2**5

>> 32

print .3/.1

>>> 2.9999999999999996

print .3//.1

>>> 2.0

這是我見過諸多語言中很少有的如此棒的簡便法

x = 2

if 3 > x > 1:

print x

>>> 2

if 1 < x > 0:

print x

>>> 2

nfc = ["packers", "49ers"]

afc = ["r**ens", "patriots"]

for teama, teamb in zip(nfc, afc):

print teama + " vs. " + teamb

>>> packers vs. r**ens

>>> 49ers vs. patriots

teams = ["packers", "49ers", "r**ens", "patriots"]

for index, team in enumerate(teams):

print index, team

>>> 0 packers

>>> 1 49ers

>>> 2 r**ens

>>> 3 patriots

已知乙個列表,我們可以刷選出偶數列表方法:

numbers = [1,2,3,4,5,6]

even =

for number in numbers:

if number%2 == 0:

even.append(number)

轉變成如下:

numbers = [1,2,3,4,5,6]

even = [number for number in numbers if number%2 == 0]

是不是很牛呢,哈哈。

和列表推導類似,字典可以做同樣的工作:

teams = ["packers", "49ers", "r**ens", "patriots"]

print

>>>

items = [0]*3

print items

>>> [0,0,0]

teams = ["packers", "49ers", "r**ens", "patriots"]

print ", ".join(teams)

>>> 'packers, 49ers, r**ens, patriots'

我承認try/except**並**致,不過這裡有一種簡單方法,嘗試在字典中查詢key,如果沒有找到對應的alue將用第二個引數設為其變數值。

data =

tris_admin = data['admin']

except keyerror:

is_admin = false

data =

is_admin = data.get('admin', false)

有時,你只需要列表中的部分元素,這裡是一些www.cppcns.com獲取列表子集的方法。

x = [1,2,3,4,5,6]

#前3個

print x[:3]

>>> [1,2,3]

#中間4個

print x[1:5]

>>> [2,3,4,5]

#最後3個

print x[-3:]

>>> [4,5,6]

#奇數項

print x[::2]

>>> [1,3,5]

#偶數項

print x[1::2]

>>> [2,4,6]

除了python內建的資料型別外,在collection模組同樣還包括一些特別的用例,在有些場合counter非常實用。如果你參加過在這一年的facebook hackercup,你甚至也能找到他的實用之處。

from collections import counter

print counter("hello")

>>> counter()

和collections庫一樣,還有乙個庫叫itertools,對某些問題真能高效地解決。其中乙個用例是查詢所有組合,他能告訴你在乙個組中元素的所有不能的組合方式

from itertools import combinations

teams = ["packers", "49ers", "r**ens", "patriots"]

for game in combinations(teams, 2):

print game

>>> ('packers', '49ers')

>>> ('packers', 'r**ens')

>>> ('packers', 'patriots')

>>> ('49ers', 'r**ens')

>>> ('49ers', 'patriots')

>>> ('r**ens', 'patriots')

比起實用技術來說這是乙個很有趣的事,在python中,true和false是全域性變數,因此:

false = true

if false:

print "hello"

else:

print "world"

>>> hello

Python實用技巧

1 改變工作目錄 1 import os2 os.chdir c users mr.zhao 2 搜尋制定目錄下的檔案 1 import glob 2 glob.glob c user mr.zhao csv 3 對字典進行排序 1 dict test 2 sorted dict test.item...

python實用技巧(一)

python實用技巧 一 python實用技巧 二 python實用技巧 三 python實用技巧 四 已知x是多維陣列型別,則 在作業系統 windows或者linux 命令列介面下 python v python 3.4 3 anaconda 2.3.0 32 bit 如果我們已經進行pytho...

Python實用技巧 object篇

返回字典中所有鍵的列表。def keys only flat dict return list flat dict.keys 示例 ages keys only ages peter isabel anna 建立乙個與提供的物件具有相同鍵的物件,每個值為通過提供的函式生成的值。def map val...