亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python筆記(2)

 更新時間:2012年10月24日 22:07:04   作者:  
python筆記,參考上一篇文章大家繼續(xù)
繼續(xù)List:

刪除元素:
復(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)文章

最新評論