我是靠谱客的博主 明亮鸭子,这篇文章主要介绍python3.x编程模板总结,现在分享给大家,希望可以做个参考。

刚接触Python3版本的小伙伴们,编程时会对于Python中各种数据结构如:array、list、dict、set以及字符串str操作都不太熟悉。同时类似于Python网络编程、文件读取、数据库连接以及协程这些编程模板基本也都是固定的,本文便就这些方面进行总结,希望让大家进行Python3编程时能够更加的便捷,可以直接复制粘贴而不用每次都手敲了,好下面进入正题啦!

一、list各种操作

1、list和array之间相互转换及遍历

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from numpy import * #python 中list和array之间的相互转换以及list和array的遍历 testList=[[1,2,3],[4,5,6]] #将list转化成array testArray=array(testList) for i in range(testArray.shape[0]): for j in range(testArray.shape[1]): print(testArray[i,j],' ',end='') print() print() #将array转化成list toList=testArray.tolist() for i in range(len(toList)): for word in toList[i]: print(word,' ',end='') print()

2、查找返回list中出现次数最多的那个元素

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #查询list中出现次数最多的元素 def top(list): s=set(list) d={} for i in s: d[i]=list.count(i) print('下面输出的是前k个字典:',end='') print(d) list1=[] for i in d.values(): list1.append(i) ma=max(list1) key_max=get_keys(d,ma) string=key_max[0] return string #get_keys实现已知dict的value返回key def get_keys(d,value): return [k for k,v in d.items() if v==value] if __name__ == '__main__': listTest=[1,1,1,2,2,3,4,5,5,6,6,6,6,6,7] s=top(listTest) print('出现次数最多的元素: ', s)

二、array各种操作

1、Python3中如何自定义结构化数组

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from numpy import * import pandas as pd #通过下面这种方式定义结构数组,自定义结构数组 dtypes={'name':'s32','age':'i','weight':'f'} mydata=pd.DataFrame([['zhang',32,65.5],['wang',24,55.2]],columns=['name','age','weight']) print(mydata) t=mydata.shape for i in mydata.columns: print('') for j in range(mydata.ndim): print(' '+str(mydata[i][j]),end='')

2、array切片操作

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from numpy import * a=arange(10)**3 for element in a.flat: print(' %d' %element,end='') print('') for i in range(a.size): print(' %d' %a[i],end='') print('') print(a[2:5]) #数组的切片处理 a[:6:2]=-1000 #省略的位置代表0 print(a) m=a[: :-1] #将一维数组反转 print(m)

三、dict各种操作

1、如何根据dict字典的value反去除key

复制代码
1
2
def get_keys(d,value): return [k for k,v in d.items() if v==value]

2、dict中存取key、value各种函数使用

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import operator a_dict={1:{'name':'Mary'},2:'python',3:'google','email':'qq.com'} print(a_dict) print(a_dict.items()) #字典的三个函数 keys()、values()、items() print(a_dict.keys()) print(a_dict.values()) print(a_dict.items()) #两种遍历dict中key的方式 for k in a_dict.keys(): print(k) for k in a_dict: print(k) print() #两种遍历dict中value的方式 for v in a_dict.values(): print(v) for k in a_dict.keys(): print(a_dict[k]) print() #Python字典调用items()函数以列表返回可遍历的(键,值)元组数组 for k,v in a_dict.items(): print(str(k)+' : '+str(v)) for k in a_dict: print(str(k)+' : '+str(a_dict[k])) print() #get函数的使用,用来取出dict的value的 for k in a_dict.keys(): print(a_dict.get(k)) print('字典的存储的数据量为: %d' %len(a_dict))

四、set各种操作

1、set声明操作集合和list之间转化

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np import operator #set中只存储key,不存储value,并且key不能够重复 #下面给出Python中声明set的方法 s1=set([]) while len(s1)!=5: a=np.random.randint(0,10) s1.add(a) print(s1) s2=set([]) for i in range(10): s2.add(i) print(s2) #两个set进行相减操作 s3=s2-s1 print(s3) #将set转化成list list1=list(s1) list2=list(s3) for i in range(len(list1)): print(list1[i]) for j in range(len(list2)): print(list2[j])

五、字符串操作

1、Python中字符串相等判断

复制代码
1
2
3
4
5
6
7
str1='csdn' str2='csdn' #Python中和Java不同,字符串相等直接使用‘==’ if str1==str2: print('相等') else: print('不相等')

2、将文本中有效单词取出,过滤掉空格和其他符号

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import re #在表示完整的文件路径需要在前面加 r file_name = r'E:pythonPython_projectmachine learningbayesemailham23.txt' lines_count = 0 words_count = 0 chars_count = 0 words_dict = {} lines_list = [] with open(file_name, 'r') as f: print(f) for line in f: #print('line: ',line) lines_count = lines_count + 1 chars_count = chars_count + len(line) #这里的findall函数特殊 match = re.findall(r'[^a-zA-Z0-9]+', line) #print('match: ',match) for i in match: # 只要英文单词,删掉其他字符 line = line.replace(i, ' ') #split()返回的是 list lines_list = line.split() #下面的i表示的是单词,所以字典的key是单词,value是单词出现的次数 for i in lines_list: if i not in words_dict: words_dict[i] = 1 else: words_dict[i] = words_dict[i] + 1 print('words_count is %d' %len(words_dict)) print('lines_count is %d' %lines_count) print('chars_count is %d' %chars_count) print(words_dict.keys()) print(words_dict.values()) for k,v in words_dict.items(): print(k,v)

六、json使用

1、Python对象和json对象相互转化

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json #python对象--->json对象 json.dumps(python对象) #Python对象<---json对象 json.loads(json对象) #下面是字典类型的对象和json对象之间的互相转化 d = dict(name='Bob', age=20, score=88) data = json.dumps(d) print('JSON Data is a str:', data) reborn = json.loads(data) print(reborn)

2、利用一个函数定制json序列化

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json #Python中类对象<--->json对象 #利用一个函数定制json序列化 class Student(object): def __init__(self, name, age, score): self.name = name self.age = age self.score = score def __str__(self): return 'Student object (%s, %s, %s)' % (self.name, self.age, self.score) s = Student('Bob', 20, 88) std_data = json.dumps(s, default=lambda obj: obj.__dict__) print('Dump Student:', std_data) rebuild = json.loads(std_data, object_hook=lambda d: Student(d['name'], d['age'], d['score'])) print(rebuild)

七、读取文件操作

1、一次性读取所有文件内容到内存:read()

复制代码
1
2
3
4
5
6
7
8
9
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from datetime import datetime #read函数对于文件过大时,会导致内存爆炸的! with open('test1.txt','r') as f: s=f.read() print('open for read') print(s)

2、每次读取一行文件内容:readline()

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
l=[] try: f=open('test2_data.txt','r') s=f.readline() #每次读取一行文件内容,循环读取 while len(s)!=0: list1=[] list1=s.split('t') #将读取的文件内容保存到list中 l.append(list1) s=f.readline() #print(l) except: if f: f.close()

3、一次性读取所有文件内容但是按行返回list:readlines() 很好用

复制代码
1
2
3
4
f=open('testSet.txt') for line in f.readlines(): lineList=line.strip().split() print(lineList)

4、向文件中写信息

复制代码
1
2
3
4
5
6
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from datetime import datetime with open('test.txt', 'w') as f: f.write('今天是 ') f.write(datetime.now().strftime('%Y-%m-%d'))

八、数据库操作

1、Python数据库的连接模板

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #导入mysql驱动 import mysql.connector #连接mysql数据库 conn=mysql.connector.connect(user='root',password='',db='test') cur=conn.cursor() #查询多条记录 info=cur.fetchmany(5) for ii in info: print(ii) #运行查询的另一种方式 cur.execute("select * from user") values=cur.fetchall() print(values) #提交事务 conn.commit() conn.close() cur.close()

九、TCP网络通讯

1、服务器端server

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import socket,threading,time def tcplink(socket,addr): print('Accept new connection from %s:%s...' %addr) sock.send(b'Welcome!') while True: data=sock.recv(1024) time.sleep(1) if not data or data.decode('utf-8')=='exit': break sock.send(('Hello,%s!' % data.decode('utf-8')).encode('utf-8')) sock.close() print('Connection from %s:%s closed' %addr) if __name__=='__main__': # 创建一个socket: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #监听窗口 #其中IP地址和端口号使用tuple的形式 s.bind(('127.0.0.1',9999)) #开始监听端口 s.listen(5) print('waiting for connection...') #永久循环接受客服端连接 while True: #接受一个新连接 sock,addr=s.accept() #创建新线程处理TCP连接 t = threading.Thread(target=tcplink, args=(sock, addr)) t.start()

2、客服端client

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import socket # 创建一个socket: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #建立连接 s.connect(('127.0.0.1',9999)) #接受欢迎消息 print(s.recv(1024).decode('utf-8')) for data in [b'Michael',b'Tracy',b'Sarah']: s.send(data) print(s.recv(1024).decode('utf-8')) s.send(b'exit') s.close()

十、Python协程async

1、Python中协程比使用多线程更高效

如是Python3.5及以上版本,代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import asyncio async def wget(host): print('wget %s...' % host) connect = asyncio.open_connection(host, 80) reader,writer=await connect header = 'GET / HTTP/1.0rnHost: %srnrn' % host writer.write(header.encode('utf-8')) await writer.drain() while True: line=await reader.readline() if line== b'rn': break print('%s header > %s' % (host, line.decode('utf-8').rstrip())) writer.close() loop = asyncio.get_event_loop() tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']] loop.run_until_complete(asyncio.wait(tasks)) loop.close()

如果是Python3.4的版本,代码如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import asyncio @asyncio.coroutine def wget(host): print('wget %s...' % host) connect = asyncio.open_connection(host, 80) reader, writer = yield from connect header = 'GET / HTTP/1.0rnHost: %srnrn' % host writer.write(header.encode('utf-8')) yield from writer.drain() while True: line = yield from reader.readline() if line == b'rn': break print('%s header > %s' % (host, line.decode('utf-8').rstrip())) # Ignore the body, close the socket writer.close() loop = asyncio.get_event_loop() tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']] loop.run_until_complete(asyncio.wait(tasks)) loop.close()

以上内容便是Python3.x常用数据结构和常用模板的总结,当然并不可能很全啦,后期如果有比较好的模板还会继续更新,小伙伴们如果有比较好的模板也欢迎添加分享!

最后

以上就是明亮鸭子最近收集整理的关于python3.x编程模板总结的全部内容,更多相关python3内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(90)

评论列表共有 0 条评论

立即
投稿
返回
顶部