python筆記(2)
更新時間:2012年10月24日 22:07:04 作者:
python筆記,參考上一篇文章大家繼續(xù)
繼續(xù)List:
刪除元素:
a =[1, 2, 3, 4]
a[2:3] = [] #[1, 2, 4]
del a[2] #[1, 2]
清空list
a[ : ] = []
del a[:]
list作為棧使用(后入先出):
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
stack.pop() # 7
stack.pop() # 6
stack.pop() # 5
用負(fù)數(shù)索引:
b=[1, 2, 3, 4]
b[-2] #3
"+"組合list:
end = ['st', 'nd'] + 5*['th'] + ['xy'] # ['st', 'nd', 'th', 'th', 'th', 'th', 'th', 'xy']
查出某元素在list中的數(shù)量:
lst.('hello') # hello 的數(shù)量
list排序:
sort()
#對鏈表中的元素進(jìn)行適當(dāng)?shù)呐判颉?
reverse()
#倒排鏈表中的元素
函數(shù)指針的問題:
def f2(a, L=[])
L.append(a)
return L
print(f2(1)) # 1
print(f2(2)) # 1, 2 L在這次函數(shù)調(diào)用時是[1]
print(f2(3)) # 1, 2, 3
函數(shù)中的參數(shù)中有:
*參數(shù)名 :表示任意個數(shù)的參數(shù)
** :表示dictionary參數(shù)
控制語句:
IF:
if x < 0:
x = 0
print 'Negative changed to zero'
elif x == 0:
print 'Zero'
elif x == 1:
print 'Single'
else:
print 'More'
FOR:
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
WHILE:
a, b = 0, 1
while b < 1000:
print b,
a, b = b, a+b
#1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
pass :空操作語句
while True:
pass
dictionary: 鍵值對的數(shù)據(jù)結(jié)構(gòu)
用list來構(gòu)造dictionary:
items = [('name', 'dc'), ('age', 78)]
d = dict(items) #{'age': 78, 'name': 'dc'}
有趣的比較:
x = [] #list
x[2] = 'foo' #出錯
x = {} #dictionary
x[2] = 'foo' #正確
內(nèi)容比較雜,學(xué)到什么就記下來。完全利用工作中的空閑和業(yè)余時間來完成,更加充實(shí)了。
刪除元素:
復(fù)制代碼 代碼如下:
a =[1, 2, 3, 4]
a[2:3] = [] #[1, 2, 4]
del a[2] #[1, 2]
清空list
復(fù)制代碼 代碼如下:
a[ : ] = []
del a[:]
list作為棧使用(后入先出):
復(fù)制代碼 代碼如下:
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
stack.pop() # 7
stack.pop() # 6
stack.pop() # 5
用負(fù)數(shù)索引:
復(fù)制代碼 代碼如下:
b=[1, 2, 3, 4]
b[-2] #3
"+"組合list:
復(fù)制代碼 代碼如下:
end = ['st', 'nd'] + 5*['th'] + ['xy'] # ['st', 'nd', 'th', 'th', 'th', 'th', 'th', 'xy']
查出某元素在list中的數(shù)量:
復(fù)制代碼 代碼如下:
lst.('hello') # hello 的數(shù)量
list排序:
復(fù)制代碼 代碼如下:
sort()
#對鏈表中的元素進(jìn)行適當(dāng)?shù)呐判颉?
reverse()
#倒排鏈表中的元素
函數(shù)指針的問題:
復(fù)制代碼 代碼如下:
def f2(a, L=[])
L.append(a)
return L
print(f2(1)) # 1
print(f2(2)) # 1, 2 L在這次函數(shù)調(diào)用時是[1]
print(f2(3)) # 1, 2, 3
函數(shù)中的參數(shù)中有:
*參數(shù)名 :表示任意個數(shù)的參數(shù)
** :表示dictionary參數(shù)
控制語句:
IF:
復(fù)制代碼 代碼如下:
if x < 0:
x = 0
print 'Negative changed to zero'
elif x == 0:
print 'Zero'
elif x == 1:
print 'Single'
else:
print 'More'
FOR:
復(fù)制代碼 代碼如下:
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
WHILE:
復(fù)制代碼 代碼如下:
a, b = 0, 1
while b < 1000:
print b,
a, b = b, a+b
#1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
pass :空操作語句
復(fù)制代碼 代碼如下:
while True:
pass
dictionary: 鍵值對的數(shù)據(jù)結(jié)構(gòu)
用list來構(gòu)造dictionary:
復(fù)制代碼 代碼如下:
items = [('name', 'dc'), ('age', 78)]
d = dict(items) #{'age': 78, 'name': 'dc'}
有趣的比較:
復(fù)制代碼 代碼如下:
x = [] #list
x[2] = 'foo' #出錯
x = {} #dictionary
x[2] = 'foo' #正確
內(nèi)容比較雜,學(xué)到什么就記下來。完全利用工作中的空閑和業(yè)余時間來完成,更加充實(shí)了。
相關(guān)文章
Python實(shí)現(xiàn)從網(wǎng)絡(luò)攝像頭拉流的方法分享
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)從網(wǎng)絡(luò)攝像頭拉流的幾種方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下2023-01-01TensorFlow卷積神經(jīng)網(wǎng)絡(luò)AlexNet實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了TensorFlow卷積神經(jīng)網(wǎng)絡(luò)AlexNet實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11Python中numpy模塊常見用法demo實(shí)例小結(jié)
這篇文章主要介紹了Python中numpy模塊常見用法,結(jié)合實(shí)例形式總結(jié)分析了numpy常見的運(yùn)算操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-03-03用Python實(shí)現(xiàn)大文本文件切割的方法
今天小編就為大家分享一篇用Python實(shí)現(xiàn)大文本文件切割的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python eval()與exec()函數(shù)使用介紹
exec函數(shù)執(zhí)行的是python語句,沒有返回值,eval函數(shù)執(zhí)行的是python表達(dá)式,有返回值,exec函數(shù)和eval函數(shù)都可以傳入命名空間作為參數(shù),本文給大家介紹下Python eval()和exec()函數(shù),感興趣的朋友跟隨小編一起看看吧2023-01-01解決pycharm debug時界面下方不出現(xiàn)step等按鈕及變量值的問題
這篇文章主要介紹了解決pycharm debug時界面下方不出現(xiàn)step等按鈕及變量值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python利用pandas進(jìn)行數(shù)據(jù)合并詳解
當(dāng)使用Python中的pandas庫時,merge函數(shù)是用于合并(或連接)兩個數(shù)據(jù)框(DataFrame)的重要工具。它類似于SQL中的JOIN操作,下面我們就來看看它的具體操作吧2023-11-11