python涂鸦
【exit】
退出程序? ?
【%】
占位符:%s 【string】? %f【float】? %d【int】 ? ?isdigit:長得像數(shù)字
【print不換行】
python 2.X 版本使用逗號分割可以使print不換行? ? 【print "hello",】
python 3.X版本利用end方法可以使print不換行? ?【print ("hello ",end="")】
?
【】
for x in y (遍歷所有的y元素)
【dict字典】
訪問dict列表? ? ? ? ? ? ? ? ?d = {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'Adam': 95,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'Lisa': 85,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'Bart': 59
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for a in d:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print (a,d.get(a))
get(key值) 更新 d['Paul'] = 72
【增】
dict【元素】=***? ? ?元素有就修改,沒有就添加
dict.setdefault('','')? ? ?鍵存在就不改動,返回相應(yīng)的值,鍵不存在,增加新的鍵值對,并返回相應(yīng)的值
【刪】
del dict(元素) 刪除鍵值對? ? pop(元素) 刪除返回值? ? ? ?popitem()? ?隨機刪除一個鍵值對? ? ? clear() 清空? ? del dict? 刪除字典
【查】
dict.key() 鍵? ? ? ?.value() 值? ??.Iteme()鍵值對? ? ? ? in()? ?查詢是否存在值
【改】
dict.update(dict1)? ?有就更新,沒有添加
?
【set列表】
set列表是無序排列的(可以包含前邊所有的列表的格式)
set.remove(刪除)
set.add(添加)
【len(計算屬性的長度)】
?
【list切片】
print L[4:50:5]#從索引4開始,每個5位取一個,取到索引50結(jié)束,50索引不包含
?
【索引迭代】
enumerate(集合) dict迭代value【itervalue()or value()】
dict迭代key,value【item()or iteritems()】
?
【range()隨機數(shù)】
range(1,100,2) {1,3,5,7,。。。}
?
【編寫函數(shù):def】
【列表生成式for 可以添加篩選條件,可以添加多層嵌套循環(huán)】
return [x.upper() for x in L if isinstance(x,str)]
isinstance()判斷是否是字符串 upper()字母大寫
?
【對list進行增刪改】
?
append(默認添加到末尾)? ? ? ? insert(index,“”) 添加到那個索引? ? ? ?list.extend(list1)? ? ? 將兩個列表合成一個
remove(“”)? 直接刪除那個元素? ? ? b= pop(1,“””) 刪除那個索引值的元素,可以將這個元素取出? ? ? ? del? 什么都能刪除,包括這個對象? ? ?.clear? 清空
?
【對list進行查詢】
list.count(元素)? ? ? 查找列表中同一元素的個數(shù)
list.index(元素)? ?查找列表的的元素索引值
【sort】
沒有返回值
數(shù)字按照從小到大排序,字符按照Ascll排序
【reverse】
list 反序擺列
?
【enumerate】
一個可以添加索引值的方法? ? ??enumerate(list,1)??
?
【字符串拼接】
‘’.join(【a,b,c】)
center(50,'')?
?
【endswith() / startswith】? ?以什么開頭/結(jié)尾? ? ? ?【find】 查找元素返回索引值? ? ?【isdigit】判斷是否是數(shù)字? 【islower/ isupper】? 判斷是否全大寫/ 小寫
【strip】 刪除左右的空格,換行符? ? ? 【replace】 替換? ? ? ? ? ? 【split】? 分割符? ? ? 【expandtabs】 制作表格
【format】 格式化輸出?
?
【文件】
【open() 】? 打開文件? ? ? ? 【read()】? ?讀取文件? ? 【 write()】 寫入文件? ? ? 【 tell() 】 獲取光標的位置? ? ? 【seek()】 移動光標? ? ? ?
【flush】? ?刷新緩存區(qū),立即寫入磁盤? ? ? ? ? 【truncate()】? 數(shù)據(jù)截斷? ?“w” 沒有太大意義? “a”? ?可按要求截斷
?
【*******函數(shù)*******】
【默認參數(shù)】? ? 默認函數(shù)一般放在參數(shù)的最后
def? f(name,age=12)
f("Tom")
?
【不定參數(shù)(沒有命名)】? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?【不定參數(shù)(有命令)】
def? ?f(*args)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?def f (**args)
f(1,2,3,4)? ? ? f(name=“Tom”,age=12)
運行后會生成一個元組 運行后會生成一個字典
補充參數(shù):
def a(*args):print args a(*[1,2,3,4]) #####單個添加到元組####def b(**kwargs):print kwargs b(**{"name":"aa"}) ######鍵值對方式添加到字典####?
?
【return】
1 結(jié)束某個函數(shù)? ?2返回一個對象? ? 3返回多個對象默認放在一個元組中
【作用域】
built_in(系統(tǒng))? ? ? ? ? ? ? ? ? ?global(全局)? ? ? ? ? ? ? ?enclosing(嵌套)? ? ? ? ? ? ? ? lical(本地)
count = 10 def out():global count ##申明全局變量print countcount = 5 ##局部不能修改全局的變量 out()?
【高階函數(shù)】
def f(n):return n*n def foo(a,b,fn):return fn(a)+fn(b) ####fn()就是運行這個f()函數(shù)###### print foo(1,2,f) ######f就是一個函數(shù)的內(nèi)存地址######def f():return 8 def fun():return f ter=fun() #####只是得到f函數(shù)的內(nèi)存地址###### print ter print ter() ####只要加上括號就代表運行這個函數(shù),所以就是運行f()這個函數(shù)?
?【遞歸函數(shù)】
調(diào)用自己的函數(shù)方法? + 結(jié)束條件
階乘:
def f(n):if n==1:return 1return n*f(n-1) #####調(diào)用自己,結(jié)束條件 print f(7)print reduce(lambda a,b:a*b ,range(1,8))運行結(jié)果: 5040斐波那契數(shù)列:(沒大搞懂)
0,1,1,2,3,5,8,13.。。。。。。
def f(n):if n<=2:return nreturn f(n-1)+f(n-2)print f(5)運行結(jié)果: 8?
?【內(nèi)置函數(shù)】
幾個重要的內(nèi)置函數(shù) :
filter(可以用于篩選【迭代器】)? ? ? ? ?? map(可以用于給原來的元素添加)?? reduce(返回的是一個具體的值) lambda(匿名函數(shù))
#filter str = ["a","b","c","d","e"] def func(s):#print sif s !="a":return s let = filter(func,str) print list(let)#map str = [ 'a', 'b'] def fun2(s):return s + "alvin" ret = map(fun2, str) print(ret) # map object的迭代器 print(list(ret)) # ['aalvin', 'balvin', 'calvin', 'dalvin']#reduce from functools import reduce def add1(x, y):return x + y print (reduce(add1, range(1, 101))) ## 5050 (注:1+2+...+100) print (reduce(add1, range(1, 101), 20)) ## 5070 (注:1+2+...+100+20)#lambda #普通函數(shù) def add(a, b):return a + b print add(2, 3) # 匿名函數(shù) add = lambda a, b: a + b print add(2, 3)運行結(jié)果: ['b', 'c', 'd', 'e'] ['aalvin', 'balvin'] ['aalvin', 'balvin'] 5050 5070 5 5?
【裝飾器函數(shù)】
閉包定義:1 有內(nèi)部函數(shù)? ?2 引用外部函數(shù)的值? ? ?3 return函數(shù)名
示:1:
import time def show_time(f):def out(*x,**y):start = time.time()f(*x,**y)end = time.time()print "spend time:%s"%(end-start)return out @show_time # f=show_time(f) def f(*a,**b):sums=0for i in a:sums+=iprint sumstime.sleep(1) f(1,2,7,4)#遵守開放,封閉的原則,在不修改原來的代碼的基礎(chǔ)上添加要求,調(diào)用的時候重新賦值相當于調(diào)用原來的函數(shù), 不會影響其他對該函數(shù)的調(diào)用運行結(jié)果: 14 spend time:1.0示例2:
import time def log(flag):def show_time(f): #2.show_time(f,flag)def out(*x,**y):start = time.time()f(*x,**y)end = time.time()run = end-startprint "spend time:%s"%(run)if flag=="True":with open("text","a") as ji:ji.write("\nspend time:%s"%(run))return outreturn show_time @log("True") # 1.@封裝的只能傳一個參數(shù),只要滿足閉包的定義就能運行,返回值show_time,就相當于@show_time, def f(*a,**b): #2,不用@多加一個參數(shù)也可以sums=0for i in a:sums+=iprint sumstime.sleep(1) f(1,2,7,4)運行結(jié)果: 14 spend time:1.0文本內(nèi)容請自行查看?
【模塊】
【time(時間)】
time.time()當前時間戳? time.strftime()? ?可以自定義格式? 【time.time(%Y-%m-%d %X,time.localtime)】? time.localtime()本地時間
time.ctime()? ?當?shù)毓潭〞r間 (Tm_Year=2018的格式) time.mktime()? ?轉(zhuǎn)化為時間戳
【datetime】
datetime.datetime.now()
【random】
random()0-1隨機數(shù) randint()整數(shù)隨機數(shù)包含最后一個 choice()隨機選擇 shuffle()?
randrange()不包含最后一個
?
?
?
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/guo970910/p/9820410.html
總結(jié)
- 上一篇: 20180826(01)-Java数据结
- 下一篇: 具体knn算法概念参考knn代码pyth