列表
列表是Python中最基本的數據結構。列表中的每個元素都分配一個數字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推。
Python有6個列表的內置類型,但最常見的是列表和元組。
序列都可以進行的操作包括索引,切片,加,乘,檢查成員。
此外,Python已經內置確定列表的長度以及確定最大和最小的元素的方法。
列表是最常用的Python數據類型,它可以作爲一個方括號內的逗號分隔值出現。
列表的數據項不需要具有相同的類型。
list = [('a', 'A'), ('b', 'B'), ('c', 'C')]
for x in list:
print(x)
for x, y in list:
print(x, y)
枚舉:
enumerate 枚舉 : 可以讓被遍歷元素 添加一個編號(索引值)
for後面的第一個參數 即爲索引值 第二個參數 爲被遍歷的元素
for x, y in enumerate(list):
print(x, y)
for x, (y, z) in enumerate(list):
print(x, y, z)
list = [(1, [2, 3]), (4, [5, 6]), (7, [8, 9])]
for index, (x, [y, z]) in enumerate(list):
print(index, x, y, z)
extend函數列表和字符串一樣可以進行拼接;
extend 將被合併的集合的所有值給 主動進行合併的集合
最終結果爲 兩個集合的元素個數的綜合
它和append類似但又有不同;
append 將被合併的集合作爲一個整體給 主動進行合併的集合
最終結果爲 主動進行合併的集合元素個數等於之前的個數+1
list1 = ['a', 'b', 'c']
list2 = ['d', 'e', 'f']
list3 = list1 + list2
print(list3)
# extend 擴展,添加
list1.extend(list2)
print(list1)
startswith方法
startswith() 方法用於檢查字符串是否是以指定子字符串開頭,如果是則返回 True,否則返回 False。如果參數 beg 和 end 指定值,則在指定範圍內檢查。
list = ['張三', '張飛', '張良', '趙子龍', '關雲長']
list5 = [x for x in list if x.startswith('張')]
print(list5)
reverse函數reverse() 函數用於反向列表中元素。
list = ['1', '2', '3']
# reverse() 相反的
list.reverse()
print(list)
sum函數sum() 方法對系列進行求和計算。
list = [1, 2 ,3 ,4 ,5,6,7,8,9,10]
some = 0
for x in list:
some += x
print(some)
result = sum(list)
print(result)
sorted 函數
sorted() 函數對所有可迭代的對象進行排序操作。
sort 與 sorted 區別:
sort 是應用在 list 上的方法,sorted 可以對所有可迭代的對象進行排序操作。
list 的 sort 方法返回的是對已經存在的列表進行操作,而內建函數 sorted 方法返回的是一個新的 list,而不是在原來的基礎上進行的操作。
獲取正序:
list = [2, 3, 1, 5, 7, 10, 9, 8, 6, 4]
list2 = sorted(list)
print(list2)
獲取反序:
list = [2, 3, 1, 5, 7, 10, 9, 8, 6, 4]
# revsrse 倒序 默認值爲Flase
list2 = sorted(list, reverse=True)
print(list2)
列表元素的索引與遍歷
list = [
[
['a'], # [0][0][0]
['b'] # [0][1][0]
],
[
['c'], # [1][0][0]
['d'] # [1][1][0]
],
[
['e'], # [2][0][0]
['f'] # [2][1][0]
]
]
print(list[2][0][0])
list1 = [[1], [2], [3],[4],[5]]
for x in list1:
for y in x:
print(y)
兩種方法遍歷:
list = [
[
['a'],
['b']
],
[
['c'],
['d']
],
[
['e'],
['f']
]
]
# 方法1:
for x in list:
for y in x:
for z in y:
print(z)
# 方法2:
for x , [[y],[z]] in enumerate(list):
print(y,z)
元組
Python的元組與列表類似,不同之處在於元組的元素不能修改。元組使用小括號,列表使用方括號。
元組創建很簡單,只需要在括號中添加元素,並使用逗號隔開即可。
創建元組的兩種方式:
tp1 = ()
tp2 = tuple()
如果元組在創建的時候沒有放入元素那麼這個元組就沒有意義
元組和列表的區別:
列表可以任意進行增刪改查等操作
元組只可以進行 查 操作
tp = ('a','b','c','d')
if 'a' in tp:
print('存在')
else :
print('不存在')
len函數
Python len() 方法返回對象(字符、列表、元組等)長度或項目個數。
tp = ('a','b','c','d')
print(len(tp))
字典
字典是另一種可變容器模型,且可存儲任意類型對象。
字典的每個鍵值(key=>value)對用冒號(:)分割,每個對之間用逗號(,)分割,整個字典包括在花括號({})中 。
鍵必須是唯一的,但值則不必。
值可以取任何數據類型,但鍵必須是不可變的,如字符串,數字或元組。
聲明字典兩種方式:
dic1 = {}
dic2 = dict()
獲取字典的元素:
dic3 = {
'name':'小明',
'age':'18',
# True代表男還是女 是你自己定義的
'sex':True,
'height':1.76,
'fond':['打遊戲','學習','寫代碼','寫博客']
}
print(dic3)
print('愛好:%s'%dic3['fond'])
修改字典的元素:dic3 = {
'name': '小明',
'age': '18',
# True代表男還是女 是你自己定義的
'sex': True,
'height': 1.76,
'fond': ['打遊戲', '學習', '寫代碼', '寫博客']
}
# 有的話 修改;沒有的話 則增加
dic3['age'] = 18
print(dic3['age'])
dic3['girlFriend'] = '奶茶妹'
print(dic3)
#刪除girlFriend
dic3.pop('girlFriend')
print(dic3)
獲取字典中的鍵和值:dic3 = {
'name': '小明',
'age': '18',
'sex': True,
'height': 1.76,
'fond': ['打遊戲', '學習', '寫代碼', '寫博客']
}
print(dic3.keys())
print(dic3.values())
判斷字典中的鍵是否存在:
dic3 = {
'name': '小明',
'age': '18',
'sex': True,
'height': 1.76,
'fond': ['打遊戲', '學習', '寫代碼', '寫博客']
}
if 'fonds' in dic3.keys():
print('fonds存在')
else:
print('fonds不存在')
判斷字典中的值是否存在:
dic3 = {
'name': '小明',
'age': '18',
'sex': True,
'height': 1.76,
'fond': ['打遊戲', '學習', '寫代碼', '寫博客']
}
if ['打遊戲', '學習', '寫代碼', '寫博客'] in dic3.values():
print('存在')
else:
print('不存在')
獲取key和value值:有兩種方法可以獲取key和value值;
dic4 = {
'name' : '小蘭',
'age': '12',
'fond':'美食',
'info':{
'description':'很好的一個人',
'phone':'123123123',
'frend':[
{
'frend_name':'小明',
'frend_age':12
},
{
'frend_name':'小王',
'frend_age':12
},
{
'frend_name':'小張',
'frend_age':12
},
{
'frend_name':'小沈',
'frend_age':12
}
]
}
}
# 獲取key和value值的第一種方法
for key in dic4:
value = dic4[key]
print(key,value)
# 獲取key和value的第二種方法 (推薦這種寫法)
for key , value in dic4.items():
print(key,value)
如果獲取一個沒有的key會報錯。
獲取key對應的值 如果沒有key 則賦予一個默認值
# 值1:key
# 值2:默認值
value = dic4.get('class','三年級二班')
print(value)