Directory:git
1.List, tuple actionapi
2.String operations服務器
3.Dictionary operationsapp
4.Setide
5.File operationsthis
6.Character encoding and transcoding編碼
----------------------------------------------------------------------spa
1.List and Tuplescode
names = ['Alex',"Tenglan",'Eric'] >>> names[0] 'Alex' >>> names[2] 'Eric' >>> names[-1] 'Eric' >>> names[-2] 'Tenglan'
Lists are modifiable (or 'mutable', as a programmer may say), so their values can be changed. Most of the time we use lists, not tuples, because we want to easily change the values of things if we need to.orm
Slice: take multiple elements
>>> names = ["Alex","Tenglan","Eric","Rain","Tom","Amy"] >>> names[1:4] #取下標1至下標4之間的數字,包括1,不包括4 ['Tenglan', 'Eric', 'Rain'] >>> names[1:-1] #取下標1至-1的值,不包括-1 ['Tenglan', 'Eric', 'Rain', 'Tom'] >>> names[0:3] ['Alex', 'Tenglan', 'Eric'] >>> names[:3] #若是是從頭開始取,0能夠忽略,跟上句效果同樣 ['Alex', 'Tenglan', 'Eric'] >>> names[3:] #若是想取最後一個,必須不能寫-1,只能這麼寫 ['Rain', 'Tom', 'Amy'] >>> names[3:-1] #這樣-1就不會被包含了 ['Rain', 'Tom'] >>> names[0::2] #後面的2是表明,每隔一個元素,就取一個 ['Alex', 'Eric', 'Tom'] >>> names[::2] #和上句效果同樣 ['Alex', 'Eric', 'Tom']
Adding items to a list:
append:
>>> names ['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy'] >>> names.append("new_name") >>> names ['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', 'new_name']
insert:
>>> names ['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', '我是新來的'] >>> names.insert(2,"強行從Eric前面插入") >>> names ['Alex', 'Tenglan', '強行從Eric前面插入', 'Eric', 'Rain', 'Tom', 'Amy', '我是新來的'] >>> names.insert(5,"從eric後面插入試試新姿式") >>> names ['Alex', 'Tenglan', '強行從Eric前面插入', 'Eric', 'Rain', '從eric後面插入試試新姿式', 'Tom', 'Amy', '我是新來的']
Modify tuples
>>> names ['Alex', 'Tenglan', 'Winter', 'Eric', 'Rain', 'vince', 'Tom', 'Amy', 'new_name'] >>> names[2] = "new_Winter" >>> names ['Alex', 'Tenglan', 'new_Winter', 'Eric', 'Rain', 'vince', 'Tom', 'Amy', 'new_name']
Deleting an item
>>> del names[2] >>> names ['Alex', 'Tenglan', 'Eric', 'Rain', 'vince', 'Tom', 'Amy', 'new_name'] >>> del names[4] >>> names ['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', 'new_name'] >>> >>> names.remove("Eric") >>> names ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 'new_name'] >>> names.pop() #delete the last element(default) 'new_name' >>> names ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy']
extend
>>> names ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy'] >>> b = [1,2,3] >>> names.extend(b) >>> names ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]
copy
>>> names ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3] >>> name_copy = names.copy() >>> name_copy ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]
count
>>> names ['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3] >>> names.count("Amy") 2
sort and reverse
>>> names ['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3] >>> names.sort() #sort Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: int() < str() >>> names[-3] = '1' >>> names[-2] = '2' >>> names[-1] = '3' >>> names ['Alex', 'Amy', 'Amy', 'Tenglan', 'Tom', '1', '2', '3'] >>> names.sort() >>> names ['1', '2', '3', 'Alex', 'Amy', 'Amy', 'Tenglan', 'Tom'] >>> names.reverse() #reverse >>> names ['Tom', 'Tenglan', 'Amy', 'Amy', 'Alex', '3', '2', '1']
To obtain the subscript
>>> names ['Tom', 'Tenglan', 'Amy', 'Amy', 'Alex', '3', '2', '1'] >>> names.index("Amy") 2
-------------------------------------------------------------------------------------------------
2.String
Features:unmodifiable
name.capitalize() 首字母大寫 name.casefold() 大寫所有變小寫 name.center(50,"-") 輸出 '---------------------Alex Li----------------------' name.count('lex') 統計 lex出現次數 name.encode() 將字符串編碼成bytes格式 name.endswith("Li") 判斷字符串是否以 Li結尾 "Alex\tLi".expandtabs(10) 輸出'Alex Li', 將\t轉換成多長的空格 name.find('A') 查找A,找到返回其索引, 找不到返回-1 format : >>> msg = "my name is {}, and age is {}" >>> msg.format("alex",22) 'my name is alex, and age is 22' >>> msg = "my name is {1}, and age is {0}" >>> msg.format("alex",22) 'my name is 22, and age is alex' >>> msg = "my name is {name}, and age is {age}" >>> msg.format(age=22,name="ale") 'my name is ale, and age is 22' format_map >>> msg.format_map({'name':'alex','age':22}) 'my name is alex, and age is 22' msg.index('a') 返回a所在字符串的索引 '9aA'.isalnum() True '9'.isdigit() 是否整數 name.isnumeric name.isprintable name.isspace name.istitle name.isupper "|".join(['alex','jack','rain']) 'alex|jack|rain' maketrans >>> intab = "aeiou" #This is the string having actual characters. >>> outtab = "12345" #This is the string having corresponding mapping character >>> trantab = str.maketrans(intab, outtab) >>> >>> str = "this is string example....wow!!!" >>> str.translate(trantab) 'th3s 3s str3ng 2x1mpl2....w4w!!!' msg.partition('is') 輸出 ('my name ', 'is', ' {name}, and age is {age}') >>> "alex li, chinese name is lijie".replace("li","LI",1) 'alex LI, chinese name is lijie' msg.swapcase 大小寫互換 >>> msg.zfill(40) '00000my name is {name}, and age is {age}' >>> n4.ljust(40,"-") 'Hello 2orld-----------------------------' >>> n4.rjust(40,"-") '-----------------------------Hello 2orld' >>> b="ddefdsdff_哈哈" >>> b.isidentifier() #檢測一段字符串能否被看成標誌符,便是否符合變量命名規則 True
3.Dictionary
info = { 'stu1101': "TengLan Wu", 'stu1102': "LongZe Luola", 'stu1103': "XiaoZe Maliya", }
字典的特性:
add
>>> info["stu1104"] = "蒼井空" >>> info {'stu1102': 'LongZe Luola', 'stu1104': '蒼井空', 'stu1103': 'XiaoZe Maliya', 'stu1101': 'TengLan Wu'}
modify
>>> info['stu1101'] = "武藤蘭" >>> info {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤蘭'}
delete
>>> info {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤蘭'} >>> info.pop("stu1101") #標準刪除姿式 '武藤蘭' >>> info {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} >>> del info['stu1103'] #換個姿式刪除 >>> info {'stu1102': 'LongZe Luola'} >>> >>> >>> >>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} >>> info {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} #隨機刪除 >>> info.popitem() ('stu1102', 'LongZe Luola') >>> info {'stu1103': 'XiaoZe Maliya'}
查找
>>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} >>> >>> "stu1102" in info #標準用法 True >>> info.get("stu1102") #獲取 'LongZe Luola' >>> info["stu1102"] #同上,可是看下面 'LongZe Luola' >>> info["stu1105"] #若是一個key不存在,就報錯,get不會,不存在只返回None Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'stu1105'
多級字典嵌套及操做
av_catalog = { "歐美":{ "www.youporn.com": ["不少免費的,世界最大的","質量通常"], "www.pornhub.com": ["不少免費的,也很大","質量比yourporn高點"], "letmedothistoyou.com": ["可能是自拍,高質量圖片不少","資源很少,更新慢"], "x-art.com":["質量很高,真的很高","所有收費,屌比請繞過"] }, "日韓":{ "tokyo-hot":["質量怎樣不清楚,我的已經不喜歡日韓範了","據說是收費的"] }, "大陸":{ "1024":["所有免費,真好,好人一輩子平安","服務器在國外,慢"] } } av_catalog["大陸"]["1024"][1] += ",能夠用爬蟲爬下來" print(av_catalog["大陸"]["1024"]) #ouput ['所有免費,真好,好人一輩子平安', '服務器在國外,慢,能夠用爬蟲爬下來']
其它
#values >>> info.values() dict_values(['LongZe Luola', 'XiaoZe Maliya']) #keys >>> info.keys() dict_keys(['stu1102', 'stu1103']) #setdefault >>> info.setdefault("stu1106","Alex") 'Alex' >>> info {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'} >>> info.setdefault("stu1102","龍澤蘿拉") 'LongZe Luola' >>> info {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'} #update >>> info {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'} >>> b = {1:2,3:4, "stu1102":"龍澤蘿拉"} >>> info.update(b) >>> info {'stu1102': '龍澤蘿拉', 1: 2, 3: 4, 'stu1103': 'XiaoZe Maliya', 'stu1106': 'Alex'} #items info.items() dict_items([('stu1102', '龍澤蘿拉'), (1, 2), (3, 4), ('stu1103', 'XiaoZe Maliya'), ('stu1106', 'Alex')]) #經過一個列表生成默認dict,有個沒辦法解釋的坑,少用吧這個 >>> dict.fromkeys([1,2,3],'testd') {1: 'testd', 2: 'testd', 3: 'testd'}
循環dict
#方法1 for key in info: print(key,info[key]) #方法2 for k,v in info.items(): #會先把dict轉成list,數據裏大時莫用 print(k,v)