![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python学习
冬日and暖阳
Github: https://github.com/pengfeidip
邮箱:pengfeidip@qq.com
展开
-
python json load文件报错
json文件不完整导致load错误原创 2022-07-16 19:36:05 · 935 阅读 · 0 评论 -
python pathlib的使用
python pathlib原创 2022-06-10 16:12:20 · 461 阅读 · 0 评论 -
python: numba 加速python的 for loop
numba对python加速原创 2022-06-06 18:25:42 · 1797 阅读 · 0 评论 -
python读写json
python读取json原创 2022-06-06 16:50:10 · 277 阅读 · 0 评论 -
linux下python输出彩色字体
https://blog.csdn.net/zhangyixin_618/article/details/89526717转载 2021-08-02 16:15:47 · 238 阅读 · 0 评论 -
python 刷新输出/输出进度
原理:每次输入后,移动光标,然后覆盖之前的输出#Try this:import sysimport timefor i in range (10): sys.stdout.write('\r'+str(i)) time.sleep(0.5)原创 2021-07-26 09:10:42 · 991 阅读 · 0 评论 -
python读取yaml文件
# 安装yaml的语句是: pip install pyyamlimport yamlwith open('yolov5s.yaml') as f: info = yaml.load(f.read()) 参考:https://www.cnblogs.com/klb561/p/10085328.html原创 2021-05-12 16:07:18 · 94 阅读 · 0 评论 -
python2 浮点数FP32转化为十六进制,并以字符串输出
def float_to_hex(f): return hex(struct.unpack('<I', struct.pack('<f', f))[0]) def hex2str(h): return str(h)[2:] print(hex2str(float_to_hex(np.float(17.5))))原创 2021-03-04 15:34:07 · 855 阅读 · 1 评论 -
python同时将信息保存在日志文件里面并打印出来
需求 服务器执行程序的时候,打印到终端,并且保存打印的信息。代码如下import syssys.stdout = open('logfile', 'w')class Tee(object): def __init__(self, *files): self.files = files def write(self, obj): for f in self.files: f.write(obj)f = open('logf原创 2020-11-27 17:02:08 · 408 阅读 · 0 评论 -
python / matploylib 绘制水平柱状图
记录一下matplotlib绘制水平 柱状图的代码import matplotlib.pyplot as plt# 构建数据plt.figure(figsize=(12, 8))x_data = keys_sety_data = res_precisiony_data2 = vgg_precisionbar_width=0.3plt.barh(range(len(x_data))...原创 2019-11-05 09:23:32 · 2560 阅读 · 0 评论 -
python/matplotlib绘制统计直方图
记录一下绘制统计直方图的代码for k, v in area_dict.items(): plt.figure() plt.hist(v, bins=40, density=0, facecolor="blue", edgecolor="black", alpha=0.7) plt.xlabel("Area") plt.ylabel("Number") ...原创 2019-11-05 09:21:28 · 834 阅读 · 0 评论 -
matplotlib/plt 函数savefig保存的图像有空白
问题:最近在作图时需要将输出的图片紧密排布,还要去掉坐标轴,同时设置输出图片大小。但是发现matplotlib使用plt.savefig()保存的图片周围有一圈空白。那么如何去掉该空白呢?首先,关闭坐标轴显示:plt.axis('off')但是,这样只是关闭显示而已,透明的坐标轴仍然会占据左下角位置,导致输出的图片偏右。要想完全去掉坐标轴,需要改为以下代码:plt.axis('off...转载 2019-10-17 10:45:01 · 1182 阅读 · 0 评论 -
pytorch-unsqueeze用法
tensor.unsqueeze 为tenor添加维度a = torch.rand((1, 3, 5), dtype=torch.float32)print("a shape is ", a.size())b = a.unsqueeze(-1) # 负1表示 在最后一维上添加print("b shape is ", b.size())输出结果如下a shape is torch...原创 2018-12-07 21:06:01 · 961 阅读 · 0 评论 -
pytorch tensor division/除法
除法的时候要注意数据类型的问题a = torch.div(torch.tensor([160, 110]), 0.137)jupyter notebook就会奔溃, 因为torch.tensor([160, 110])是torch.int64数据类型, 而0.137(或者说torch.tensor(0.137))是torch.float32类型.如果改成下面这样, 就会顺利运行a = t...原创 2018-11-26 11:03:15 · 14382 阅读 · 0 评论 -
opencv 读取图像,解决imread不能读取中文路径的问题
opencv-python 无法读取中文距离# 假设 im_name是中文路径im = cv2.imdecode(np.fromfile(im_name,dtype=np.uint8),-1) # 读取的数据是RGB 而不是 BGR, 要注意原创 2018-12-07 10:50:40 · 4142 阅读 · 3 评论 -
python list 转化为 int/ 含有整形字符的list转化为整形list
results = ['1', '2', '3'] 转化为 [1, 2, 3]利用map 函数:results = list(map(int, results))原创 2018-12-09 16:48:23 · 16669 阅读 · 1 评论 -
python str类型 startswith()方法
Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。语法startswith()方法语法:str.startswith(str, beg=0,end=len(string));参数str -- 检测的字符串。strbeg -- 可选参数用于设置字符串检...原创 2018-12-23 19:35:56 · 1421 阅读 · 2 评论 -
python itertools product的用法
product 是一个很好用的函数一个简单的用例for i in product(range(3), repeat=2): print(i)输出(0, 0)(0, 1)(0, 2)(1, 0)(1, 1)(1, 2)(2, 0)(2, 1)(2, 2)这种用法可以用与矩阵的索引...原创 2019-01-02 15:50:15 · 1173 阅读 · 0 评论 -
python 函数默认参数的坑 --- 廖雪峰
默认参数很有用,但使用不当,也会掉坑里。默认参数有个最大的坑,演示如下:要点默认参数等于初始化了一个变量.尽量使用不变对象实例先定义一个函数,传入一个list,添加一个END再返回:def add_end(L=[]): L.append('END') return L当你正常调用时,结果似乎不错:>>> add_end([1, 2, 3])...转载 2019-01-16 10:05:01 · 219 阅读 · 0 评论 -
python的驻留机制
python 中 a is b 等价于id(a) == id(b),因此is 与 ==是不同的。a = 'abc'b = 'abc'print(id(a))# 3206115244944print(id(b))# 3206115244944根据 内存id来看,二者指向同一块内存。这就是python的驻留机制,但驻留机制是有局限的。它只对数字0 - 9, 字母a - z,已经下划线...原创 2019-03-21 11:58:01 · 1410 阅读 · 0 评论 -
python 读写csv有空行
问题读出来的CSV文件有空的列表原因写入csv文件时有问题有问题的写入代码:with open('sd.csv', 'w+', ') as f: a = csv.writer(f) a.writerows(info)正确的写入代码:with open('sd.csv', 'w+', newline='') as f: a = csv.writer(f) ...原创 2019-07-15 14:22:38 · 854 阅读 · 0 评论 -
Python拷贝和视图
转载自:https://blog.csdn.net/wangwenzhi276/article/details/53428353— 1. 完全不拷贝2. 视图或浅拷贝3. 深拷贝当对数组进行运算和操作时,其数据有时会被拷贝到一个新的数组而有时又不会拷贝。这一点常常对刚使用NumPy的用户造...转载 2018-06-01 20:05:33 · 1425 阅读 · 1 评论 -
python __getitem__() 方法
如果在类中定义了__getitem__()方法,那么他的实例对象(假设为P)就可以这样P[key]取值。当实例对象做P[key]运算时,就会调用类中的__getitem__()方法。class DataTest: def __init__(self,id,address): self.id=id self.address=address s...转载 2018-11-22 08:54:30 · 2200 阅读 · 4 评论 -
python __call__() 与__init__()
在Python的class中有一些函数往往具有特殊的意义。__init__()和__call__()就是class很有用的两类特殊的函数。__init__()在Python中,__init__()函数的意义等同于类的构造器(同理,__del__()等同于类的析构函数)。因此,__init__()方法的作用是创建一个类的实例。__call__()Python中的函数是一级对象。这意味着Pyt...转载 2018-11-21 17:55:11 · 183 阅读 · 0 评论 -
python万物皆对象
偶然见到enumerate把一个自建类的示例给打包了, 有点惊奇,然后做了以下实验:def my_add(x, y): return x+ydef my_substract(x, y): return x-ydef my_multiply(x, y): return x*yfor ind, func in enumerate([my_add, my_subst...原创 2018-11-21 17:40:17 · 315 阅读 · 0 评论 -
anaconda conda 切换为国内源
转载自:https://blog.csdn.net/dream_allday/article/details/80344511windows下在清华源和中科大源之间自行选择1 添加清华源命令行中直接使用以下命令conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free...转载 2018-07-18 19:10:59 · 129930 阅读 · 15 评论 -
python中的 序列copy
The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the a...转载 2018-08-31 16:19:54 · 186 阅读 · 0 评论 -
python安装以及卸载
python安装我安装的是python3.5.2\color{Blue}{\text{python3.5.2}}(以后描述的相关操作, 均是基于此版本),具体网址: https://www.python.org/downloads/release/python-352/安装的时候会自动添加以下功能自动添加环境变量(看了下,貌似是添加到了用户变量中,并非系统变量, 建议还是自己添加一下原创 2018-01-22 11:06:28 · 22423 阅读 · 0 评论 -
python直接读取tar文件中的图片
背景: tar文件过大,解压缩太慢又占空间,希望直接读取其中的图片分析:分析:\color{Red}{\text{分析:}} 涉及到两方面: 1. 获取其中的文件名; 2. 读取其中的文件具体操作:具体操作:\color{Red}{\text{具体操作:}} 1. 利用 tarfile 模块读取其中文件名 #假设文件名为 n01440764.tar 并置于E盘下i...原创 2018-08-27 21:06:27 · 7923 阅读 · 0 评论 -
python中的一维数组作为index array
python很注重形式/格式 比如靠缩进来确定代码块再比如,一维数组的定义import numpy as nprank_1 = np.array([1, 2, 3]) #print(rank_1.shape)"""这是一个一维数组 将会输出: (3,)(3,) 是不是看上去很奇怪,但是这很形象的表示这个数组只有一个维度!"""对于一维数组 习惯了以后也还可以,但是对于”精...原创 2018-06-04 18:30:02 · 3080 阅读 · 1 评论 -
opencv + python
本人版本:python 3.5.2 + 64bit -windows 10python 3.5.2 + 64bit -windows 10\color{Blue}{\text{python 3.5.2 + 64bit -windows 10}}, 配置的opencv版本是 3.4.03.4.0\color{Red}{\text{3.4.0}}步骤 1:步骤 1:\color{Red}{...原创 2018-03-06 18:08:05 · 385 阅读 · 0 评论 -
Python的一些操作
PythonPythonBasic ConceptsControl Structuresimport module导入模块from module import funtion导入指定函数1 Basic Conceptsfloatspython 中forward slash“/”代表除法, 得到的是float 型的数值。 e.g. 12/6 输出是 2.原创 2018-01-28 14:48:42 · 298 阅读 · 0 评论 -
python中的string类
python中的string数据类型string类的操作 string 可以直接相加,作用相当于字符拼接string 可以利用与 integer 相乘的方法进行复制。>>>"egg" + "chicken"eggchicken>>>4*'egg'eggeggeggegg>>>2*'23''2323'>>>int('2323')#类型转换2323格式化输出.原创 2018-01-28 14:51:21 · 1260 阅读 · 0 评论 -
python中的List/列表的相关操作
列表/List列表List一 列表List的基本概念二 列表的操作List Operations三 列表常用函数List Functions四 for循环遍历List的两中方案一: 列表(List)的基本概念列表是 python 的一种数据类型,功能上比较像matlab里面的cell类型,一个可以存放各种数据类型的’数组。string 可以当做一个原创 2018-01-28 15:28:09 · 1490 阅读 · 0 评论 -
pytthon-range 返回的是一个可迭代的object
A strange thing happens if you just print a range:&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; print(range(10))range(0, 10)In many ways the object returned by range() behaves as if it is a list, b转载 2018-08-31 17:44:42 · 277 阅读 · 0 评论 -
python-函数中变量名的搜过过程
函数中的变量名如何去解释,解释为局部变量?全局?还是什么。。。The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store ...原创 2018-09-01 08:22:24 · 199 阅读 · 0 评论 -
python中的opencv和PIL(pillow)转化
opencv > pilimport cv2 from PIL import Imageimg = cv2.imread("test.png")image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)) pil > opencvimport cv2 from PIL import Image imag...转载 2018-09-26 08:46:37 · 4383 阅读 · 0 评论 -
python中tuple * 和 dict **
偶然见到以下用法 *tuple 和 **dict作为参数, 经过查询,才知道这是一种简要写法, 见下面的例子*tupledef foo(x, y): print(x, y)>>t = (1, 2)>>foo(*t)1 2**dictdef foo(x, y): print(x, y)>>> d = {'x':1, ...原创 2018-11-21 10:05:17 · 345 阅读 · 0 评论 -
python中找到所有指定元素的索引
定义:X=[1,2,3,1,4]任务:找出元素为1的索引Solution:#如果直接用X.index(1),只能得到0这一个索引,而我们需要所有索引.l = len(X)zip_list = zip(*(range(l),X))id1 = [z[0] for i,z in enumerate(zip_list) if z[1]==1]# 或者更简单的id1 = [i for i,...转载 2018-10-29 18:51:30 · 3520 阅读 · 0 评论 -
python:格式化输出,format
想要输出固定位数,并智能补零# 共占3位,靠右对齐"{:3d}".format(5)# 输出: 5# 共占3位,左侧补零"{:3d}".format(5)# 输出:005原创 2018-10-29 14:37:15 · 469 阅读 · 0 评论