【Python學習整理 D02】:集合、元組、format、遞歸

集合(set)

跟數學中集合的概念一致
內容無序+內容不重複

通過set關鍵字定義

sa = set()
print(type(sa))  # <class 'set'>
print(sa)  # set()

使用大括號定義

sc = {1, 2, 3, 4, 5, 123, 1, 23, 12, 31, 2, 132, 31, 23, 123, 2, 2, 21, 12, 3, 3, 3, }
print(sc) 	#{1, 2, 3, 4, 5, 132, 12, 21, 23, 123, 31}

集合的另一種遍歷

sa = {(1, 2, 3), (4, 5, 6), ('i', 'love', 'wangxiaojing')}
for i, j, k in sa:
    print(i, j, k)
#4 5 6
#i love wangxiaojing
#1 2 3

集合的生成式

sa = {1, 2, 3, 4, 5, 6, 7, 8, 9}
sb = {i for i in sa if i % 2 == 0}
print(sb)
#{8, 2, 4, 6}

刪除操作,remove 和 discard的區別

sa = {1, 2, 3, 4}
print(sa)	#{1, 2, 3, 4}
sa.remove(4)  # remove刪了再刪會報錯
print(sa)
sa.discard(3)
print(sa)	#{1, 2}
sa.discard(3)  # discard刪了再刪不會報錯
print(sa)	#{1, 2}

pop彈出集合的一個內容,刪除的內容是隨機的

集合的數學操作

# intersection:交集
# difference:差集
# union:並集
sa = {1, 2, 3, 4}
sb = {3, 4, 5, 6}
print(sa.intersection(sb))  # {3, 4}
print(sa.difference(sb))  # {1, 2}
print(sa - sb)  # {1, 2}
print(sa.union(sb))  # l{1, 2, 3, 4, 5, 6}

元組(tuple)

可以理解成一個不允許更改的列表

用小括號創建一個元素的tuple的時候

tb = (100)
print(type(tb))  # <class 'int'>
tb = (100,)
print(type(tb))  # <class 'tuple'>
tb = (100, 200, 300)
print(type(tb))  # <class 'tuple'>
# 當創建tuple時,只有一個值時,最後需加個,逗號

創建tuple時也可以不加小括號,直接加逗號

tb = 100,
print(type(tb))  # <class 'tuple'>
tb = 100, 200, 300
print(type(tb))  # <class 'tuple'>

使用tuple定義

tc = tuple()  # 要求tuple的參數必須可迭代
print(type(tc))  # <class 'tuple'>

tuple其餘特徵跟list基本一致

  • 有序
  • 可以訪問不可以被修改
  • 元素可以是任意類型

tuple的特殊用法

# 要求對a,b值進行互換
# 此種用法是Python的獨門用法
print(a, b)  # (100,) wang xiao jing
a, b = b, a
print(b, a)  # (100,) wang xiao jing

傳統格式化、format格式化常規用法

    傳統格式化:
        使用 % 進行格式化
            %s  字符串
            %d  十進制整數
            %f  十進制浮點數
            "-"  左對齊
            "+"  右對齊

    format:
        1.順序使用
        2.使用命名參數
        3.設置指定位置

format 命名參數

a = "l love {name1},and i love {name2}"
print(a.format(name1="ming", name2="tian"))
#l love ming,and i love tian

format 字典命名參數,需**解包操作

a = "l love {name1},and i love {name2}"
dic = {
    'name1': 'ming',
    'name2': 'tian'
}
print(a.format(**dic))
#l love ming,and i love tian

format 設置指定位置

a = "l love {1},and i love {0}"
print(a.format("ming", "tian"))
#l love tian,and i love ming

遞歸函數

	遞歸函數
	遞歸:函數間接或者直接調用自己
	遞歸分兩個過程
	    往下調用,分解的過程
	    網上回溯,綜合的過程
	遞歸需要注意:
	    一定要有結束條件
	是以資源換取編寫速度的算法

階乘

# 階乘
def fun(n):
    s = 1
    for i in range(1, n + 1):
        s *= i
    return s


def funa(n):
    if n == 1:
        return 1
    return n * funa(n - 1)


print(fun(100))
print(funa(100))

斐波那契數列

# 斐波那契數列
# 1,1,2,3,4,8,13,21,34.....
# f(n) = f(n - 1)+ f(n - 2)
def fib(n):
    if n == 1 or n == 2:
        return 1
    return fib(n - 1) + fib(n - 2)

print(fib(10))

漢諾塔

# 漢諾塔
a = 'A'
b = 'B'
c = 'C'

def hano(a, b, c, n):
    if n == 1:
        print('{}-->{}'.format(a, c))
        return None
    if n == 2:
        print('{}-->{}'.format(a, b))
        print('{}-->{}'.format(a, c))
        print('{}-->{}'.format(b, c))
        return None
    hano(a, c, b, n - 1)
    print('{}-->{}'.format(a, c))
    hano(b, a, c, n - 1)

hano(a, b, c, 3)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章