python进阶例题
生活随笔
收集整理的這篇文章主要介紹了
python进阶例题
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
python進(jìn)階
hello,大家好,我是Dream
上次給大家?guī)砹藀ython基礎(chǔ)例題(關(guān)注我可以看到喲),這次給大家整理了一些進(jìn)階的知識(shí)點(diǎn)和例題,希望大家喜歡!
首先和大家分享一個(gè)有意思的東西:
當(dāng)當(dāng)當(dāng)當(dāng)~
就是這個(gè)進(jìn)度條了,我感覺老有意思了,哈哈哈
這是它的代碼,大家可以參考一下:
python進(jìn)階
#用for循環(huán)實(shí)現(xiàn)把字符串變成Unicode碼未的列表: a='!@#$%^&*(' codes=[] for i in a:codes.append(ord(i)) print(codes)#怎樣用列表推導(dǎo)式把字符串變成Unicode碼位的列表 a="!@#$" codes=[ord(s) for s in a] print(codes) #很明顯,?列表推導(dǎo)式實(shí)現(xiàn)? for 循環(huán)加 append 更?效簡(jiǎn)潔,可讀性更好。#打印出兩個(gè)列表的笛卡爾積 #解法1:使??成器表達(dá)式產(chǎn)?笛卡爾積,可以幫忙省掉運(yùn)? for 循環(huán)的開銷。 colors=['black','white'] sizes=['S','M','l'] for tshirts in ('%s %s'%(c,s)for c in colors for s in sizes):print(tshirts) #解法2:使? itertools ?的 product ?成器函數(shù)。 import itertools print(list(itertools.product(['black','white'],['S','M','L'])))#用神魔方法接收不確定值或者參數(shù) #? *args 的?式,*args 位置可以在任意位置。 a,b,*c=range(8) print(a,b,c) a,*b,c,d=range(5) print(a,b,c,d) *a,b,c,d=range(5) print(a,b,c,d)#sort與sorted的區(qū)別 l=[1,9,5,8] l.sort() print(l) print(sorted(l)) #sort() 會(huì)就地在原序列上排序,sorted() 新建了?個(gè)新的序列。#怎么通過 reverse 參數(shù)對(duì)序列進(jìn)?降序排列 l=[1,9,5,8] j=sorted(l,reverse=True) print(j)#快速插?元素到列表頭部 #1.切片 l=[1,2,3,4,5] l[0:0]='Python' print(l) #將python分開了 #2.用insert方法 l=[1,2,3,4,5] l.insert(0,'first') print(l)#創(chuàng)建字典 a = dict(one=1, two=2, three=3) b = {'one': 1, 'two': 2, 'three': 3} c = dict(zip(['one', 'two', 'three'], [1, 2, 3])) d = dict([('two', 2), ('one', 1), ('three', 3)]) e = dict({'one': 1, 'two': 2, 'three': 3}) print(a,b,c,d,e)#列表去重 l=['A','A','B','B'] print(list(set(l)))#Python中怎么定義私有屬性。 #在屬性前加兩個(gè)前導(dǎo)下劃線,尾部沒有或最多有?個(gè)下劃線#怎么隨機(jī)打亂?個(gè)列表?元素的順序 from random import shuffle l=list(range(30)) shuffle(l) print(l)#進(jìn)度條顯示 import time from tqdm import tqdm for i in tqdm(range(1000)):time.sleep(.01)這就是我熬夜整理的了,希望對(duì)大家有幫助。如果喜歡我的話,可不可以給我個(gè)一鍵三連呀-.-,謝謝你們!
總結(jié)
以上是生活随笔為你收集整理的python进阶例题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux shell sed awk
- 下一篇: activiti6创建28张表