python 內(nèi)置數(shù)據(jù)結(jié)構(gòu)list丹擎、set尾抑、dict、tuple(三)

關(guān)于元組的函數(shù)

  • 以下看代碼
  • 以下函數(shù)鸥鹉,對(duì)list基本適用
# len:獲取元組的長(zhǎng)度
t = (1,2,3,4,5)
len(t)
5
# max蛮穿,min:最大最小值
print(max(t))
print(min(t))
5
1
# tuple:轉(zhuǎn)化或創(chuàng)建元組
l = (1,2,3,4,5)
t = tuple(l)
print(t)

t = tuple()
print(t)
(1, 2, 3, 4, 5)
()

元組的函數(shù)

  • 基本跟list通用
# count:計(jì)算指定數(shù)據(jù)出現(xiàn)的次數(shù)
t = (2,1,2,3,45,1,1,2,)

print(t.count(2))

# index:求指定元素在元組中的索引位置

print(t.index(45))
# 如果需要的查找的數(shù)字是多個(gè)庶骄,則返回第一個(gè)

print(t.index(1))
3
4
1

元組變量交換法

  • 兩個(gè)變量交換值
# 兩個(gè)變量交換值
a = 1
b = 3

print(a)
print(b)
print("*" * 20)
# java程序員會(huì)這么寫:
c = a
a = b
b = c
print(a)
print(b)

print("*" * 20)
# python寫法
a,b = b,a
print(a)
print(b)
1
3
********************
3
1
********************
1
3

集合-set

  • 集合是高中數(shù)學(xué)中的一個(gè)概念
  • 一堆確定的無序的唯一的數(shù)據(jù)毁渗,集合中每一個(gè)數(shù)據(jù)成為一個(gè)元素
# 集合的定義
s = set()
print(type(s))
print(s)

# 此時(shí),大括號(hào)內(nèi)一定要有值单刁,否則定義出的是一個(gè)dict
s = {1,2,3,4,5,6,7}
print(type(s))
print(s)
<class 'set'>
set()
<class 'set'>
{1, 2, 3, 4, 5, 6, 7}
# 如果只是用大括號(hào)定義灸异,則定義的是一個(gè)dict類型
d = {}
print(type(d))
print(d)
<class 'dict'>
{}

集合的特征

  • 集合的數(shù)據(jù)無序府适,即無法使用索引和分片
  • 集合內(nèi)部數(shù)據(jù)元素具有唯一性,可以用來排除重復(fù)數(shù)據(jù)
  • 集合內(nèi)的數(shù)據(jù)肺樟,str檐春,int,float么伯,tuple疟暖,冰凍集合等,即內(nèi)部只能放置可哈希數(shù)據(jù)

集合序列操作

# 成員檢測(cè)
# in田柔,not in
s = {4,5,"i", "love", "you"}
print(s)

if "love" in s:
    print("Yes")
    
if "haha" not in s:
    print("Yes")
{'you', 4, 5, 'love', 'i'}
Yes
Yes

集合遍歷操作

# for 循環(huán)
s = {4,5,"i", "love", "you"}

for i in s:
    print(i)
you
4
5
love
i
# 帶有元組的集合遍歷
s = {(1,2,3,), ("i", "love", "you"), (4,5,6)}

for k,m,n in s:
    print(k, "--", m, "--", n)
    
for k in s:
    print(k)
i -- love -- you
4 -- 5 -- 6
1 -- 2 -- 3
('i', 'love', 'you')
(4, 5, 6)
(1, 2, 3)

集合的內(nèi)涵

# 普通集合內(nèi)涵
# 以下集合在初始化后自動(dòng)過濾掉重復(fù)元素
s = {23,223,233,2,4,5,6,3,4,1,5,3}
print(s)

# 普通集合內(nèi)涵
ss = {i for i in s}
print(ss)
{1, 2, 3, 4, 5, 6, 233, 23, 223}
{1, 2, 3, 4, 5, 6, 233, 23, 223}
# 帶條件的集合內(nèi)涵
sss = {i for i in s if i % 2 == 0}
print(sss)
{2, 4, 6}
# 多循環(huán)的集合內(nèi)涵
s1 = {1,2,3,4}
s2 = {"i", "love", "you"}

s = {m*n for m in s2 for n in s1}
print(s)

s = {m*n for m in s2 for n in s1 if n == 2}
print(s)
{'you', 'youyou', 'love', 'lovelovelovelove', 'lovelovelove', 'lovelove', 'iii', 'youyouyouyou', 'ii', 'i', 'iiii', 'youyouyou'}
{'lovelove', 'youyou', 'ii'}

集合函數(shù)/關(guān)于集合的函數(shù)

# len俐巴,max,min:跟其他基本的函數(shù)一致
s = {23,54,72,3,5,3,3,6,1,543}
print(len(s))
print(max(s))
print(min(s))
8
543
1
# set:生成一個(gè)集合
l = {1,2,3,4,5,4,3,2,1}
s = set(l)
print(s)
{1, 2, 3, 4, 5}
# add:向集合內(nèi)添加元素
s = {1}
s.add(3)
print(s)
{1, 3}
# clear
s = {1,2,3,4,5}
print(id(s))
s.clear()
print(id(s))
# 結(jié)果表明clear函數(shù)是原地清空數(shù)據(jù)
1370773843528
1370773843528
# copy:拷貝
# remove:移除指定的值硬爆,直接改變?cè)兄敌蓝妫绻獎(jiǎng)h除的值不存在,報(bào)錯(cuò)
# discard:移除集合中指定的值跟remove一樣缀磕,但是如果要?jiǎng)h除的話缘圈,不報(bào)錯(cuò)
s = {23,4,3,5,1,2,3}
s.remove(4)
print(s)
s.discard(1)
print(s)

print("*" * 20)
s.discard(100)
print(s)

s.remove(100)
print(s)
{1, 2, 3, 5, 23}
{2, 3, 5, 23}
********************
{2, 3, 5, 23}



---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-35-0113522ad176> in <module>
     12 print(s)
     13 
---> 14 s.remove(100)
     15 print(s)


KeyError: 100
# pop 隨機(jī)移除一個(gè)元素
s = {1,2,3,4,5,6,7}
d = s.pop()
print(d)
print(s)
1
{2, 3, 4, 5, 6, 7}
# 集合函數(shù)
# intersection:交集
# difference:差集
# union:并集
# issubset:檢查一個(gè)集合是否為另一個(gè)子集
# issuperset:檢查一個(gè)集合是否為另一個(gè)超集
s1 = {1,2,3,4,5,6}
s2 = {5,6,7,8,9}

s_1 = s1.intersection(s2)
print(s_1)

s_2 = s1.difference(s2)
print(s_2)

s_3 = s1.issubset(s2)
print(s_3)

s_4 = s1.issuperset(s2)
print(s_4)
{5, 6}
{1, 2, 3, 4}
False
False
# 集合數(shù)學(xué)操作
s1 = {1,2,3,4,5,6}
s2 = {5,6,7,8,9}

# 以下不支持
s_1 = s1 - s2
print(s_1)

s_2 = s1 + s2
print(s_2)
{1, 2, 3, 4}



---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-45-fac787d752ea> in <module>
      7 print(s_1)
      8 
----> 9 s_2 = s1 + s2
     10 print(s_2)


TypeError: unsupported operand type(s) for +: 'set' and 'set'

frozen set :冰凍集合

  • 冰凍就是不可以進(jìn)行任何修改的操作
  • frozenset是一種特殊集合
# 創(chuàng)建
s = frozenset()
print(type(s))
print(s)
<class 'frozenset'>
frozenset()

dict字典

  • 字典是一種組合數(shù)據(jù),沒有順序的組合數(shù)據(jù)袜蚕,數(shù)據(jù)以鍵值對(duì)形式出現(xiàn)
# 字典的創(chuàng)建
# 創(chuàng)建空字典1
d = {}
print(type(d))
print(d)

# 創(chuàng)建空字典2
d = dict()
print(d)

# 創(chuàng)建有值的字典糟把,每一組數(shù)據(jù)用冒號(hào)隔開,每一對(duì)鍵值對(duì)用逗號(hào)隔開
d = {"one":1, "two":2, "three":3}
print(d)

# 用dict創(chuàng)建有內(nèi)容字典1
d = dict({"one":1, "two":2, "three":3})
print(d)

# 用dict創(chuàng)建有內(nèi)字典2
# 利用關(guān)鍵參數(shù)
d = dict(one=1, two=2, three=3)
print(d)

# 
d = dict( [("one",1), ("two",2), ("three",3)])
print(d)
<class 'dict'>
{}
{}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}

字典的特性

  • 字典是序列類型牲剃,但是是無序序列糊饱,所以沒有分片和索引
  • 字典中的數(shù)據(jù)每個(gè)都有鍵值對(duì)組成,即kv對(duì)
    • key:必須是可哈希的值颠黎,比如:int另锋,string,float狭归,tuple夭坪,但是,list过椎,set室梅,dict不行
    • value:任何值

字典常見操作

# 訪問數(shù)據(jù)
d = {"one":1, "two":2, "three":3}
# 注意訪問格式
# 中括號(hào)內(nèi)是鍵值
print(d["one"])

d["one"] = "eins"
print(d)

# 刪除某個(gè)操作
# 使用del操作
del d["one"]
print(d)
1
{'one': 'eins', 'two': 2, 'three': 3}
{'two': 2, 'three': 3}
# 成員檢測(cè):in,not in
# 成員檢測(cè)檢測(cè)時(shí)的key內(nèi)容
d = {"one":1, "two":2, "three":3}

if 2 in d:
    print("value")

if "two" in d:
    print("key")
    
if ("two,2") in d:
    print("kv")
key
# 遍歷在python2 和 3 中區(qū)別比較大疚宇,代碼不通用
# 按key值使用for循環(huán)
d = {"one":1, "two":2, "three":3}
# 使用for循環(huán)亡鼠,直接按keu值訪問
for k in d:
    print(k, d[k])
    
# 上述代碼可以改寫如下
for k in d.keys():
    print(k, d[k])

# 只訪問字典的值
for v in d.values():
    print(v)
    
# 注意以下特殊用法
for k,v in d.items():
    print(k, "--", v)
one 1
two 2
three 3
one 1
two 2
three 3
1
2
3
one -- 1
two -- 2
three -- 3

字典生成式

d = {"one":1, "two":2, "three":3}

# 常規(guī)字典生成式
dd = {k:v for k,v in d.items()}
print(dd)

# 加限制條件的字典生成式
dd = {k:v for k,v in d.items() if v % 2 == 0}
print(dd)
{'one': 1, 'two': 2, 'three': 3}
{'two': 2}

字典相關(guān)函數(shù)

# 通用函數(shù):len,max敷待,min间涵,dict
# str(字典):用于返回字典的字符串格式
d = {"one":1, "two":2, "three":3}
print(str(d))
{'one': 1, 'two': 2, 'three': 3}
# clear:清空字典
# items:返回字典的鍵值對(duì)組成的元組格式

d = {"one":1, "two":2, "three":3}
i = d.items()
print(type(i))
print(i)
<class 'dict_items'>
dict_items([('one', 1), ('two', 2), ('three', 3)])
# keys:返回字典的鍵組成的一個(gè)結(jié)構(gòu)
k = d.keys()
print(type(k))
print(k)
<class 'dict_keys'>
dict_keys(['one', 'two', 'three'])
# values:同理,一個(gè)可迭代的結(jié)構(gòu)
v = d.values()
print(type(v))
print(v)
<class 'dict_values'>
dict_values([1, 2, 3])
# get:根據(jù)指定鍵返回相應(yīng)的值榜揖,好處是勾哩,可以生成默認(rèn)值

d = {"one":1, "two":2, "three":3}
print(d.get("oner"))

# get默認(rèn)值是None抗蠢,可以設(shè)置
print(d.get("one", 100))
print(d.get("one33", 100))

print(d['on333'])
None
1
100



---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-86-f8c01a58018e> in <module>
      8 print(d.get("one33", 100))
      9 
---> 10 print(d['on333'])


KeyError: 'on333'
# fromkeys:使用指定的序列作為鍵,使用一個(gè)值作為字典的所有鍵的值
l = ["eins", "zwei", "dree"]
# 注意fromkeys兩個(gè)參數(shù)的類型
# 注意fromkeys的調(diào)用主體
d = dict.fromkeys(l, "hahahahaha")
print(d)
{'eins': 'hahahahaha', 'zwei': 'hahahahaha', 'dree': 'hahahahaha'}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末思劳,一起剝皮案震驚了整個(gè)濱河市迅矛,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌潜叛,老刑警劉巖秽褒,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異威兜,居然都是意外死亡震嫉,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門牡属,熙熙樓的掌柜王于貴愁眉苦臉地迎上來票堵,“玉大人,你說我怎么就攤上這事逮栅°彩疲” “怎么了?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵措伐,是天一觀的道長(zhǎng)特纤。 經(jīng)常有香客問我,道長(zhǎng)侥加,這世上最難降的妖魔是什么捧存? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮担败,結(jié)果婚禮上昔穴,老公的妹妹穿的比我還像新娘。我一直安慰自己提前,他們只是感情好吗货,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著狈网,像睡著了一般宙搬。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上拓哺,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天勇垛,我揣著相機(jī)與錄音,去河邊找鬼士鸥。 笑死闲孤,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的础淤。 我是一名探鬼主播崭放,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼鸽凶!你這毒婦竟也來了币砂?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤玻侥,失蹤者是張志新(化名)和其女友劉穎决摧,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凑兰,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡掌桩,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了姑食。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片波岛。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖音半,靈堂內(nèi)的尸體忽然破棺而出则拷,到底是詐尸還是另有隱情,我是刑警寧澤曹鸠,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布煌茬,位于F島的核電站,受9級(jí)特大地震影響彻桃,放射性物質(zhì)發(fā)生泄漏坛善。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一邻眷、第九天 我趴在偏房一處隱蔽的房頂上張望眠屎。 院中可真熱鬧,春花似錦肆饶、人聲如沸组力。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)燎字。三九已至,卻和暖如春阿宅,著一層夾襖步出監(jiān)牢的瞬間候衍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工洒放, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蛉鹿,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓往湿,卻偏偏與公主長(zhǎng)得像妖异,于是被迫代替她去往敵國(guó)和親惋戏。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容