1. 程式人生 > >Python系統學習第九課

Python系統學習第九課

關於元組的函式

  • 基本和list一樣
#獲取元組長度
t = (1,2,3,4, 3)
len(t)
4
#獲取元組最大最小值
print(max(t))
print(min(t))
4
1
#index 求指定元素在元祖中的索引位置, 如果中元組中有多個值,則返回的是第一個
print(t.index(3))
2

元組變數交換法

  • 兩個變數交換值
a = 1
b = 3
a,b = b, a
print(a)
print(b)
3
1

集合-set

  • 集合是高中數學的一個概念
  • 一堆確定的,無序的,唯一的資料,集合中每一個數據成為一個元素
#集合的定義
s = set()
print(type(s))
<class 'set'>
#如果只是用大括號定義,則定義的是一個dict型別
d = {}
print(type(d))
d = {1,2,3,3,4,5}   #如果大括號內部有值就成為了一個集合
print(type(d))
<class 'dict'>
<class 'set'>

集合的特徵

  • 集合資料無序,那麼不可以索引和分片,
  • 集合內部資料具有唯一性,可以排除重複資料
  • 集合內的資料可以放字串,數值,元組,就是說內部可以放可雜湊資料

成員檢測

  • in / not in
# 帶有元祖的集合遍歷
s = {(1,2,3),("lizi", "liuya", "tiantian"),(2,3,5)}    #集合本身不可雜湊,集合內部不可有集合的元素
for i,j,k in s:
    print(i, "-", j, "-",k)
2 - 3 - 5
lizi - liuya - tiantian
1 - 2 - 3

集合的內涵

#普通的集合內涵
s = {2,3,6,5,4,12,5,8,79,4,5,6,12,3,5}
print(s)   #自動過濾重複函式
{2, 3, 4, 5, 6, 8, 12, 79}
#普通集合內涵
s1 = {i for i in s}
print(s1)
{2, 3, 4, 5, 6, 8, 12, 79}
#帶條件的集合過濾
s1 = {i for i in s if i%2 == 0}
print(s1)
{2, 4, 6, 8, 12}

關於集合的函式

  • len
  • max
  • min
#set生成一個集合
s = [1,2,3,34,45,5,6,6,3,2,2,3,4,5,3]
a = set(s)
print(a)
{1, 2, 3, 34, 5, 6, 4, 45}
#add在集合內新增元素
a.add(99)
print(s)
[1, 2, 3, 34, 45, 5, 6, 6, 3, 2, 2, 3, 4, 5, 3]
#clear函式
a.clear()
print(a)   #只是原地將資料清空了,id不變
set()

#copy函式
#remove函式;移除指定的值,直接改變原有的值,如果原有的值不存在,就會報錯
#discard函式;移除集合中指定的值, 如果原有的值不存在,不會報錯

#pop函式,隨機移除一個元素
s = {5,9,32,23,5,6,54}
d = s.pop()
print(d)
print(s)
32
{5, 6, 9, 54, 23}
#集合函式
#intersection:交集
#difference:差集
#union: 並集
#issubset:檢查一個集合是否是另一個集合的子集
#issuperset:檢查一個集合是不是另一個集合的超集
s1 = {1,2,3,4,5,6}
s2 = {3,4,5,6,7,8}
s3 = s1.intersection(s2)
s4 = s1.difference(s2)

print(s3)
print(s4)
print(s1.issubset(s2))
print(s1.issuperset(s2))
{3, 4, 5, 6}
{1, 2}
False
False
#frozen set :冰凍集合:冰凍集合就是不可以進行任何修改的集合,一種特殊的集合
s = {1,2,3,4,5,6}
s1 = frozenset(s)
print(s)
print(type(s))
s1.pop()   #不可修改
print(s1)
{1, 2, 3, 4, 5, 6}
<class 'set'>



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

AttributeError                            Traceback (most recent call last)

<ipython-input-57-4eb685a8ebbb> in <module>()
      4 print(s)
      5 print(type(s))
----> 6 s1.pop()
      7 print(s1)


AttributeError: 'frozenset' object has no attribute 'pop'

字典

  • 字典是一種組合資料,沒有順序的組合資料,資料以“鍵值對”形式出現
#字典的建立
d = {}
print(d)

#建立空字典
d1 = dict()
print(d1)

#建立有值的字典
d2 = {"one":1, "second":2, "three":3}
print(d)

#建立有值的字典2
d3 = dict({"one":1, "second":2, "three":3})
print(d3)

#建立有值的字典3(使用關鍵字引數)
d4 = dict(one=1, second=2, three=3)
print(d4)

#建立有值的字典4
d5 = dict([("one",1),("second", 2),("three", 3)])
print(d5)

{}
{}
{}
{'one': 1, 'second': 2, 'three': 3}
{'one': 1, 'second': 2, 'three': 3}
{'one': 1, 'second': 2, 'three': 3}

字典的特徵

  • 字典是序列型別,但是是無序序列, 所有沒有分片和索引
  • 字典中的資料每個都有鍵值對組成,即kv對
  • key: 必須是可雜湊的值,比如int,float,tuple,但是,list,set, dict不行
  • value: 可以是任何值

字典常見操作

# 訪問資料
d = {'one': 1, 'second': 2, 'three': 3}
print(d["second"])
2
#賦值
d = {'one': 1, 'second': 2, 'three': 3}
d["one"] = 4
print(d)
d["one"] = "aaa"
print(d)
{'one': 4, 'second': 2, 'three': 3}
{'one': 'aaa', 'second': 2, 'three': 3}
#刪除
d = {'one': 1, 'second': 2, 'three': 3}
del d["one"]
print(d)  #將整個鍵值對都刪除了
{'second': 2, 'three': 3}
#成員檢測, 成員檢測檢測的將是關鍵字
d = {'one': 1, 'second': 2, 'three': 3}
if 2 in d:
    print("value")
if "second" in d:
    print("key")
if ("two", 2) in d:
    print("kv")

key
#遍歷, 遍歷是按照key來執行for迴圈的。
d = {'one': 1, 'second': 2, 'three': 3}
for i in d:
    print(i, d[i])
    
for i in d:
    print(d.values())
    
#也可以進行下邊的迴圈
for i in d.values():
    print(i)
    
for i in d.keys():
    print(i)
    
for i,j in d.items():
    print(i, "--", j)
one 1
second 2
three 3
dict_values([1, 2, 3])
dict_values([1, 2, 3])
dict_values([1, 2, 3])
1
2
3
one
second
three
one -- 1
second -- 2
three -- 3
#加限制條件的字典生成式
d = {'one': 1, 'second': 2, 'three': 3}
d1 = {k:v for k,v in d.items() if v%2 == 0}
print(d1)
{'second': 2}
#字典相關函式:len,max,min,dict
#str(字典):返回字典的字串格式
d = {'one': 1, 'second': 2, 'three': 3}
print(str(d))
{'one': 1, 'second': 2, 'three': 3}
#clear:請空字典
#items: 返回子弟的鍵值對組成的元組格式
d = {'one': 1, 'second': 2, 'three': 3}
i = d.items()
print(i)
dict_items([('one', 1), ('second', 2), ('three', 3)])
k = d.keys()
print(k)  #返回一個可迴圈的結構
dict_keys(['one', 'second', 'three'])
v = d.values()
print(v)  #返回一個可迴圈的結構
dict_values([1, 2, 3])
#get函式;根據指定鍵返回響應的值,並且可以設定預設值
d = {'one': 1, 'second': 2, 'three': 3}
print(d.get("one"))
print(d.get("one222"))   #沒找到指定值,並不報錯,如果這樣寫就會報錯,print(d["one222"])   #會報錯

print(d.get("one", 100))  #如果能找到one就打印出來,如果找不到,就返回一個預設值,就是100

print(d.get("one22", 100))

print(d.get("one", 100))
1
None
1
100
1
#fromkeys: 使用指定的序列作為鍵key,使用一個值作為字典的所有的鍵的值value
d = {'one', 'second', 'three'}
d1 = dict.fromkeys(d,"asderdds")
print(d1)
{'one': 'asderdds', 'three': 'asderdds', 'second': 'asderdds'}