python学习笔记四一列表元组字典等
?python中的內(nèi)置類型?str
?list
?tuple
?set
?dict
?list 列表
?一組由有序數(shù)據(jù)組成的序列?數(shù)據(jù)有先后數(shù)據(jù)
?數(shù)據(jù)可以不是一類數(shù)據(jù)
?list 的創(chuàng)建?直接創(chuàng)建,用中括號創(chuàng)建,內(nèi)容使用因為逗號 , 隔開
?使用list創(chuàng)建
?列表包含單個字符串的時候是一個特例
[1, 2, 3, 4, 5]
[1, 2, 3, ‘gepengcheng’, ‘楊雨’]
[]
<class ‘list’>
內(nèi)置函數(shù)
?help ;幫助函數(shù)
?type : 現(xiàn)實變量的類型
?id :現(xiàn)實變量id
?print : 打印
<class ‘list’>
[‘g’, ‘e’, ‘p’, ‘e’, ‘n’, ‘g’, ‘c’, ‘h’, ‘e’, ‘n’, ‘g’]
列表的常見操作
?訪問?使用下標操作,也叫索引
?列表的元素索引從0開始
?切片操作?對列表進行任意一段的截取需要注意取值范圍,左包括右不包括
?截取之后創(chuàng)建一個新得列表
12
22
IndexError Traceback (most recent call last)
in
4 print (l1[3])
5 #IndexError訪問超標
----> 6 print (l1[15])
IndexError: list index out of range
#切片操作需要注意取值范圍,元素索引從0開始左包括右不包括 l1 = [100,200,300,400,500,600,700,800,900] print (l1[3:6]) #切片操作下標可以為空 print (l1[:6]) print (l1[3:]) print (l1[:])print("__________________________________")#分隔符 #下面結(jié)果說明切片后生成一個全新的列表 #通過內(nèi)置函數(shù)id可以判斷出切片是否生成了一個全新的列表 #id 的作用是用來判斷兩個變量是否是一個變量 l2 = l1[0:10] print (id(l1)) print (id(l2))[400, 500, 600]
[100, 200, 300, 400, 500, 600]
[400, 500, 600, 700, 800, 900]
[100, 200, 300, 400, 500, 600, 700, 800, 900]
2546774356808
2546755019528
[100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
[100, 300, 500, 700, 900]
[100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
[]
[]
[900, 800, 700]
[700, 800, 900]
tuple (元組)
?可以理解成一個不允許更改的列表
tuple (元組)
?可以理解成一個不允許更改的列表
<class ‘tuple’>
<class ‘int’>
<class ‘tuple’>
(1, 2, 3, 4)
<class ‘tuple’>
<class ‘tuple’>
<class ‘tuple’>
()
(1, 2, 3, ‘gepengcheng’)
tuple 其余特征和list基本一致
?有序
?可以訪問不可以更改
?元素可以是任意類型
[‘i’, ‘love’, ‘una’]
una
(‘i’, ‘love’, ‘una’)
(‘i’, ‘love’)
(‘una’, ‘love’, ‘i’)
(100, 200, 300, ‘i’, ‘love’, ‘una’)
#tuple 乘法 aa = a3 * 2 print (aa)[‘i’, ‘love’, ‘una’, ‘i’, ‘love’, ‘una’]
# tuple 成員檢測 print (a3) if "una" in a3:print ("檢測到una") if "yangyu" not in a3:print ("未檢測到y(tǒng)angyu") if "una" not in a3:print ("未檢測到una")[‘i’, ‘love’, ‘una’]
檢測到una
未檢測到y(tǒng)angyu
i
love
una
(100, 200, 300)
100
200
300
(‘i’, ‘love’, ‘una’)
i
love
una
(520, 13, 14)
520
13
14
100 200 300
i love una
520 13 14
ValueError Traceback (most recent call last)
in
6 print ("____________________________________")#分隔符
7 # 上面訪問中有一個規(guī)定,既i,l,u要跟元組個數(shù)進行對應
----> 8 for i,l,u,y in b4:
9 print (i,l,u,y)
10
ValueError: not enough values to unpack (expected 4, got 3)
#常用元組函數(shù) # len : 長度 c4 = ((100,200,300),("i","love","una"),(520,13,14)) print(len(c4))print ("____________________________________")#分隔符# max/min : 最大值/最小值 d4 = (12,342,31312,212) print (max(d4)) print (min(d4))print ("____________________________________")#分隔符# count : 對某一元素進行計數(shù) f4 = (1,2,3,4,5,1,1,1,1,1) print (f4.count(1))print ("____________________________________")#分隔符# index : 某一元素所在位置 e4 = (1,2,3,4,5,1,1,1,1,1) print(e4.index(5))3
31312
12
6
4
# tuple的特殊用法 a = 520 b = "yang yu" print (a,b)print ("____________________________________")#分隔符#要求對a,b值進行互換 a,b = b,a print (a,b)520 yang yu
yang yu 520
集合
?跟數(shù)學中集合得概念一致
?內(nèi)容無序+內(nèi)容不重復
set()
#集合定義最多只能定義1個參數(shù) bb = set (1,2,3,4,5,6) print (bb)TypeError Traceback (most recent call last)
in
1 #集合定義最多只能定義1個參數(shù)
----> 2 bb = set (1)
3 print (bb)
TypeError: ‘int’ object is not iterable
b = [1,2,3,4,5,6,7] bbb = set (b) print (bbb){1, 2, 3, 4, 5, 6, 7}
# 2 ,使用大括號定義 sc = {1,2,3,4,5,6,7,8,11,22,33} print (sc){1, 2, 3, 4, 5, 6, 7, 8, 33, 11, 22}
# in 操作 if 2 in sc:print ("2在sc里面") if 9 in sc:print ("9在sc里面") print ("____________________________________")#分隔符 #集合里面的順序是無序的 for i in sc:print (i)2在sc里面
1
2
3
4
5
6
7
8
33
11
22
i love una
4 5 6
1 2 3
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{8, 2, 4, 6}
{1, 3, 5, 7, 9}
{64, 1, 4, 36, 9, 16, 49, 81, 25}
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, 28, 30, 32, 35, 36, 40, 42, 45, 48, 49, 54, 56, 63, 64, 72, 81}
# 集合的內(nèi)置函數(shù) # len: 長度 print (len (se)) # max/min : 最大值/最小值 #add : 向集合中添加元素sa = {1,2,3,4,5,6,5,4,3,2,1} print (sa) # 向sa中添加數(shù)字7 結(jié)果打印出來None print (sa.add(7)) #添加了值并不會在add添加選項中打印出來 如下列 print (sa) print (sa.add(8)) print (sa)36
{1, 2, 3, 4, 5, 6}
None
{1, 2, 3, 4, 5, 6, 7}
None
{1, 2, 3, 4, 5, 6, 7, 8}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3, 4, 6, 7, 8, 9}
KeyError Traceback (most recent call last)
in
8 print ("____________________________________")#分隔符
9 #如果remove刪除的值并不在集合中就會報錯
—> 10 sa1.remove(5)
11 print (sa1)
KeyError: 5
# discard案例 sa1 = {1,2,3,4,5,6,7,8,9} print (sa1) sa1.discard(5) print (sa1) print ("____________________________________")#分隔符 #如果discard刪除的值就不會報錯 sa1.discard(5) print (sa1){1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3, 4, 6, 7, 8, 9}
{1, 2, 3, 4, 6, 7, 8, 9}
# pop刪除集合的一個內(nèi)容 #刪除的內(nèi)容是隨機的 sa1 = {1,2,3,4,5,6,7,8,9} print (sa1) sa1.pop() print (sa1) sa1.pop() print (sa1){1, 2, 3, 4, 5, 6, 7, 8, 9}
{2, 3, 4, 5, 6, 7, 8, 9}
{3, 4, 5, 6, 7, 8, 9}
{4, 5, 6}
{1, 2, 3}
{1, 2, 3}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
TypeError Traceback (most recent call last)
in
13 print (sa.union(sb))
14 #并集 不能使用+表示
—> 15 print (sa + sb)
TypeError: unsupported operand type(s) for +: ‘set’ and ‘set’
frozenset 冰凍集合?
?不允許修改的集合
{1, 2, 3, 4, 5, 6}
frozenset({1, 2, 3, 4, 5, 6})
遞歸函數(shù)?
?遞歸 : 函數(shù)間接或者直接調(diào)用自己
?遞歸分兩個過程?往下調(diào)用,分解過程
?往上回溯,綜合過程
?遞歸需要注意?一定要有結(jié)束條件
?是以資源換取編寫速度的算法比較吃硬件
def a (n):print ("i love una ")def b (n):a(100)print ("una love gpc") b(100)i love una
una love gpc
5
4
3
2
1
f(5) = 120
遞歸必須有結(jié)束,負責會死掉報錯?
?def fun_a(n):
?print (n)
?return n * fun_a(n-1)
?rst = fun_a(5)
?print ("f(5) = ",rst)
jieguo= 55
# 漢諾塔 a = "A" b = "B" c = "C" def hannuo(a,b,c,n):if n == 1:print ("{}-->{}".format(a,c))return Noneif n == 2:print ("{}-->{}".format(a,c))print ("{}-->{}".format(a,b))print ("{}-->{}".format(b,c))return Nonehannuo(a,c,b,n-1)print ("{}-->{}".format(a,c))hannuo(b,a,c,n-1) # 只有一個盤子 hannuo(a,b,c,1)A–>C
hannuo (a,b,c,5)A–>B
A–>C
C–>B
A–>C
B–>C
B–>A
A–>C
A–>B
C–>A
C–>B
B–>A
C–>B
A–>B
A–>C
C–>B
A–>C
B–>C
B–>A
A–>C
B–>A
C–>A
C–>B
B–>A
B–>C
A–>B
A–>C
C–>B
A–>C
B–>C
B–>A
A–>C
總結(jié)
以上是生活随笔為你收集整理的python学习笔记四一列表元组字典等的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python学习笔记二— 循环
- 下一篇: 机器学习从零开始-常见算法手推pure