python常用代码大全分享

简介: python常用代码大全分享

def getFileList(dir,Filelist, ext=None):

  1. newDir = dir
  2. if os.path.isfile(dir):
  3. if ext is None:
  4. Filelist.append(dir)
  5. else:
  6. if ext in dir[-3:]:
  7. Filelist.append(dir)
  8. elif os.path.isdir(dir):
  9. for s in os.listdir(dir):
  10. newDir=os.path.join(dir,s)
  11. getFileList(newDir, Filelist, ext)
  12. return Filelist
      //代码效果参考:http://www.ezhiqi.com/zx/art_5352.html

2.对指定数组进行等分,可以用于多线程程序
复制代码
1 def div_list(ls,n):
2 result = []
3 cut = int(len(ls)/n)
4 if cut == 0:
5 ls = [[x] for x in ls]
6 none_array = [[] for i in range(0, n-len(ls))]
7 return ls+none_array
8 for i in range(0, n-1):
9 result.append(ls[cuti:cut(1+i)])
10 result.append(ls[cut*(n-1):len(ls)])
11 return result
3.用csv文件处理数据集
1 import threading
2 import os
3 from PIL import Image
4 import math
5 import pandas as pd
6 from tqdm import tqdm
//代码效果参考:http://www.ezhiqi.com/bx/art_6717.html

8 image_path = "/data0/Manually_Annotated_Images/"
9 save_path = "/home/frank/affectNet/val/"
10 filename = "/data0/validation.csv"
11
12 def savePic(subdata):
13 print("thread %s is running..." %threading.current_thread().name)
14 for index, row in tqdm(subdata.iterrows()):
15 try:
16 imgPath = image_path + row['subDirectory_filePath']
17 image = Image.open(imgPath)
18 classes = row['expression']
19 event = row['subDirectory_filePath'].split('/')[-1]
20 if not os.path.exists(save_path + classes):
21 os.makedirs(save_path + classes)
22 image.save(save_path+classes+'/'+event)
23 except:
24 pass
25 print("thread %s is ended....." %threading.current_thread().name)
26
27
28
29
30 def splitdf(df,num):
31 linenum = math.floor(len(df)/num)
32 pdlist = []
33 for i in range(num):
34
35 pd1 = df[ilinenum:(i+1)linenum]
36 pdlist.append(pd1)
37 # print(len(pd1))
38 pd1 = df[(num-1)*linenum:len(df)]
39 pdlist.append(pd1)
40 return pdlist
41
42 data = pd.read_csv(filename)
43 data = data.applymap(str)
44 subData = splitdf(data,32)
45
46 th = []
47 for i in range(32):
48 t = threading.Thread(target=savePic,args = (subData[i],))
49 t.start()
50 th.append(t)
//代码效果参考:http://www.ezhiqi.com/bx/art_2649.html

51
52 for t in th:
53 t.join()
54
55
56
57 print("saving is success!")

相关文章
|
Java Python
python代码大全
python代码大全(小白篇)
|
Java Python
python代码大全简单图解
python代码大全简单图解
|
6月前
|
数据可视化 流计算 Python
Python创意爱心代码大全:从入门到高级的7种实现方式
本文分享了7种用Python实现爱心效果的方法,从简单的字符画到复杂的3D动画,涵盖多种技术和库。内容包括:基础字符爱心(一行代码实现)、Turtle动态绘图、Matplotlib数学函数绘图、3D旋转爱心、Pygame跳动动画、ASCII艺术终端显示以及Tkinter交互式GUI应用。每种方法各具特色,适合不同技术水平的读者学习和实践,是表达创意与心意的绝佳工具。
4114 0
|
JSON 人工智能 数据挖掘
Python零基础入门必背代码大全
Python零基础入门必背代码大全
435 1
|
JSON 数据格式 索引
Python之巅:探索50个代码大全
Python之巅:探索50个代码大全
467 0
|
数据采集 机器学习/深度学习 人工智能
python代码大全
python代码大全
308 0
|
1月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
201 102
|
1月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
209 104
|
1月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
194 103
|
1月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
137 82

推荐镜像

更多