### 獲得更多資料歡迎進入[我的網站](http://rlovep.com/)或者 [csdn](http://blog.csdn.net/peace1213)或者[博客園](http://home.cnblogs.com/u/onepeace/)
> 前面一張主要學習了Python的安裝,以及第一個程序helloword的編寫,以及簡單的輸入和輸出函數
### 序列
這章主要介紹的是列表和元組,而列表和元組是序列的六種內型中的兩種,主要區別。列表可以修改,而元組不能。而序列很好用比如要操作一組人的名字和年齡可以這樣:
~~~
>>>peace=['peace one',23]
>>>rong=['sisi',22]
>>>data=[peace,rong]
>>>data
[['peace one',23],['sisi',22]]
~~~
序列可以包含其他序列;比如data包含了peace和rong序列
### 索引
1、序列名按序號索引
peace[1]
23
data[1][1]
22
2、字符串直接按序號索引還可對輸入進行索引
‘hello’[1]
‘e’
two=raw_input(‘year: ‘)[1]
year: 2015
two
‘0’
### 分片索引、
1跟按序號索引類似,可以使用分片操作來訪問一定范圍內的元素。分片通過冒號分隔開來實現;
tag=’My name is one peace’
tag[11:20]
‘one peace’
注意:第一個索引是分片的第一個元素索引,第二個索引是分片元素最后一個元素索引+1;哪怕像上面的+1索引不存在也沒關系.同樣制空最后一個索引也是可以的;如下:
tag[-9: ]
‘one peace’
2更大的步長,在兩個索引后面再加一個冒號和步長;
tag[1:11:2]
‘ynm s’
注意:同樣步長也可以為負數,不過為負數時,是從后往前輸出。此時必須第一個索引再第二個索引的后面;比如:
tag[11:1:-2]
‘os mn’
### 序列運算
1相加又稱連接
[1,2,3]+[3,4,6,]
[1,2,3,4,5,6]
注意:兩種相同類型才能進行連接操作;比如:[1,2]+’hello’會返回錯誤
2相乘用列表乘以數字x,得到的新的序列會重復被乘序列x次
[42]*5
[42,42,42,42,42]
3成員資格,檢查一個值是否在序列當中,可以使用in運算。
‘p’ in tag
True
‘pe’ in tag
True
‘px’ in tag
False
?4長度,最大值,最小值
#長度函數len
len(tag)
20
#最大值函數max
max([3,4,5])
5
min([3,4,5])
3
### 列表
### list函數
1,list函數,創建列表;
str=list(‘hello’)
str
[‘h’,’e’,’l’,’l’,’o’]
### 列表操作
1改變列表,元素賦值
str[1]=’p’
str
[‘h’,’p’,’l’,’l’,’o’]
?2刪除元素 使用del來刪除
del str[1]
str
[‘h’,’l’,’l’,’o’]
?3分片賦值。主要作用如下:
#1,可以使用與原序列不等長的序列將分片替換
str[1: ]=list(‘peace’)
str
[‘h’,’p’,’e’,’a’,’c’,’e’]
#2,可以不替換任可元素的情況下插入元素
str[1:1]=list(‘one’)
str
[‘h’,’o’,’n’,’e’,’p’,’e’,’a’,’c’,’e’]
#3,當然也可以刪除、
str[1:4]=list()
str
[‘h’,’p’,’e’,’a’,’c’,’e’]
### 列表方法
方法是一個與對象緊密相關的函數。直接對象.方法進行調用
列表有append(末尾追加),count(統計次數),extend(新列表追加),index(找到元素為知),insert(按序號插入)
pop(按序號刪除)remove(按值刪除)reverse(元素翻轉),sort(排序),sorted(獲取排序后的結果),特殊排序:
sort(cmp)通過比較類型排序,sort(key=len)通過建函數排序(此去是長度len),sort(reverse=True)通過True和false來判斷是否翻轉排序;
方法操作如下:
~~~
#append方法
>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.append(list(" tiger"))
>>> name
['s', 'c', 'o', 't', 't', [' ', 't', 'i', 'g', 'e', 'r']]
>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.append("A","B") #添加多個元素即將報錯
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: append() takes exactly one argument (2 given)
>>> name.append("A")
>>> name
['s', 'c', 'o', 't', 't', 'A']
#count方法
>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.count('s')
1
>>> name.count("t")
2
>>> name.count("A")
0
>>> name.append(list("Python"))
>>> name
['s', 'c', 'o', 't', 't', ['P', 'y', 't', 'h', 'o', 'n']]
>>> name.count(['P', 'y', 't', 'h', 'o', 'n'])
1
#extend方法
>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.extend(list(" tiger"))
>>> name
['s', 'c', 'o', 't', 't', ' ', 't', 'i', 'g', 'e', 'r']
#index方法
>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.index('t') ##第一個字母t的索引位置是3
3
>>> name.index('a')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: list.index(x): x not in list
>>> 'a' in name
False
>>> 'a' not in name
True
#insert方法
>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.insert(2,'tiger') ##在索引為2的地方插入字符串tiger
>>> name
['s', 'c', 'tiger', 'o', 't', 't']
#pop方法
>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.pop()
't'
>>> name
['s', 'c', 'o', 't']
>>> name.append("t")
>>> name
['s', 'c', 'o', 't', 't']
#remove方法
>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.remove("t") #去掉第一個t
>>> name
['s', 'c', 'o', 't']
>>> name.remove("A") #不存在會報錯
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: list.remove(x): x not in list
>>> "A" not in name
True
>>> name.remove("s","c") #一次只能移除一個元素
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: remove() takes exactly one argument (2 given)
#reverse方法
>>> name = list("scott")
>>> name
['s', 'c', 'o', 't', 't']
>>> name.reverse()
>>> name
['t', 't', 'o', 'c', 's']
#sort方法
>>> result = [8,5,5,3,9]
>>> result.sort()
>>> result
[3, 5, 5, 8, 9]
#sorted方法
>>> result = [8,5,5,3,9]
>>> result2 = sorted(result)
>>> result
[8, 5, 5, 3, 9]
>>> result2
[3, 5, 5, 8, 9]
~~~
### 元組
元組
元組與列表一樣,也是一種序列,唯一不同的是元組不可以修改:
### 元組操作
~~~
>>> 1,2,3
(1, 2, 3)
>>> ()
()
#對于單個元素必須加上逗號,加上逗號后就表示數字是元組了
>>> 42
42
>>> 42,
(42,)
>>> (42,)
(42,)
~~~
### 元組tuple函數
~~~
>>> tuple([1,2,3])
(1, 2, 3)
>>> tuple('abc')
('a', 'b', 'c')
>>> tuple((1,2,3))
(1, 2, 3)
~~~
### 列表與元組的相互轉化
~~~
>>> T=('cc','aa','dd','bb')
>>> tmp=list(T)
>>> tmp
['cc', 'aa', 'dd', 'bb']
>>> T=tuple(tmp)
>>> T
('cc', 'aa', 'dd', 'bb')
~~~
#### 相關鏈接:
[python3入門之類](http://rlovep.com/2015/09/23/python3%E5%85%A5%E9%97%A8%E4%B9%8B%E7%B1%BB/)
[python3入門之函數](http://rlovep.com/2015/09/06/python3%E5%85%A5%E9%97%A8%E4%B9%8B%E5%87%BD%E6%95%B0/)
[python3入門之循環](http://rlovep.com/2015/09/06/python3%E5%85%A5%E9%97%A8%E4%B9%8B%E5%BE%AA%E7%8E%AF/)
[python3之if語句](http://rlovep.com/2015/08/05/python3%E4%B9%8Bif%E8%AF%AD%E5%8F%A5/)
[python3入門之賦值語句介紹](http://rlovep.com/2015/08/03/python3%E5%85%A5%E9%97%A8%E4%B9%8B%E8%B5%8B%E5%80%BC%E8%AF%AD%E5%8F%A5%E4%BB%8B%E7%BB%8D/)
[python3入門之print,import,input介紹](http://rlovep.com/2015/08/03/python3%E5%85%A5%E9%97%A8%E4%B9%8Bprint%EF%BC%8Cimport%EF%BC%8Cinput%E4%BB%8B%E7%BB%8D/)
[python3入門之set](http://www.cnblogs.com/onepeace/p/4791578.html)
[python3入門之字典](http://rlovep.com/2015/07/29/python3%E5%85%A5%E9%97%A8%E4%B9%8B%E5%AD%97%E5%85%B8/)
[python3入門之字符串](http://rlovep.com/2015/07/28/python%E5%85%A5%E9%97%A8%E4%B9%8B%E5%AD%97%E7%AC%A6%E4%B8%B2/)
[python3入門之列表和元組](http://rlovep.com/2015/07/14/python%E5%85%A5%E9%97%A8%E4%B9%8B%E5%88%97%E8%A1%A8%E5%92%8C%E5%85%83%E7%BB%84/)
[python3入門之軟件安裝](http://rlovep.com/2015/07/14/python%E5%85%A5%E9%97%A8%E4%B9%8B%E8%BD%AF%E4%BB%B6%E5%AE%89%E8%A3%85/)
[python3爬蟲之入門和正則表達式](http://rlovep.com/2015/09/23/python3%E7%88%AC%E8%99%AB%E4%B9%8B%E5%85%A5%E9%97%A8%E5%92%8C%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/)