bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

python函數操作邏輯 python的邏輯運算

python函數深入淺出 12.os.getcwd()函數詳解

os是python自帶的系統模塊,需要import使用

創新互聯專業為企業提供華安網站建設、華安做網站、華安網站設計、華安網站制作等企業網站建設、網頁設計與制作、華安企業網站模板建站服務,10年華安做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。

os 源于英文Operating System(操作系統)的縮寫

cwd 則是源于Current Working Directory,中文意思是 當前工作目錄

所以os.getcwd() 指獲取當前工作目錄

示例:

getcwd()方法語法格式如下:

總之,舉例來講,os.getcwd()、sys.path[0] (sys.argv[0])和 file 的區別是這樣的:

假設目錄結構是:

然后我們在C:\test下面執行

這時sub_path.py里面與各種用法對應的值其實是:

path的準確定位對于import包,讀寫文件都非常重要,

如果一時不能理解,可以在文件執行開頭多加幾個

print幫助我們確定那個路徑是我們希望拿到的,然后將其設為全局變量就好了

對基礎運行環境有疑問的,推薦參考: python函數深入淺出 0.基礎篇

python函數深入淺出 16.time.sleep()函數詳解

time.sleep() 函數命名來源于英文單詞time(時間)和sleep(睡眠)。

time 是python帶的非內置庫,使用時需要import,主要用于處理和時間相關的操作。

time.sleep用于給定時間內掛起(等待)當前線程的執行。

time.sleep() 函數的例子:

可以注釋掉time.sleep(2)再運行一次對比一下

可以看到雖然都是打印出一樣的結果,但time.sleep()加入了等待時間

這里還要解釋一下python中線程與進程的區別。

舉個例子,廚房做菜看成是一個進程,那么這個進程下面就可能有多個人或一個人(cpu基本執行單元,即線程)來執行,多個人可以分別洗菜,刷碗,擺盤等等同時作業,他們又是共享這個廚房的資源的。每個人存在一定的資源競爭關系,比如爐火只有1個。

這里time.sleep是針對線程執行的,也就是其中一個人去sleep睡覺了,不影響其他人的繼續工作。

參數

該函數沒有返回值。

結果類似如下:

可以看到秒數相差了5

time.sleep()常用于推遲執行的場景

在python中,與時間相關的模塊有:time,datetime以及calendar

對基礎運行環境有疑問的,推薦參考: python函數深入淺出 0.基礎篇

python函數深入淺出 13.os.listdir()函數詳解

這是os模塊下操作目錄和文件相關的函數

listdir() 只返回文件夾下所有文件名的列表(list)

math 模塊則會把參數轉換為 float。

listdir()方法語法格式如下:

參數

path -- 需要列出的目錄路徑,默認為當前路徑

其他相關的操作文件(文件夾)函數如下:

當我們需要遍歷某個文件夾下文件/目錄通常有兩種操作

當獲取文件后如何獲取其后綴名,判斷是否指定格式的文檔、圖片比如txt,doc,bmp,png,gif,jpg等:

熟練操作文件和目錄,可以幫組我們寫批量腳本時更高效,也是重要的python基礎之一。

對基礎運行環境有疑問的,推薦參考: python函數深入淺出 0.基礎篇

python字典操作函數

字典是一種通過名字或者關鍵字引用的得數據結構,其鍵可以是數字、字符串、元組,這種結構類型也稱之為映射。字典類型是Python中唯一內建的映射類型,基本的操作包括如下:

(1)len():返回字典中鍵—值對的數量;

(2)d[k]:返回關鍵字對于的值;

(3)d[k]=v:將值關聯到鍵值k上;

(4)del d[k]:刪除鍵值為k的項;

(5)key in d:鍵值key是否在d中,是返回True,否則返回False。

(6)clear函數:清除字典中的所有項

(7)copy函數:返回一個具有相同鍵值的新字典;deepcopy()函數使用深復制,復制其包含所有的值,這個方法可以解決由于副本修改而使原始字典也變化的問題

(8)fromkeys函數:使用給定的鍵建立新的字典,鍵默認對應的值為None

(9)get函數:訪問字典成員

(10)has_key函數:檢查字典中是否含有給出的鍵

(11)items和iteritems函數:items將所有的字典項以列表方式返回,列表中項來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表

(12)keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器

(13)pop函數:刪除字典中對應的鍵

(14)popitem函數:移出字典中的項

(15)setdefault函數:類似于get方法,獲取與給定鍵相關聯的值,也可以在字典中不包含給定鍵的情況下設定相應的鍵值

(16)update函數:用一個字典更新另外一個字典

(17)?values和itervalues函數:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復的元素

一、字典的創建

1.1 直接創建字典

d={'one':1,'two':2,'three':3}

printd

printd['two']

printd['three']

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

1.2 通過dict創建字典

# _*_ coding:utf-8 _*_

items=[('one',1),('two',2),('three',3),('four',4)]

printu'items中的內容:'

printitems

printu'利用dict創建字典,輸出字典內容:'

d=dict(items)

printd

printu'查詢字典中的內容:'

printd['one']

printd['three']

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

items中的內容:

[('one',1), ('two',2), ('three',3), ('four',4)]

利用dict創建字典,輸出字典內容:

{'four':4,'three':3,'two':2,'one':1}

查詢字典中的內容:

或者通過關鍵字創建字典

# _*_ coding:utf-8 _*_

d=dict(one=1,two=2,three=3)

printu'輸出字典內容:'

printd

printu'查詢字典中的內容:'

printd['one']

printd['three']

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

輸出字典內容:

{'three':3,'two':2,'one':1}

查詢字典中的內容:

二、字典的格式化字符串

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3,'four':4}

printd

print"three is %(three)s."%d

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four':4,'three':3,'two':2,'one':1}

threeis3.

三、字典方法

3.1?clear函數:清除字典中的所有項

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3,'four':4}

printd

d.clear()

printd

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four':4,'three':3,'two':2,'one':1}

{}

請看下面兩個例子

3.1.1

# _*_ coding:utf-8 _*_

d={}

dd=d

d['one']=1

d['two']=2

printdd

d={}

printd

printdd

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two':2,'one':1}

{}

{'two':2,'one':1}

3.1.2

# _*_ coding:utf-8 _*_

d={}

dd=d

d['one']=1

d['two']=2

printdd

d.clear()

printd

printdd

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two':2,'one':1}

{}

{}

3.1.2與3.1.1唯一不同的是在對字典d的清空處理上,3.1.1將d關聯到一個新的空字典上,這種方式對字典dd是沒有影響的,所以在字典d被置空后,字典dd里面的值仍舊沒有變化。但是在3.1.2中clear方法清空字典d中的內容,clear是一個原地操作的方法,使得d中的內容全部被置空,這樣dd所指向的空間也被置空。

3.2?copy函數:返回一個具有相同鍵值的新字典

# _*_ coding:utf-8 _*_

x={'one':1,'two':2,'three':3,'test':['a','b','c']}

printu'初始X字典:'

printx

printu'X復制到Y:'

y=x.copy()

printu'Y字典:'

printy

y['three']=33

printu'修改Y中的值,觀察輸出:'

printy

printx

printu'刪除Y中的值,觀察輸出'

y['test'].remove('c')

printy

printx

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

初始X字典:

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

X復制到Y:

Y字典:

{'test': ['a','b','c'],'one':1,'three':3,'two':2}

修改Y中的值,觀察輸出:

{'test': ['a','b','c'],'one':1,'three':33,'two':2}

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

刪除Y中的值,觀察輸出

{'test': ['a','b'],'one':1,'three':33,'two':2}

{'test': ['a','b'],'three':3,'two':2,'one':1}

注:在復制的副本中對值進行替換后,對原來的字典不產生影響,但是如果修改了副本,原始的字典也會被修改。deepcopy函數使用深復制,復制其包含所有的值,這個方法可以解決由于副本修改而使原始字典也變化的問題。

# _*_ coding:utf-8 _*_

fromcopyimportdeepcopy

x={}

x['test']=['a','b','c','d']

y=x.copy()

z=deepcopy(x)

printu'輸出:'

printy

printz

printu'修改后輸出:'

x['test'].append('e')

printy

printz

運算輸出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

輸出:

{'test': ['a','b','c','d']}

{'test': ['a','b','c','d']}

修改后輸出:

{'test': ['a','b','c','d','e']}

{'test': ['a','b','c','d']}

3.3?fromkeys函數:使用給定的鍵建立新的字典,鍵默認對應的值為None

# _*_ coding:utf-8 _*_

d=dict.fromkeys(['one','two','three'])

printd

運算輸出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':None,'two':None,'one':None}

或者指定默認的對應值

# _*_ coding:utf-8 _*_

d=dict.fromkeys(['one','two','three'],'unknow')

printd

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':'unknow','two':'unknow','one':'unknow'}

3.4?get函數:訪問字典成員

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.get('one')

printd.get('four')

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

1

None

注:get函數可以訪問字典中不存在的鍵,當該鍵不存在是返回None

3.5?has_key函數:檢查字典中是否含有給出的鍵

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.has_key('one')

printd.has_key('four')

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

True

False

3.6?items和iteritems函數:items將所有的字典項以列表方式返回,列表中項來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

list=d.items()

forkey,valueinlist:

printkey,':',value

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

three :3

two :2

one :1

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

it=d.iteritems()

fork,vinit:

print"d[%s]="%k,v

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

d[three]=3

d[two]=2

d[one]=1

3.7?keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printu'keys方法:'

list=d.keys()

printlist

printu'\niterkeys方法:'

it=d.iterkeys()

forxinit:

printx

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

keys方法:

['three','two','one']

iterkeys方法:

three

two

one

3.8?pop函數:刪除字典中對應的鍵

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

d.pop('one')

printd

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

{'three':3,'two':2}

3.9?popitem函數:移出字典中的項

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

d.popitem()

printd

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

{'two':2,'one':1}

3.10?setdefault函數:類似于get方法,獲取與給定鍵相關聯的值,也可以在字典中不包含給定鍵的情況下設定相應的鍵值

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.setdefault('one',1)

printd.setdefault('four',4)

printd

運算結果:

{'three':3,'two':2,'one':1}

{'four':4,'three':3,'two':2,'one':1}

3.11?update函數:用一個字典更新另外一個字典

# _*_ coding:utf-8 _*_

d={

'one':123,

'two':2,

'three':3

}

printd

x={'one':1}

d.update(x)

printd

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':123}

{'three':3,'two':2,'one':1}

3.12?values和itervalues函數:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復的元素

# _*_ coding:utf-8 _*_

d={

'one':123,

'two':2,

'three':3,

'test':2

}

printd.values()

運算結果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

[2,3,2,123]

網頁名稱:python函數操作邏輯 python的邏輯運算
文章鏈接:http://vcdvsql.cn/article30/hepspo.html

成都網站建設公司_創新互聯,為您提供手機網站建設商城網站搜索引擎優化網站維護網站營銷

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站建設網站維護公司