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

一、list各种操作

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

#!/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中出现次数最多的那个元素

#!/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中如何自定义结构化数组

#!/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切片操作

#!/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

def get_keys(d,value):return [k for k,v in d.items() if v==value]

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

#!/usr/bin/env python3
# -*- coding: utf-8 -*-import operatora_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之间转化

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中字符串相等判断

str1='csdn'
str2='csdn'
#Python中和Java不同,字符串相等直接使用‘==’
if str1==str2:print('相等')
else:print('不相等')

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

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
#在表示完整的文件路径需要在前面加 r
file_name = r'E:\python\Python_project\machine learning\bayes\email\ham\23.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 + 1chars_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()返回的是 listlines_list = line.split()#下面的i表示的是单词,所以字典的key是单词,value是单词出现的次数for i in lines_list:if i not in words_dict:words_dict[i] = 1else:words_dict[i] = words_dict[i] + 1print('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对象相互转化

#!/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序列化

#!/usr/bin/env python3
# -*- coding: utf-8 -*-import json#Python中类对象<--->json对象
#利用一个函数定制json序列化
class Student(object):def __init__(self, name, age, score):self.name = nameself.age = ageself.score = scoredef __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()

#!/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()

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() 很好用

    f=open('testSet.txt')for line in f.readlines():lineList=line.strip().split()print(lineList)

4、向文件中写信息

#!/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数据库的连接模板

#!/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

#!/usr/bin/env python3
# -*- coding: utf-8 -*-import socket,threading,timedef 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':breaksock.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

#!/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及以上版本,代码如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-import asyncioasync def wget(host):print('wget %s...' % host)connect = asyncio.open_connection(host, 80)reader,writer=await connectheader = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % hostwriter.write(header.encode('utf-8'))await writer.drain()while True:line=await reader.readline()if line== b'\r\n':breakprint('%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的版本,代码如下

#!/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 connectheader = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % hostwriter.write(header.encode('utf-8'))yield from writer.drain()while True:line = yield from reader.readline()if line == b'\r\n':breakprint('%s header > %s' % (host, line.decode('utf-8').rstrip()))# Ignore the body, close the socketwriter.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编程模板总结相关推荐

  1. 创建模板_UG中如何创建属于自己的编程模板界面?

    点击关注 不迷路 ◆UG12如何实现多窗口显示部件 ◆[回转]命令 ◆实体建模工具拉伸 ◆UG12如何提醒自动保存时间 ◆UG12如何对实体产品剖视 先给大家出一道感性的推理题: 从前,有一个被巫师施 ...

  2. Python3 异步编程之进程与线程-1

    Python3 异步编程之进程与线程-1 一.了解进程间通信 进程间通信 进程 线程 线程 vs 进程 IO模型 并发 vs 并行 异步 vs 同步 二.多线程与多进程的用法 计算密集型 vs I/O ...

  3. 单机编程c语言,完美的8051单机C语言编程模板.doc

    完美的8051单机C语言编程模板 <8051单片机C语言编程模板> [程序开始处的程序说明] /********************************************** ...

  4. STM32F103五分钟入门系列(一)跑马灯(库函数+寄存器)+加编程模板+GPIO总结

    摘自:STM32F103五分钟入门系列(一)跑马灯(库函数+寄存器)+加编程模板+GPIO总结 作者:自信且爱笑' 发布时间: 2021-04-28 21:17:40 网址:https://blog. ...

  5. 大学c语言编程模板,c语言编程模板

    <c语言编程模板>由会员分享,可在线阅读,更多相关<c语言编程模板(8页珍藏版)>请在人人文库网上搜索. 1.单片机C语言编程模板(基础模板) 程序开始处的程序说明 /* * ...

  6. Python3——网络编程基础

    Python3--网络编程基础 基础知识参考: https://blog.csdn.net/wqx521/article/details/51037048 https://blog.csdn.net/ ...

  7. [进阶] --- Python3 异步编程详解(史上最全篇)

    [进阶] - Python3 异步编程详解:https://blog.csdn.net/lu8000/article/details/45025987 参考:http://aosabook.org/e ...

  8. 引用另一模板的宏_生信人值得拥有的编程模板Shell

    前言 "工欲善其事必先利其器",生信工程师每天写代码.搭流程,而且要使用至少三门编程语言,没有个好集成开发环境(IDE,Integrated Development Environ ...

  9. 杜洋单片机C语言编程组成,8051单片机C语言编程模板

    c语言 本文由fan159147贡献 doc文档可能在WAP端浏览体验不佳.建议您优先选择TXT,或下载源文件到本机查看. <8051 单片机 C 语言编程模板> 杜洋 2009.7 [程 ...

  10. python编程-Python3 网络编程

    Python3 网络编程 Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口 ...

最新文章

  1. 深度解析KGDB调试Linux模块和内核
  2. Android应用中使用AsyncHttpClient来异步网络数据
  3. 市场定位和硬件设计的错误-浅谈GM8126的封装
  4. 常量缓存与integer比较_Integer缓存范围到底是多少?
  5. Linux开机启动过程(8):初期中断(缺页中断)和异常处理
  6. python dataframe将字符转换为数字_python中如何将华氏温度转换为摄氏温度?
  7. 堆排序的Java实现
  8. VSCode创建vue模板(快捷方便)
  9. 公摊面积用计算机怎么计算,公摊面积计算(公摊面积计算器)
  10. IELTS4-15 LISTENING (Fill in the blanks)
  11. Quorum NWR算法
  12. “千亿市值”巨无霸的膨胀 腾讯靠什么撬动下一个1000亿美金?
  13. NDN命名网络工作机制和优点
  14. 导出https网站证书
  15. 工业交换机与商用交换机的区别有哪些
  16. 搭建完整的开发环境--Linux下的开发
  17. python实数绝对值的计算循环操作_Python复数属性和方法运算操作示例
  18. 【2017秋季校园招聘笔经面经专题汇总】
  19. 王半仙儿的日记-0002
  20. 网络通讯端口为什么要设计浪涌保护电路

热门文章

  1. APICloud连接夜神模拟器--博客园老牛大讲堂
  2. mysql损坏打不开_mysql 断电导致表打不开解决方案
  3. 云服务器怎么多人进去编辑文档,服务器如何设置多人登陆
  4. A[1054] The Dominant Colour (map!!)
  5. c语言中用了double语句,求助 C语言 中 double语句用法
  6. 《C++(一)--类》
  7. daemons java_Java ThreadGroup isDaemon()方法
  8. 线接触和面接触的区别_接触器是啥?跟继电器有啥区别,6大常见故障怎么处理...
  9. python %号_python基础集结号
  10. 机器学习- 吴恩达Andrew Ng Week4 神经网络Neural Networks知识总结