【如夢令·纖月黃昏庭院】python 進階 01

【標題和詩無關】

 

欲語幽情期紅裙,平林漠漠柳枝深。

除卻當時畫眉鳥,風情許知一佳人。

 

 

使用的是jupyter notebook

python版本 3.6.7

 

 

# 一、在列表 字典 集合中根據條件篩選數據
# 生成隨機的列表
import random
data = [random.randint(-10,10) for _ in range(10)]
# 1.過濾列表中的負數
# 方法零 普通的迭代方式
# 方法一 filter函數
list(filter(lambda x:x>=0,data))
# 方法二 列表解析
# (1)
[i if i>=0 else 111 for i in data ]
# (2)
[i for i in data if i>=0]
# 根據timeit,可以發現列表解析效率更高,首選列表解析式  timeit list(filter(lambda x:x>=0,data))

# 注意:filter和列表解析式等生成的是迭代器。

# 2.根據值過濾字典 分數高於120的
# 字典解析
# 生成字典
stu_grade = {x:random.randint(90,150) for x in range(10)}
{k:v for k,v in stu_grade.items() if v>120}

# 3.# 集合中能被3整除的
s = set(data)
{x for x in s if x%3==0}


# 二、爲元祖中的元素命名,提高程序可讀性
# 元祖的優勢 :減小內存開銷,同時索引很快

'''
('Lily',16,'[email protected]'),
('Lilei',17,'[email protected]'),
('Liming',18,'[email protected]')
'''
# 訪問值的時候,如何通過索引訪問?
# 方法一、定義類似於其他語言的枚舉類型
student = ('Lily',16,'[email protected]')
NAME,AGE,E_MAIL = 0,1,2
student[NAME]
# 方法二、使用標準庫nametuple替代內置tuple
from collections import namedtuple
Student = namedtuple('student',['name','age','e_mail'])
# (1)
s1 = Student('Lily',16,'[email protected]') #student(name='Lily', age=16, e_mail='[email protected]')
# (2)關鍵字傳參
s2 = Student(name='Lily', age=16, e_mail='[email protected]')
# 訪問
s.name
# 通過下面可以看出 namedtuple是tuple的子類
isinstance(s2,tuple)  # True

# 三 統計序列中元素出現的頻度
from collections import Counter
import random
data = [random.randint(0,20) for _ in range(20)]
# 方法一 普通方法 略
# 方法二 collectons Counter
c = Counter(data)
c[10]
c
# c.most_common(n) 找出出現頻度最高的3個數
c.most_common(3)
# 詞頻統計
text = '''There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.'''
# 引入re
import re
text_list = re.split('\W+',text)  #匹配特殊字符,即非字母、非數字、非漢字、非_
c3 = Counter(text_list)
c3.most_common(10) # 查看出現次數最多的十個單詞


# 四 根據字典中值的大小對字典的鍵進行排序
# 比如對成績表進行排序
import random
student = {x:random.randint(60,100) for x in 'abcxyz'}
# 普通的sorted方法
sorted(student)  # ['a', 'b', 'c', 'x', 'y', 'z'] 發現只是對鍵進行了排序且值不存在了
# sorted()傳入的是可迭代對象,list(iter(student))
# 因此,普通的sorted函數行不通。這時需要對字典進行轉換。
(79,'a')>(78,'aa')  # True
# 受到上面一行代碼的啓發,可以把字典轉換爲元祖
# 方法一
stu_new1 = dict(zip(student.values(),student.keys()))
sorted(stu_new1)  # 但是這裏得到的只是對key進行排序的結果
stu_new2 = {i:stu_new1[i] for i in sorted(stu_new1)}  # {67: 'c', 70: 'z', 77: 'x', 84: 'y', 100: 'a'}
# 方法二
dict(sorted(student.items(),key=lambda x:x[1],reverse=True))  # reverse=True表示降序排列。key=lambda x:x[1]  表示根據元祖中的第二個元素進行排序

# 五、 快速找到多個字典的公共鍵
from functools import reduce
import random
# 第一步 獲取多個字典
data1 = {x:random.randint(1,4) for x in random.sample('abcxyz',random.randint(3,5))}
data2 = {x:random.randint(1,4) for x in random.sample('abcxyz',random.randint(3,5))}
data3 = {x:random.randint(1,4) for x in random.sample('abcxyz',random.randint(3,5))}
# 第二步 獲取公共鍵
# 方法一  # 普通的集合交集
s1 = set(data1.keys())&set(data2.keys())&set(data3.keys())
# 方法二  高階函數
s2 = reduce(lambda x,y:x&y,map(lambda x:set(x.keys()),[data1,data2,data3]))   
# 或者 reduce(lambda x,y:x&y,map(lambda x:x.keys(),[data1,data2,data3]))

# 延申

# 注意:字典中沒有重複的鍵。
'''
字典是通過散列表或說哈希表實現的。字典也被稱爲關聯數組,還稱爲哈希數組等。
字典也是一個數組,但數組的索引是鍵經過哈希函數處理後得到的散列值。
哈希函數的目的是使鍵均勻地分佈在數組中,並且可以在內存中以O(1)的時間複雜度進行尋址,從而實現快速查找和修改。
只有可哈希的對象才能作爲字典的鍵.Python中所有不可變的內置類型都是可哈希的。
可變類型(如列表,字典和集合)就是不可哈希的,因此不能作爲字典的鍵。不可變的集合可以作爲字典的鍵,比如{(1,2,3):1}'''

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