随笔分类 - python
摘要:对list中的dict对象去重 方案1: 依据是dict对象的某个键值对的value 原理:利用dict健的不可重复行 结果:保留最新的数据 def unique_list(list_obj, primary_key): list_obj_dict = {i.get(primary_key): i
阅读全文
摘要:设置时间限制,等待任务结束 import time print('start task and wait') waitflag = 0 timeline = 10 while True: time.sleep(60) print('check process and get result') tas
阅读全文
摘要:获取本机外网ip 方法一: 浏览器访问https://ip138.com/即可获知 方法二: 浏览器访问http://api.ipify.org/即可获知 import requests url = 'http://api.ipify.org/' content = requests.get(url
阅读全文
摘要:# map(function, iterbale) 返回map对象,可list转化,不可tuple转化 # 输入 iterable对象数量没有限制 vs 输出对象取决于最短的输入iterable。 ret = map(lambda x, y: x + y, [1, 3, 5, 7, 9, 20],
阅读全文
摘要:while True: paper_count = input("请输入要下载的5K超清壁纸的数量:") if paper_count.isnumeric() and int(paper_count) > 0: paper_count = int(paper_count) break
阅读全文
摘要:from queue import Queue # 队列 先进先出 q = Queue() for i in range(5): q.put(i) while not q.empty(): print(q.get()) 0 1 2 3 4 from queue import LifoQueue #
阅读全文
摘要:all_dat_str = '1213124123425' # 写入信息 with open('音频信息1111111111111', 'w') as f: print('写入信息的长度为 ', f.write(all_dat_str)) # 确认有信息 with open('音频信息1111111
阅读全文
摘要:import redis def create_redis_pool(): REDIS_DB = '127.0.0.1' REDIS_PORT = 6379 pool = redis.ConnectionPool(host=REDIS_DB, port=REDIS_PORT, max_connect
阅读全文
摘要:# 加u说明编码的方式 data_str = u'中文测试' print('加u,说明是unicode编码的变量 ', len(data_str)) 加u,说明是unicode编码的变量 4 # 禁止\的转义功能 r_str = r'\n' o_str = '\\n' print('加r,去掉反斜杠
阅读全文
摘要:# -*- coding: utf-8 -*- list1 = [1, 2, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5] list2 = [5, 5, 6, 7, 8, 9] # set集合 # 差集--两种情况 print(list(set(list1) - set(list2))
阅读全文
摘要:# -*- encoding=utf-8 -*- from itertools import combinations,permutations test_data = {'a', 'b', 'c'} # 排列 -- 关注排列方式 print('排列有:{}'.format(list(permuta
阅读全文
摘要:``` # 第一种: number = 11223344556677889900 # 反转1,reverse()方法 # list1 = list(str(number)) # list1.reverse() # 反转2, 切片 list1 = list(str(number))[::-1] print('反转后的', list1) # 删除 # 删除1, 直接remove元素 list1.rem
阅读全文
摘要:``` # -*- coding: utf-8 -*- import cv2 # 读取图片 img = cv2.imread('phone.png') # h、w为想要截取的图片大小 h = 35 w = 170 x = 500 y = 425 cropImg = img[(y):(y + h), (x):(x + w)] cv2.imwrite('test.png', cropImg) ```
阅读全文
摘要:``` import time from multiprocessing import Process start_time = time.time() def get_data(): while True: time.sleep(2) print('休眠2秒钟') def jisuan(): print('开始计算,用时10秒') time.sleep(10) print('结束计算,用时10秒
阅读全文
摘要:python高级编程和异步io并发编程 第一章 1. 导学 python进阶方法 阅读(库和框架)源码 优化代码 目标:python高级和并发编程 方法:功能 原理 应用案例 课程安排:元类、多线程、多进程、异步IO、asyncio 重要 2. 开发环境配置 第二章(一切皆对象) 1. python
阅读全文
摘要:```class Demo(object): __instance = None def __new__(cls, *args, **kwargs): if cls.__instance is None: cls.__instance = object.__new__(cls) return cls.__instance ...
阅读全文