#數據結構(Data Structure)
#存儲大量數據的容器---->內置數據結構(Bulit-in Structure)
#概念基于現實生活中的原型
'''
#1.列表
list = [val1,val2,val3,val4]
#2.字典
dict = {key1:val1, key2:val2} #均帶有’:‘的key與value的對應關系組
#3.元組
tuple = (val1,val2,val3,val4)
#4.集合
set = {val1,val2,val3,val4}
元素均需要加逗號分開
'''
##1.列表(List)
最顯著的特征
A.每一個元素都是可變的
B.列表中的元素是有序的,咱就是說 每一個元素都有一個自己的位置
C.列表可以容納Python中的任何對象
#Demo1 通過輸入位置查詢該位置所對應的值
Weekday = ['Monday','Tuesday','Wednesday','Thursday','Friday']
print(Weekday[0])
#Demo2 列表可以輸入Python中所有對象
all_in_list = [
? ? 1,? ? ? ? ? ? ? ? ? #整數? ??
????1.0,? ? ? ? ? ? ? ? #浮點數
? ? 'a word',? ? ? ? ? ? #字符串
? ? print(1),? ? ? ? ? ? #函數
? ? True,? ? ? ? ? ? ? ? #布爾值
? ? [1,2],? ? ? ? ? ? ? ? #列表? ??
????(1,2),? ? ? ? ? ? ? #元組
? ? {'Key':'value'}? ? ? #字典
]
#Demo3 列表的增刪改查
#a.增加:insert方法
fruit = ['Pineapple','Pear']
fruit.insert(1,'Grape') #insert方法,插入位置在指定位置之前
fruit.insert(3,'Orange') #insert方法,插入位置不存在則放在最后
fruit[0:0] = ['Orange']? #另一種插入方法
print(fruit)
#b.刪除:remove方法
fruit = ['Pineapple','Pear','Orange']
fruit.remove('Orange')
print(fruit)
del fruit[0:2] #除remove外另一種方法
print(fruit)
#c.改變:fruit = ['Pineapple','Pear','Orange']
fruit[0] = 'Strawberry' #指定列表位置和新的元素
print(fruit)
#d.查找:列表的索引
#輸入對應的位置就會返回在這個位置上的值
periodic_table = ['H','He','Li','Be','B','C','N','O','F','Ne']
print(periodic_table[0])
print(periodic_table[-2])
print(periodic_table[0:3])
print(periodic_table[-10:-7])
print(periodic_table[-10:])# 等價于[-10:0]
print(periodic_table[:9]) #等價于[0:9]
print(periodic_table['H']) #報錯(X) 列表僅能接受數字索引#
#列表也是一種數據結構
##2.字典(Dictionary)
#使用 名稱-內容 進行數據構建----> 鍵(Key)——值(Value)(鍵值對)
#映射關系(mapping)
最顯著的特征
A.字典中的數據以鍵值對[(Key)-(value)]的形式出現
B.邏輯上:鍵不能重復,值可以重復
C.鍵不可改變,無法修改;值可以改變,可以修改,可以是任何對象
#Demo 4 字典的特點
NASDAQ_code = {
????'BIDU':'BaiDu',
????'SINA':'Sina',
????'YOKU':'YouKu',
????'BIDU': #一個字典中的鍵與值不能脫離對方存在
? ? ?[]:'a Test' #可變元素做鍵是不可以的}
????a = {'key':123,'key':123}
????print(a)
#Key 和 value是一一對應的,Key是不可變的
#鍵值對不能有重復,重復后相同的鍵值只能出現一次。
#Demo5 字典的增刪改查
NASDAQ_code = {'BIDU':'Baidu','SINA':'Sina'}
#a.增加:使用update方法
NASDAQ_code['YOKU'] = 'YouKu'? #增加單個元素
print(NASDAQ_code)
NASDAQ_code.update({'FB':'Facebook','TSLA':'Tesla'})#增加多個元素update
print(NASDAQ_code)
#b.刪除:使用del方法
del? NASDAQ_code['FB'] #刪除使用del
#c.查找--->索引(index)
NASDAQ_code['TSLA'] #字典和列表在索引的時候是一樣的,都是使用的方括號
#d.其他
NASDAQ_code[1:4] #(X錯誤寫法)字典不能夠切片
##3.元組(Truple)
#元組可視為一個不可修改的列表
#元組使用小括號
#沒有增刪不能改,只能進行索引
Letters = ('a','b','c','d','e','f','g')??
print(Letters[0])
##4.集合(Set)
#元素:無序、不重復、任意
#集合屬性:添加、刪除
a_set = {1,2,3,4}
add = a_set.add(5)
print(a_set)
delete = a_set.discard(5)
print(a_set)
##總結
根據以上信息,整理出四種數據類型的屬性圖: