python數據類型:序列(字符串,元組,列表,字典)
序列通常有2個特點:
1,可以根據索引取值
2,可以切片操作
字符串,元組,列表,字典,都可以看做是序列類型
我的操作環境:Ubuntu16.04+python2.7
一、字符串類型
>按索引獲取,索引從0開始
1 >>> name=‘ghostwu‘ 2 >>> name[0] 3 ‘g‘ 4 >>> name[1] 5 ‘h‘ 6 >>> name[6] 7 ‘u‘ 8 >>>
>切片操作,第1個冒號的值,表示從哪個索引開始切片。第2個冒號的值,表示從到哪個索引結束(註意:結果不包含這個位置)。第3個冒號的值,表示步長
>>> name=‘My Name Is Ghostwu‘ >>> name[0:7] ‘My Name‘ >>> name[0:7:1] ‘My Name‘ >>> name[0:7:2] ‘M ae‘
默認切片操作為:從左到右。如果步長為負數,表示從右往左切片。從後往前數(索引從-1開始), type的作用:查看數據類型。
1 >>> name=‘My Name Is Ghostwu‘ 2 >>> name[-1] 3 ‘u‘ 4 >>> name[-1:-4]5 ‘‘ 6 >>> name[-1:-4:-1] 7 ‘uwt‘ 8 >>> type(name) 9 <type ‘str‘> 10 >>> name[2] 11 ‘ ‘ 12 >>> name[2:] 13 ‘ Name Is Ghostwu‘ 14 >>> name[2:-1] 15 ‘ Name Is Ghostw‘ 16 >>>
字符串其他小技巧:
>len函數,計算長度
>>> str="ghostwu" >>> len(str) 7
>+號,連接字符串
>>> str="hi " >>> str2="ghostwu" >>> str+str2 ‘hi ghostwu‘
>*號,重復字符串次數,是不是很簡潔,在php中要用str_repeat或者循環連接字符串
>>> str="ghostwu" >>> str*2 ‘ghostwughostwu‘ >>> str ‘ghostwu‘ >>>
>in: 判斷元素是否在序列中
>>> str="ghostwu" >>> ‘g‘ in str True >>> ‘x‘ in str False >>>
>max最大值,min最小值
>>> str="abc123" >>> max(str) ‘c‘ >>> min(str) ‘1‘ >>>
>cmp(str1,str2) 比較序列值是否相同
1 >>> str="abc" 2 >>> str2="ab1" 3 >>> cmp(str,str2) 4 1 5 >>> cmp(str2,str) 6 -1 7 >>> str2="abc" 8 >>> cmp(str,str2) 9 0 10 >>>
二、元組類型
用名稱=(item,item,)小括號定義,只有一項的時候,要加逗號
字符串的痛點:如果用一個字符串,保存某個人的信息,那麽在切片的時候(如人名,年齡,性別)就不太好操作
1 >>> userinfo="ghostwu 20 male" 2 >>> type(userinfo) 3 <type ‘str‘> 4 >>> userinfo[0:7] 5 ‘ghostwu‘
如果用元組來處理
1 >>> userinfo=("ghostwu","20","male") 2 >>> type(userinfo) 3 <type ‘tuple‘> 4 >>> userinfo[0] 5 ‘ghostwu‘ 6 >>> userinfo[1] 7 ‘20‘ 8 >>> userinfo[2] 9 ‘male‘ 10 >>>
看,是不是非常簡單?只有一項時?怎麽定義?
>>> userinfo=("ghostwu") >>> type(userinfo) <type ‘str‘> >>>
像上面這種定義方式,定義的是一個字符串類型。只有一項時,需要在後面加個逗號‘,‘
>>> userinfo=(‘ghostwu‘,) >>> type(userinfo) <type ‘tuple‘> >>> userinfo[0] ‘ghostwu‘ >>>
元組定義之後,不可以被修改:
>>> userinfo=("ghostwu",20,"male") >>> userinfo[0]="zhangsan" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: ‘tuple‘ object does not support item assignment >>>
可以使用類似es6的解構語法,把元組的每一項對應賦值給左邊的變量:
>>> userinfo=(‘ghostwu‘,20,‘male‘) >>> name,age,sex=userinfo >>> name ‘ghostwu‘ >>> age 20 >>> sex ‘male‘
三、列表(list)
>中括號定義
>>> list1=[] >>> type(list1) <type ‘list‘>
>>> userinfo=[‘ghostwu‘,20,‘male‘] >>> type(userinfo) <type ‘list‘> >>> userinfo[0] ‘ghostwu‘ >>> userinfo[1] 20 >>> userinfo[2] ‘male‘
>列表的切片操作
1 >>> userinfo=[‘ghostwu‘,20,‘male‘] 2 >>> userinfo[0:1] 3 [‘ghostwu‘] 4 >>> userinfo[0:2] 5 [‘ghostwu‘, 20] 6 >>> userinfo[::2] 7 [‘ghostwu‘, ‘male‘] 8 >>> userinfo[::] 9 [‘ghostwu‘, 20, ‘male‘] 10 >>> userinfo[::1] 11 [‘ghostwu‘, 20, ‘male‘]
>列表可以被重新賦值,列表項可以被修改,但是不能動態索引方式增加,否則報錯(索引超出上限)
1 >>> userinfo=[‘ghostwu‘,20,‘male‘] 2 >>> len(userinfo) 3 3 4 >>> userinfo=‘zhangsan‘ 5 >>> len(userinfo) 6 8 7 >>> userinfo=[] 8 >>> len(userinfo) 9 0 10 >>> userinfo[0]="ghostwu" 11 Traceback (most recent call last): 12 File "<stdin>", line 1, in <module> 13 IndexError: list assignment index out of range 14 >>> userinfo=["ghostwu",20,"male"] 15 >>> userinfo[0]="zhangsan" 16 >>> userinfo 17 [‘zhangsan‘, 20, ‘male‘] 18 >>> userinfo[3]="china" 19 Traceback (most recent call last): 20 File "<stdin>", line 1, in <module> 21 IndexError: list assignment index out of range 22 >>>
>列表操作:
取值:切片和索引 修改: list[] = x
>>> userinfo=[‘ghostwu‘,20,‘male‘] >>> type(userinfo) <type ‘list‘> >>> userinfo[0]... ‘ghostwu‘ >>> userinfo[0:2] [‘ghostwu‘, 20]
修改列表的某一項時候,地址沒有改變,還是列表本身
>>> userinfo=["ghostwu",20,"male"] >>> id(userinfo) 140648386293128 >>> userinfo[0]="hello" >>> id(userinfo) 140648386293128
元組重新被賦值,相當於重新定義了一個新的元組:
>>> userinfo=("ghostwu",20) >>> type(userinfo) <type ‘tuple‘> >>> userinfo[0]="hello" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: ‘tuple‘ object does not support item assignment >>> id(userinfo) 140648386125696 >>> userinfo=("zhangsan",30) >>> id(userinfo) 140648386125552
添加: list.append()
1 >>> userinfo=["ghostwu",20] 2 >>> userinfo 3 [‘ghostwu‘, 20] 4 >>> userinfo.append("male") 5 >>> userinfo 6 [‘ghostwu‘, 20, ‘male‘] 7 >>> userinfo.append("china") 8 >>> userinfo 9 [‘ghostwu‘, 20, ‘male‘, ‘china‘]
刪除: del(list[]) list.remove(list[]) , 註意:remove刪除的是列表中第一次出現的值
1 >>> userinfo 2 [‘ghostwu‘, 20, ‘male‘, ‘china‘] 3 >>> type(userinfo) 4 <type ‘list‘> 5 >>> userinfo.remove(20) 6 >>> userinfo 7 [‘ghostwu‘, ‘male‘, ‘china‘] 8 >>> userinfo.remove("china") 9 >>> userinfo 10 [‘ghostwu‘, ‘male‘]
>>> userinfo=[‘ghostwu‘,20,‘ghostwu‘,‘male‘,‘ghostwu‘] >>> userinfo [‘ghostwu‘, 20, ‘ghostwu‘, ‘male‘, ‘ghostwu‘] >>> userinfo.remove(‘ghostwu‘) >>> userinfo [20, ‘ghostwu‘, ‘male‘, ‘ghostwu‘]
1 >>> userinfo 2 [20, ‘ghostwu‘, ‘male‘, ‘ghostwu‘] 3 >>> type(userinfo) 4 <type ‘list‘> 5 >>> userinfo.remove(‘male‘) 6 >>> userinfo 7 [20, ‘ghostwu‘, ‘ghostwu‘] 8 >>> del( userinfo[1] ) 9 >>> userinfo 10 [20, ‘ghostwu‘]
查找: var in list
1 >>> userinfo 2 [20, ‘ghostwu‘] 3 >>> 20 in userinfo 4 True 5 >>> ‘20‘ in userinfo 6 False 7 >>> ‘ghostwu‘ in userinfo 8 True
四、字典
他的用法類似於javascript中的json,大括號中用鍵值對定義,取數據用對應的鍵
1 >>> userinfo={‘name‘:‘ghostwu‘, 1 : 20, ‘age‘ : 20, ‘sex‘ : ‘male‘ } 2 >>> type(userinfo) 3 <type ‘dict‘> 4 >>> userinfo 5 {1: 20, ‘age‘: 20, ‘name‘: ‘ghostwu‘, ‘sex‘: ‘male‘} 6 >>> userinfo[‘name‘] 7 ‘ghostwu‘ 8 >>> userinfo[‘age‘] 9 20 10 >>> userinfo[1] 11 20
字典中的鍵,可以是字符串,也可以是變量
1 >>> a=10 2 >>> b=20 3 >>> dic={a:‘ghostwu‘,‘b‘:‘male‘} 4 >>> dic 5 {10: ‘ghostwu‘, ‘b‘: ‘male‘} 6 >>> dic[10] 7 ‘ghostwu‘ 8 >>> dic[‘a‘]
用類似javascript的for ... in語法 遍歷字典:
1 >>> userinfo={‘name‘:‘ghostwu‘,‘age‘:20,‘sex‘:‘male‘} 2 >>> for key in userinfo: 3 ... print key 4 ... 5 age 6 name 7 sex 8 >>> for key in userinfo: 9 ... print userinfo[key] 10 ... 11 20 12 ghostwu 13 male 14 >>>
為字典增加一項值
1 >>> userinfo 2 {‘age‘: 20, ‘name‘: ‘ghostwu‘, ‘sex‘: ‘male‘} 3 >>> type(userinfo) 4 <type ‘dict‘> 5 >>> userinfo[‘email‘]=‘[email protected]‘ 6 >>> userinfo 7 {‘email‘: ‘[email protected]‘, ‘age‘: 20, ‘name‘: ‘ghostwu‘, ‘sex‘: ‘male‘}
字典相關操作方法: del可以刪除某一項,或者刪除整個字典,dict.clear()是清空整個字典. dict.pop( key ),刪除字典中對應的key和值,並返回被刪除的值
>>> userinfo {‘email‘: ‘[email protected]‘, ‘age‘: 20, ‘name‘: ‘ghostwu‘, ‘sex‘: ‘male‘} >>> type(userinfo) <type ‘dict‘> >>> userinfo[‘age‘]=30 >>> userinfo {‘email‘: ‘[email protected]‘, ‘age‘: 30, ‘name‘: ‘ghostwu‘, ‘sex‘: ‘male‘} >>> del(userinfo[‘age‘]) >>> userinfo {‘email‘: ‘[email protected]‘, ‘name‘: ‘ghostwu‘, ‘sex‘: ‘male‘} >>> userinfo.pop(‘email‘) ‘[email protected]‘ >>> userinfo {‘name‘: ‘ghostwu‘, ‘sex‘: ‘male‘} >>> userinfo.clear() >>> userinfo {} >>> del(userinfo) >>> userinfo Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name ‘userinfo‘ is not defined >>>
字典有很多的方法,比如:keys獲取所有的鍵,values:獲取所有的值
1 >>> userinfo={‘name‘:‘ghostwu‘,‘age‘:20,‘sex‘:‘male‘} 2 >>> userinfo.keys() 3 [‘age‘, ‘name‘, ‘sex‘] 4 >>> userinfo.values() 5 [20, ‘ghostwu‘, ‘male‘]
python數據類型:序列(字符串,元組,列表,字典)