#數(shù)據(jù)結(jié)構(gòu)(Data Structure)
#存儲(chǔ)大量數(shù)據(jù)的容器---->內(nèi)置數(shù)據(jù)結(jié)構(gòu)(Bulit-in Structure)
#概念基于現(xiàn)實(shí)生活中的原型
'''
#1.列表
list = [val1,val2,val3,val4]
#2.字典
dict = {key1:val1, key2:val2} #均帶有’:‘的key與value的對應(yīng)關(guān)系組
#3.元組
tuple = (val1,val2,val3,val4)
#4.集合
set = {val1,val2,val3,val4}
元素均需要加逗號(hào)分開
'''
##1.列表(List)
最顯著的特征
A.每一個(gè)元素都是可變的
B.列表中的元素是有序的,咱就是說 每一個(gè)元素都有一個(gè)自己的位置
C.列表可以容納Python中的任何對象
#Demo1 通過輸入位置查詢該位置所對應(yīng)的值
Weekday = ['Monday','Tuesday','Wednesday','Thursday','Friday']
print(Weekday[0])
#Demo2 列表可以輸入Python中所有對象
all_in_list = [
? ? 1,? ? ? ? ? ? ? ? ? #整數(shù)? ??
????1.0,? ? ? ? ? ? ? ? #浮點(diǎn)數(shù)
? ? 'a word',? ? ? ? ? ? #字符串
? ? print(1),? ? ? ? ? ? #函數(shù)
? ? 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.查找:列表的索引
#輸入對應(yīng)的位置就會(huì)返回在這個(gè)位置上的值
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:])# 等價(jià)于[-10:0]
print(periodic_table[:9]) #等價(jià)于[0:9]
print(periodic_table['H']) #報(bào)錯(cuò)(X) 列表僅能接受數(shù)字索引#
#列表也是一種數(shù)據(jù)結(jié)構(gòu)

##2.字典(Dictionary)
#使用 名稱-內(nèi)容 進(jìn)行數(shù)據(jù)構(gòu)建----> 鍵(Key)——值(Value)(鍵值對)
#映射關(guān)系(mapping)
最顯著的特征
A.字典中的數(shù)據(jù)以鍵值對[(Key)-(value)]的形式出現(xiàn)
B.邏輯上:鍵不能重復(fù),值可以重復(fù)
C.鍵不可改變,無法修改;值可以改變,可以修改,可以是任何對象
#Demo 4 字典的特點(diǎn)
NASDAQ_code = {
????'BIDU':'BaiDu',
????'SINA':'Sina',
????'YOKU':'YouKu',
????'BIDU': #一個(gè)字典中的鍵與值不能脫離對方存在
? ? ?[]:'a Test' #可變元素做鍵是不可以的}
????a = {'key':123,'key':123}
????print(a)
#Key 和 value是一一對應(yīng)的,Key是不可變的
#鍵值對不能有重復(fù),重復(fù)后相同的鍵值只能出現(xiàn)一次。
#Demo5 字典的增刪改查
NASDAQ_code = {'BIDU':'Baidu','SINA':'Sina'}
#a.增加:使用update方法
NASDAQ_code['YOKU'] = 'YouKu'? #增加單個(gè)元素
print(NASDAQ_code)
NASDAQ_code.update({'FB':'Facebook','TSLA':'Tesla'})#增加多個(gè)元素update
print(NASDAQ_code)
#b.刪除:使用del方法
del? NASDAQ_code['FB'] #刪除使用del
#c.查找--->索引(index)
NASDAQ_code['TSLA'] #字典和列表在索引的時(shí)候是一樣的,都是使用的方括號(hào)
#d.其他
NASDAQ_code[1:4] #(X錯(cuò)誤寫法)字典不能夠切片
##3.元組(Truple)
#元組可視為一個(gè)不可修改的列表
#元組使用小括號(hào)
#沒有增刪不能改,只能進(jìn)行索引
Letters = ('a','b','c','d','e','f','g')??
print(Letters[0])
##4.集合(Set)

#元素:無序、不重復(fù)、任意
#集合屬性:添加、刪除
a_set = {1,2,3,4}
add = a_set.add(5)
print(a_set)
delete = a_set.discard(5)
print(a_set)
##總結(jié)
根據(jù)以上信息,整理出四種數(shù)據(jù)類型的屬性圖:
