python学习
python学习实战
For Coding
这个作者很懒,什么都没留下…
展开
-
在Centos 7中安装Python3环境
在Centos 7中安装Python3环境原创 2022-06-08 21:04:16 · 481 阅读 · 0 评论 -
python实现排列组合问题
[3,32,321],请问有几种组合的方式[[3, 32, 321], [3, 321, 32], [32, 3, 321], [32, 321, 3], [321, 3, 32], [321, 32, 3]]实现方式:# encoding =utf-8num_list = [3, 32, 321]def permute(nums): from itertools import permutations result = [] for i in permutatio原创 2021-03-21 21:50:39 · 305 阅读 · 0 评论 -
python实现两个集合中的公共元素
方式1:如果两个列表有重复元素的from collections import Counterprint(list((Counter([0, 0, 0, 1, 2, 3]) & Counter([0, 0, 0, 3, 4, 5, 6])).elements()))结果:[0, 0, 0, 3]方式2:如果两个列表没有重复元素的print(list(set([1, 2, 3, 4, 5]) & set([3, 4, 7, 8, 9])))结果:[3, 4]print.原创 2021-03-19 09:58:36 · 2007 阅读 · 0 评论 -
python中的进制转换问题总结
# 将字符串转为对应进制下的整型print(int('0100', 2))print(int('0o100', 8))print(int('0xf', 16))print('-------------------')# 10进制转为2进制print(bin(4))# 10进制转为8进制print(oct(5))# 10进制转为16进制print(hex(16))print('-------------------')# 不足4位,前面补0,达到4位print('198'.z原创 2021-03-12 11:49:49 · 114 阅读 · 0 评论 -
求最长公共子串长度与最长公共子串问题/求最长公共子序列长度与最长公共子序列问题
import numpy as npstr1 = 'abaccb'str2 = 'aaccab'tmp_str1 = str1tmp_str2 = str2str1 = '0' + str1str2 = '0' + str2len1 = len(str1)len2 = len(str2)dp = np.zeros((len1, len2), dtype=np.int)for i in range(1, len1): for j in range(1, len2):原创 2021-03-10 15:04:56 · 148 阅读 · 0 评论 -
牛客网Mysql刷题笔记
mysql: 1、判断一个字段是否为奇数/偶数 mod(id,2)=1 奇数 mod(id,2)=0 偶数 select * from employees where mod(emp_no,2)=1 and last_name!='Mary' order by hire_date desc;原创 2021-03-04 15:15:32 · 364 阅读 · 3 评论 -
字符串“123”转为123,不使用内置的api,例如int()
def reserve(num): if num < 0: s_num = str(num) ss_num = s_num[1:] temp = 0 for item in ss_num: temp = temp * 10 + ord(item) - ord('0') return -temp else: s_num = str(num) temp原创 2021-03-01 14:57:08 · 632 阅读 · 0 评论 -
python中的正则表达式
"""match和search的区别: 1、共同点: 1)只对字符串查询一次 2)返回值类型都是re.Match类型 2、不同点: 1)match是从头开始匹配,一旦匹配失败,就返回None 2)search是在整个字符串里面进行匹配finditer:查找到所有的匹配数据放到一个可迭代对象里,返回的结果是一个可迭代对象(re.Match类型)findall:把匹配到的所有字符串结果放到一个列表里fullmatc原创 2021-02-28 16:51:03 · 69 阅读 · 0 评论 -
二叉搜索树(二叉排序树)
二叉排序树class BiTreeNode: def __init__(self, data): self.data = data self.lchild = None # 左孩子 self.rchild = None # 右孩子 self.parent = Noneclass BST: def __init__(self, li=None): self.root = None if原创 2021-02-05 16:54:14 · 126 阅读 · 0 评论 -
python中的深拷贝与浅拷贝的区别
python中的深拷贝与浅拷贝的区别复合对象深浅拷贝是有区别的:普通对象深浅拷贝是没有区别的:原创 2021-02-18 21:03:15 · 68 阅读 · 0 评论 -
python实现定时发送邮件
import scheduleimport timeimport smtplibfrom smtplib import SMTP_SSLfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.application import MIMEApplicationfrom email.header import Headerhost_server = 's原创 2020-12-21 16:29:26 · 276 阅读 · 1 评论 -
Python练习统计一个类一共创建了几个对象
# 练习一个类一共创建了几个对象class Person(): count = 0 def __new__(cls, *args, **kwargs): cls.count += 1 return object.__new__(cls) def __init__(self, name, age): self.name = name self.age = agep1 = Person('zhangsan', 2原创 2020-11-30 16:33:17 · 1353 阅读 · 1 评论 -
python中的单例设计模式的实现
class Singleton(): instance = None def __new__(cls, *args, **kwargs): if cls.instance is None: cls.instance = object.__new__(cls) return cls.instance def __init__(self, a, b): self.a = a self.b = b原创 2020-11-30 16:28:16 · 45 阅读 · 0 评论 -
python中面向对象中类的私有属性
class Person(): """ python类中的私有属性 """ def __init__(self, name, age, city): self.name = name self.age = age # 定义类中的私有属性 self.__city = city def set_city(self, city_name): self.__city = city_name原创 2020-11-30 11:23:00 · 133 阅读 · 0 评论 -
python中的uuid的使用
import uuid# uuid1是基于mac地址,时间戳,随机数来生成唯一的uuid,可以保证全球范围内的唯一性print(uuid.uuid1())# 通过伪随机数得到uuid,具有一定的概率重复的(开发使用的最多的)print(uuid.uuid4())# 生成固定的uuid,uuid3和uuid5类似,只是采用算法不同,uuid3采用的是md5,uuid5采用的是sha1print(uuid.uuid3(uuid.NAMESPACE_DNS, 'zhangsan'))print原创 2020-11-30 09:24:43 · 321 阅读 · 0 评论 -
python中的hashlib与hmad库加密
hashlib和hmac模块,通常用于加密,用的还是比较多的 # hashlib模块里主要支持两个算法:md5和sha 加密 # 加密方式:单向加密,只有加密的过程,没有解密的过程,md5/sha,对称加密,非对称加密。import hashlibimport hmac# hashlib加密md5 = hashlib.md5('abc'.encode('utf-8'))print(md5.hexdigest())sha224 = hashlib.sha3_224('abc'.encode('原创 2020-11-30 09:08:51 · 70 阅读 · 0 评论 -
Python中的互斥锁
# 互斥锁import jsonimport randomimport timefrom multiprocessing import Process, Lock# 查询票def search(): with open('data', mode='r', encoding='utf8') as f: data = json.load(f) print('现在车票还剩余:%s' % (data.get("ticket_num")))# 购票def原创 2020-06-28 11:08:23 · 170 阅读 · 0 评论 -
python的类的定义
# 创建一个类class Person: def __init__(self, name, age, address, email): self.name = name self.age = age self.address = address self.email = emailalex = Person('alex', 21, '北京', 'alex@163.com')print(alex.__dict__)# 修改属原创 2020-06-27 08:44:39 · 128 阅读 · 0 评论 -
Python在面试中问的代码块与缓存机制
Python在面试中问的代码块与缓存机制:1、同一代码块下适用一个缓存机制。不同的代码下适用另一个缓存机制(小数据池)2、小数据池:数字的范围是-5~2563、缓存机制的优点:提神性能,节省内存...原创 2020-05-18 19:28:05 · 117 阅读 · 0 评论