1. 程式人生 > >python基礎教程(第三版)學習筆記(二)

python基礎教程(第三版)學習筆記(二)

第二章 列表和元組
2.1序列概述
Python內建的序列包括列表、元組、字串、Unicode字串、buffer物件和xrange物件等,本章僅介紹列表和元組。
列表和元組的主要不同在於,列表的元素是可以修改的,而元組不可以單獨更改其中的單個元素,但可以更改元組的整體內容。
在序列中,每個元素都有編號。
2.2通用的序列操作
有幾種操作適用於所有序列,包括索引、切片、相加、相乘和成員資格檢查。另外,Python還提供了一些內建函式,可用於確定序列的長度以及找出序列中最大和最小的元素。
2.2.1索引
序列中的所有元素都有編號——從0開始遞增。你可像下面這樣使用編號來訪問各個元素:
'''

grStr="hello"
print(grStr[0])


'''

h


------------------
(program exited with code: 0)

請按任意鍵繼續. . 

.
當你使用負數索引時,Python將從右(即從最後一個元素)開始往左數。
'''

print(grStr[-1])


'''

o


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


2.2.2切片
除使用索引來訪問單個元素外,還可使用切片來訪問特定範圍內的元素。為此,可使用兩個索引,並用冒號分隔:
'''

print(grStr[1:3])


'''

el


------------------
(program exited with code: 0)

請按任意鍵繼續. . .

1、一個引數:
a、在冒號前面將擷取若是正數向後擷取,負數向前。
b、在冒號後若是正數向前擷取負數向後。
c、沒有引數擷取全部序列。
2、步長
兩個冒號三個引數最後一個是步長。比如lest[n:m:k]其中k是步長。步長不能為0,否則無法向前移動,但可以為負數,即從右向左提取元素。
2.2.3序列拼接
可使用加法運算子來拼接序列
'''

print([1,2,3,4]+[4,3,2,1])


'''

[1, 2, 3, 4, 4, 3, 2, 1]


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


'''

print([1,2,3,4]+["Hello","world"])

 

'''
[1, 2, 3, 4, 'Hello', 'world']


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


一般而言,不能拼接不同型別的序列。

print([1,2,3,4]+"Hello world")

 

Traceback (most recent call last):
  File "xx.py", line 2, in <module>
    print([1,2,3,4]+"Hello world")
TypeError: can only concatenate list (not "str") to list


------------------
(program exited with code: 1)

請按任意鍵繼續. . .


2.2.4用*建立序列
將序列與數x“相乘”時,將重複這個序列x次來建立一個新序列:
'''

print("Hello world"*4)


'''

Hello worldHello worldHello worldHello world


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


'''

print([1,2,3]*3)


'''

[1, 2, 3, 1, 2, 3, 1, 2, 3]


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


在python中None表示“空”,因此可以用None建立一個有若干元素的序列。
'''

print([None]*5)


'''

[None, None, None, None, None]


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


'''

l=[]*5
print(len(l))
print(len([None]*5))


'''

0
5


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


注:len()為python的內建函式,用來輸出序列長(元素個數)。
2.2.5包含檢查 in
要檢查特定的值是否包含在序列中,可使用運算子in。返回相應的值:滿足時返回True,不滿足時返回False。
'''

print("x" in "wrold")


'''

False


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


'''

print(3 in [1,2,3,4])


'''

True


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


另外,在Python中還有兩個用於序列的內建函式max()和min(),就不再演示了。
2.3 列表
2.3.1 list
list其實不是Python的內建函式,而是“類”(關於這個革命性的概念以後在詳細討論),在這裡姑且把list當作函式用吧!
'''

rts="Hello wrold"
print(list(rts))


'''

['H', 'e', 'l', 'l', 'o', ' ', 'w', 'r', 'o', 'l', 'd']


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


神奇吧?
2.3.2列表的基本操作
列表除了具有序列操作之外,還有自己的特點。
1、改變元素值
'''

x=[1,2,"me",3,"you"]
x[4]=5
print(x)


'''

[1, 2, 'me', 3, 5]


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


2、刪除元素
從列表中刪除元素也很容易,只需使用del語句即可。
'''

del x[3]
print(x)


'''

[1, 2, 'me', 5]


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


注意:del沒有返回值,例如:

d=(del x[3])


會報如下作錯誤:

python -m py_compile "xx.py" (在目錄 E:\pythonProjects 中)
  File "xx.py", line 6
    d=(del x[3])
         ^
SyntaxError: invalid syntax
編譯失敗。


3、給切片賦值
'''

x[:2]=["you","he"]
print(x)


'''

['you', 'he', 'me', 5]


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


使用切片賦值還可在不替換原有元素的情況下插入新元素。
'''

x[4:1]=[6,7,8,9,10]
print(x)


'''

['you', 'he', 'me', 5, 6, 7, 8, 9, 10]


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


也可以用空序列[]來刪除列表中相應元素。
'''

x[2:6]=[]
print(x)


'''

['you', 'he', 8, 9, 10]


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


2.3.3列表方法:
方法,一般指某物件的方法,和函式不同,使用方法要用英語句點顯示指定其所在的物件,其格式為:
object.metthod(parameter)
1、append
用於將一個物件附加到列表末尾。
'''

x.append("she")
print(x)


'''

['you', 'he', 8, 9, 10, 'she']


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


2、clear
用於清空列表的內容。
'''

s=x.clear()
print(s)
print(x)


'''

None
[]


------------------
(program exited with code: 0)

請按任意鍵繼續. . 

.
3、copy
複製列表。
'''

a=[1,2,3,4]
print(a)
b=a.copy()
print(b)
a.append(6)
print(a)
print(b)


'''

[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4, 6]
[1, 2, 3, 4]


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


copy是建立了一個和原列表不同的列表,他和a=b不同,a=b是賦值,當b發生變化時a同時發生變化。
這類似於使用a[:]或list(a),它們也都複製a。
4、count
計算指定的元素在列表中出現了多少次。
5、extend
能夠同時將多個值附加到列表末尾,為此可將這些值組成的序列作為引數提供給方法extend。換而言之,你可使用一個列表來擴充套件另一個列表。
'''

a.extend(b)
print(a)


'''

[1, 2, 3, 4, 6, 1, 2, 3, 4]


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


用+拼接出來的列表與前一個示例擴充套件得到的列表完全相同,但在這裡用+拼接a並沒有被修改,而是產生出新的列表,而用extend卻改變了a。但是拼接的效率要比extend低。
6、index
在列表中查詢指定值第一次出現的索引。
7、insert
用於將一個物件插入列表。
格式為:物件.insert(插入位置,要插入的物件)。
8、pop
從列表中刪除末尾的那個元素。
9、remove
用於刪除第一個指定值的元素。
'''

str1=["a","b","a","b","c"]
print(str1)
str2=str1.remove("b")
print(str2)
print(str1)


'''

['a', 'b', 'a', 'b', 'c']
None
['a', 'a', 'b', 'c']


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


10、reverse
按相反的順序排列列表中的元素。
11、sort
用於對列表就地排序。
2.4 元組
與列表一樣,元組也是序列,唯一的差別在於元組是不能修改的(你可能注意到了,字串也不能修改)。元組語法很簡單,只要將一些值用逗號分隔,就能自動建立一個元組,或者用括號()定義。。
'''

tup=1,2,3,4
print(tup)
tupl=(1,2,3,4,5)
print(tupl)


'''

(1, 2, 3, 4)
(1, 2, 3, 4, 5)


------------------
(program exited with code: 0)

請按任意鍵繼續. . .


第三章 使用字串(待續)