Python基礎——基本數據類型2
阿新 • • 發佈:2018-08-12
不同 不同之處 追加 出現的次數 返回字典 for 全部 特性 peer
列表
定義:[ ]內以逗號分隔,按照索引,存放各種數據類型,每個位置代表一個元素
列表的創建
List1 = ["Hello", "Python", "China", "me"] # 或 List2 = list("Hello") # 或 List3 = list(["Hello", "Python", "China", "me"])列表的創建
訪問列表中的值
使用下標索引來訪問列表中的值,同樣你也可以使用方括號的形式截取字符,如下所示:
list1 = ["Hello", "Python", "China", "me"] list2 = list("訪問列表中的值Hello") print("list1[0]: ", list1[0]) # 結果是:list1[0]: Hello print("list2[1:3]: ", list2[1:3])# 結果是:list2[1:5]: [‘e‘, ‘l‘]
Python列表截取與拼接
Python的列表截取與字符串操作類型,如下所示:
list1 = ["Hello", "Python", "China", "me"] # 打印全部 print(list1[:]) # 結果是:[‘Hello‘, ‘Python‘, ‘China‘, ‘me‘] # 打印索引1以及索引1後面的元素Python列表截取與拼接print(list1[1:]) # 結果是:[‘Python‘, ‘China‘, ‘me‘] # 打印到索引2的元素包括索引2 print(list1[:2]) # 結果是:[‘Hello‘, ‘Python‘] # 列表拼接 list2 = ["new","list"] list1+=list2 print(list1) # 結果是:[‘Hello‘, ‘Python‘, ‘China‘, ‘me‘, ‘new‘, ‘list‘]
Python列表函數&方法
Python包含以下函數:
list1 = ["Hello", "PythonPython列表函數&方法", "China", "me"] # len(list) 列表元素個數 print(len(list1)) # 結果是:4 # max(list) 返回列表元素最大值 # 當進行字符串的排序的時候,Python是如何排序的呢 # 是進行比較id大小值 print(max(list1)) # 結果是:me print(id(list1[0])) # 結果是:2217808757344 print(id(list1[1])) # 結果是:2217808797344 print(id(list1[2])) # 結果是:2217809588832 print(id(list1[3])) # 結果是:2217778999400 # min(list) 返回列表元素最小值 print(min(list1)) # 結果是:China # list.append(obj) 在列表末尾添加新的對象 list1.append("new") # list1.append("new","new2")# 報錯:TypeError: append() takes exactly one argument (2 given),此方法只可以添加一個 print(list1) # 結果是:[‘Hello‘, ‘Python‘, ‘China‘, ‘me‘, ‘new‘] # list.count(obj) 統計某個元素在列表中出現的次數 print(list1.count("me")) # 結果是:1 # list.extend(seq) 在列表末尾一次性追加另一個序列中的多個值(用新列表擴展原來的列表) list1.extend(["new1", "new2"]) print(list1) # 結果是:[‘Hello‘, ‘Python‘, ‘China‘, ‘me‘, ‘new‘, ‘new1‘, ‘new2‘] # list.index(obj) 從列表中找出某個值第一個匹配項的索引位置 print(list1.index("me")) # 結果是:3 # print(list1.index("jhjj")) # 結果是:ValueError: ‘jhjj‘ is not in list,找不到會報錯 # list.insert(index, obj) 將對象插入列表 list1.insert(2, "new") print(list1) # 結果是:[‘Hello‘, ‘Python‘, ‘new‘, ‘China‘, ‘me‘, ‘new‘, ‘new1‘, ‘new2‘] # list.pop([index=-1]]) 移除列表中的一個元素(默認最後一個元素),並且返回該元素的值 print(list1.pop(2)) # 結果是:new print(list1) # 結果是:[‘Hello‘, ‘Python‘, ‘China‘, ‘me‘, ‘new‘, ‘new1‘, ‘new2‘] # list.remove(obj) 移除列表中某個值的第一個匹配項,無返回值 list1.remove("me") print(list1) # 結果是:[‘Hello‘, ‘Python‘, ‘China‘, ‘new‘, ‘new1‘, ‘new2‘] # list.reverse() 反向列表中元素 list1.reverse() print(list1) # 結果是:[‘new2‘, ‘new1‘, ‘new‘, ‘China‘, ‘Python‘, ‘Hello‘] # list.sort(cmp=None, key=None, reverse=False) 對原列表進行排序 # 降序 list1.sort(reverse=True) print(list1) # 結果是:[‘new2‘, ‘new1‘, ‘new‘, ‘Python‘, ‘Hello‘, ‘China‘] # 升序 list1.sort(reverse=False) print(list1) # 結果是:[‘China‘, ‘Hello‘, ‘Python‘, ‘new‘, ‘new1‘, ‘new2‘] # list.copy() 復制列表 list2 = list1.copy() print(list2) # 結果是:[‘China‘, ‘Hello‘, ‘Python‘, ‘new‘, ‘new1‘, ‘new2‘] # list.clear() 清空列表 list1.clear() print(list1) # 結果是:[]
元組
Python 的元組與列表類似,不同之處在於元組的元素不能修改。
元組使用小括號,列表使用方括號。
元組創建很簡單,只需要在括號中添加元素,並使用逗號隔開即可。
tup = (1,) tup1 = (1, 2, 3) tup2 = ("1", "2", "3") tup3 = tuple((1,2,3)) print(type(tup)) # 結果是:<class ‘tuple‘> print(type(tup1)) # 結果是:<class ‘tuple‘> print(type(tup2)) # 結果是:<class ‘tuple‘> print(type(tup3)) # 結果是:<class ‘tuple‘>元組創建
訪問元組
元組可以使用下標索引來訪問元組中的值,如下實例:
# tup1 = (‘China‘, ‘python‘, 2016, "0710") # tup2 = (1, 2, 3, 4, 5, 6, 7) # # print("tup1[0]: ", tup1[0]) # 結果是:tup1[0]: China # print("tup2[1:5]: ", tup2[1:5]) # 結果是:tup2[1:5]: (2, 3, 4, 5)訪問元組
元組內置函數
Python元組包含了以下內置函數
tup1 = (‘China‘, ‘python‘, "2016", "0710") tup2 = (1,) # len(tuple) 計算元組元素個數。 print(len(tup1)) # 結果是:4 print(len(tup2)) # 結果是:1 # max(tuple) 返回元組中元素最大值。 print(max(tup1)) # 結果是:python # min(tuple) 返回元組中元素最小值。 print(min(tup1)) # 結果是:0710 # tuple.index(obj) 從元組中找出某個值第一個匹配項的索引位置 tup1.index("0710") # tup1.count() 從元組中找出元素出現的次數 print(tup1.count("China")) # 結果是:1元組內置函數
字典
字典是另一種可變容器模型,且可存儲任意類型對象。
字典的每個鍵值(key=>value)對用冒號(:)分割,每個對之間用逗號(,)分割,整個字典包括在花括號({})中 ,格式如下所示:
dict1 = {"key1": "value1", "key2": "value2"} # 或 dict2 = dict(key1="value1", key2="value2") # 或 dict3 = dict({"key1": "value1", "key2": "value2"}) seq = (‘name‘, ‘age‘, ‘sex‘) dict = dict.fromkeys(seq) print("新的字典為 : %s" % str(dict)) # 結果是:新的字典為 : {‘age‘: None, ‘name‘: None, ‘sex‘: None} dict = dict.fromkeys(seq, 10) print("新的字典為 : %s" % str(dict)) # 結果是:新的字典為 : {‘age‘: 10, ‘name‘: 10, ‘sex‘: 10}字典的創建
訪問字典裏的值
把相應的鍵放入到方括號中,如下實例:
dict1 = {"key1": "value1", "key2": "value2"} print(dict1["key1"]) # 結果是:value1訪問字典值
字典鍵的特性
字典值可以是任何的 python 對象,既可以是標準的對象,也可以是用戶定義的,但鍵不行。
兩個重要的點需要記住:
1)不允許同一個鍵出現兩次。創建時如果同一個鍵被賦值兩次,後一個值會被記住,如下實例:
dict = {"name":"Ojia","age":"22","name":"夜市街"} print(dict["name"])# 結果是:夜市街
2)鍵必須不可變,所以可以用數字,字符串或元組充當,而用列表就不行,如下實例:
dict = {["name"]:"Ojia","age":"22"} print(dict["name"]) # 結果是:TypeError: unhashable type: ‘list‘
字典內置函數&方法
Python字典包含了以下內置函數:
dict = {"name": "Ojia", "age": "22", "sex": "boy"} # len(dict) 計算字典元素個數,即鍵的總數。 print(len(dict)) # 結果是:3 # str(dict) 輸出字典,以可打印的字符串表示。 print(str(dict)) # 結果是:{‘name‘: ‘Ojia‘, ‘age‘: ‘22‘, ‘sex‘: ‘boy‘} # type(variable) 返回輸入的變量類型,如果變量是字典就返回字典類型。 print(type(dict)) # 結果是:<class ‘dict‘> # dict.copy() 返回一個字典的淺復制 dict2 = dict.copy() print(dict2) # 結果是:{‘name‘: ‘Ojia‘, ‘age‘: ‘22‘, ‘sex‘: ‘boy‘} # dict.keys() 返回一個包含字典所有KEY的列表 dict_list = dict.keys() print(dict_list) # 結果是:dict_keys([‘name‘, ‘age‘, ‘sex‘]) # dict.values() 返回一個包含字典所有values的列表 dict_values = dict.values() print(dict_values) # 結果是:dict_values([‘Ojia‘, ‘22‘, ‘boy‘]) # dict.items() 返回一個包含所有鍵值對的元組的列表 list1 = dict.items() print(list1) # 結果是:dict_items([(‘name‘, ‘Ojia‘), (‘age‘, ‘22‘), (‘sex‘, ‘boy‘)]) # 增加可以使用這個語法 dict["new_key"] = "new_values" print(dict) # 結果是:{‘name‘: ‘Ojia‘, ‘age‘: ‘22‘, ‘sex‘: ‘boy‘, ‘new_key‘: ‘new_values‘} # 修改 dict["sex"] = "girl" print(dict) # 結果是:{‘name‘: ‘Ojia‘, ‘age‘: ‘22‘, ‘sex‘: ‘girl‘, ‘new_key‘: ‘new_values‘} dict2 = {"new_key2": "new_value2"} dict.update( dict2) # 結果是:{‘name‘: ‘Ojia‘, ‘age‘: ‘22‘, ‘sex‘: ‘girl‘, ‘new_key‘: ‘new_values‘, ‘new_key2‘: ‘new_value2‘} print(dict) # 查看 print(dict.get("name")) # 結果是:Ojia # 循環 for key in dict: print(key, dict[key]) """結果 name Ojia age 22 sex girl new_key new_values new_key2 new_value2 """ for key, value in dict.items(): print(key + ":" + value) """結果 name:Ojia age:22 sex:girl new_key:new_values new_key2:new_value2 """ # 刪除,如果pop的值不在的話會報錯 dict.pop("sex") print(dict) # 結果是:{‘name‘: ‘Ojia‘, ‘age‘: ‘22‘, ‘new_key‘: ‘new_values‘} # 刪除全部元素 dict.clear()字典內置函數&方法
Python基礎——基本數據類型2