python
math_computer
一个热爱数学和雨点的嗯,还没想好是什么
展开
-
python中print输出到文件中
linux中使用python program.py 2>&1 | tee file.log # 同时输出到shell和file.log中 windows中可以在program.py中加入如下语句f = open(file.log, "w")...print("xxx", file=f)...print('yyy', file=f)...f.close()如果觉得总要加上file=f烦,可以考虑最后使用正则表达式一起操作。应该有更好的办法,欢迎评论~...原创 2020-05-08 19:23:15 · 798 阅读 · 0 评论 -
一次关于sklearn-crfsuite的安装之旅
为了在windows上跑CRF,我需要安装sklearn-crfsuite,最开始想到的是使用pycharm进行环境配置,装上了sklearn-crfsuite。跑起来,额,出错了:正在训练评估CRF模型...Traceback (most recent call last): File "C:/Users/cc/Documents/xxx/yyy/window_version/main....原创 2020-04-30 09:35:39 · 5281 阅读 · 8 评论 -
python中numpy的axis和torch的dim
举一个例子:import torchA = torch.rand((3,4))print(A)#tensor([[0.3602, 0.2583, 0.1758, 0.3575],# [0.9582, 0.2092, 0.6829, 0.8663],# [0.3922, 0.1360, 0.3733, 0.3477]])z = A.sum(dim=1, ke...原创 2020-04-24 10:25:35 · 722 阅读 · 1 评论 -
python中的转义字符的应用
\r转义,控制光标python中\r的意义及用法原创 2020-04-23 10:52:37 · 250 阅读 · 0 评论 -
pycharm中将python2的print全部转化为python3的print()
方法步骤:Ctrl + R,勾选Regex处理仅仅只有print的情形,使用print("")替换掉print$处理print+空格+输出的情形,使用print($1)替换掉print\s+(.*?)$(*?表示非贪婪模式)...原创 2020-04-20 20:38:48 · 871 阅读 · 1 评论 -
字符串格式化方法
python3print(f"{3:0>4}") # 0003unix shellprintf "record_%04d" 3 # record_0003原创 2020-04-02 15:51:34 · 116 阅读 · 0 评论 -
Python输出原始字符串(不转义)
使用repr函数ent = "here is a book.\n"print(repr(ent)) # "here is a book.\n"原创 2020-04-03 15:10:28 · 8231 阅读 · 2 评论 -
python动态导入模块、函数、类、对象
看kennethreitz写的pip-pop,运行时发现我的pip版本19.1太高了,需要改一下他的代码。这个过程就得考虑动态导入了。以下为修改后的bin/pip-grep文件开头部分import osfrom docopt import docoptimport importlib# from pip.req import parse_requirements# from pip.i...原创 2020-01-20 19:45:27 · 311 阅读 · 0 评论 -
matplotlib的usage guide学习
目录参考层级对象figure多个figure,多个axes一个figure,没有axesaxes参考讲了一下“state-machine environment”,帮我理解了这个概念https://blog.csdn.net/byron123456sfsfsfa/article/details/83035090层级最高级:the matplotlib “state-machine envi...原创 2019-12-21 19:20:18 · 153 阅读 · 0 评论 -
python中的随机数生成
参考:random模块官方文档:https://docs.python.org/3/library/random.htmlrandom模块原理:使用Mersenne Twister作为伪随机数生成器,它是完全确定的,不适合于密码领域,密码中随机数需要使用模块secrets。同时,它有周期。(这里不太理解)“Almost all module functions depend on the ...原创 2019-12-20 18:01:14 · 250 阅读 · 0 评论 -
python正则表达式
参考:官方文档https://docs.python.org/zh-cn/3.7/library/re.html怎么用https://docs.python.org/zh-cn/3.7/howto/regex.html#regex-howto基本梳理和基本例子https://www.cnblogs.com/peng8098/p/python_basis_6.html实际例子http...原创 2019-12-11 14:21:13 · 160 阅读 · 0 评论 -
python数据处理过程——房价预测
参考:https://blog.csdn.net/Amy_mm/article/details/79538083https://www.kaggle.com/pmarcelino/comprehensive-data-exploration-with-python(处理回归)https://www.kaggle.com/pmarcelino/data-analysis-and-fea...原创 2019-11-12 22:53:40 · 1740 阅读 · 0 评论 -
python和selenium配合自动登录百度
参考:下载chromedriver地址:http://chromedriver.storage.googleapis.com/index.html或者http://npm.taobao.org/mirrors/chromedriver/配置chromedriver:https://jingyan.baidu.com/article/f7ff0bfcdd89ed2e27bb1379.html...原创 2019-11-11 18:09:53 · 138 阅读 · 0 评论 -
python中的字符编码
参考:https://blog.csdn.net/joyfixing/article/details/79971667优秀博客收集https://www.jianshu.com/p/c2b401f7bb78编码基础原创 2019-11-01 14:55:48 · 63 阅读 · 0 评论 -
python写txt
参考:https://www.jb51.net/article/137644.htmf = open('E:/test.txt','w') f.write('hello world!') f.close()f = open('E:/test.txt','a') f.write('the second writing...') #会接着写同一行,不会换行的f....原创 2019-10-28 10:59:58 · 120 阅读 · 0 评论 -
python字符串基本操作
字符串列表合并s = ['ab', 'c', 'd']s1 = ''.join(s) # s1 = 'abcd'原创 2019-10-24 23:51:21 · 166 阅读 · 0 评论 -
pandas 基本操作
建立pandas的DataFrame对象df = pd.DataFrame({'name': ['Raphael', 'Donatello'], 'mask': ['red', 'purple'], 'weapon': ['sai', 'bo staff']}) #使用列表的字典,keys为columns读取c...原创 2019-10-24 18:38:11 · 492 阅读 · 0 评论 -
Python 解决写入csv中间隔一行空行问题
参考:https://www.cnblogs.com/littlebob/p/9293931.htmlwith open(birth_weight_file,'w',newline='') as f: writer=csv.writer(f) writer.writerow(birth_header) writer.writerows(birth_data) ...原创 2019-10-15 18:15:17 · 460 阅读 · 0 评论 -
利用openslide-python处理病理
参考:博客总结https://www.jianshu.com/p/bd5b572b5269官方文档:https://openslide.org/api/python/#module-openslide获取元信息,如每个像素有多少微米import openslidewsi = openslide.Openslide(wsi_file)mpp_x = wsi_ob.proper...原创 2019-09-23 10:58:51 · 3061 阅读 · 0 评论 -
albumentation库中的数据变换方法
源码:https://github.com/albu/albumentations在图像分割中的示例:https://github.com/albu/albumentations/blob/master/notebooks/example_kaggle_salt.ipynb在图像分类中的示例:https://github.com/albu/albumentations/blob/maste...原创 2019-08-24 18:27:19 · 1957 阅读 · 0 评论 -
python中的序列化和反序列化pickle和json
参考:https://blog.csdn.net/coffee_cream/article/details/51754484#https://blog.csdn.net/ryuhfxz/article/details/829738931.先看picklepickle是以二进制方式序列化所有代码在python3.6(win10)上测试。用于在磁盘上以持久形式存储python数据...原创 2019-07-03 15:44:33 · 99 阅读 · 0 评论 -
python图像处理库PIL
参考:https://www.cnblogs.com/ssyfj/p/9051734.htmlhttps://blog.csdn.net/u010417185/article/details/74357382(先挖坑)原创 2019-09-23 10:56:39 · 152 阅读 · 0 评论 -
读取灰度图片的cv2方法和skimage方法
cv2方法:image = cv2.imread(file)返回:numpy of shape (height, width, 3), image[0]=image[1]=image[2]skimage方法image = skimage.io.imread(file)返回:numpy of shape (height, width)原创 2019-09-05 16:14:57 · 1364 阅读 · 0 评论 -
windows安装python
参考:https://pythonguidecn.readthedocs.io/zh/latest/starting/install/win.html原创 2019-07-23 18:31:18 · 1307 阅读 · 0 评论 -
windows下设置别名区分Python2和Python3
参考:https://blog.csdn.net/fancychuan/article/details/88802696https://www.cnblogs.com/fstang/archive/2013/04/06/3002006.html问题:在windows10上面安装了python2和python3后,在命令行中如何方便调用?解决:设置别名python2和python3...原创 2019-07-23 18:29:47 · 949 阅读 · 0 评论 -
python常用文件(夹)操作
os.makedirs(dir_path):创建所有dir_path涉及的目录 os.listdir(dir_path):返回dir_path中文件及文件夹名的列表例子:#coding:utf8import osdir_path = "D:\文档"print(os.listdir(dir_path))输出结果:['文章复现(1).docx', 'Radiomics','...原创 2019-07-18 14:57:12 · 89 阅读 · 0 评论 -
在python中利用pipe实现管道
参考书:张颖,赖勇浩,编写高质量代码:改善python程序的91个建议所有代码在python3.6(win10)上测试通过。例子:from pipe import *import operatorfrom functools import reduce# basic exampley = range(1,6) | where(lambda x: x % 2) | tail(...原创 2019-07-06 12:32:03 · 3221 阅读 · 0 评论 -
functools.wraps:包装器的装饰器
使用装饰器对函数进行装饰后,返回包装器代替原来的函数,函数属性都会被替代!这时需要使用functools.wraps来对包装器进行装饰,保持原函数属性。例子见:(已经在python3.6上测过)https://www.cnblogs.com/fcyworld/p/6239951.html...原创 2019-06-28 14:53:43 · 108 阅读 · 0 评论 -
python迭代器和生成器
可迭代对象:实现了__iter__方法的对象,如列表,元组,字典,通过iter()来生成迭代器迭代器:实现了__next__方法的可迭代对象,通过next()来访问生成器:使用了yield的函数,返回迭代器迭代器例子:class Fib: def __init__(self): self._a = 0 self._b = 1 de...原创 2019-07-09 14:14:46 · 106 阅读 · 0 评论 -
ImportError: numpy.core.multiarray failed to import
我是在pip install pyradiomics时出现这个问题,只用卸载numpypip uninstall numpy再重装pip install numpy原创 2019-07-03 14:38:35 · 219 阅读 · 0 评论 -
collections.ChainMap对象
例子:from collections import ChainMapcombined = ChainMap({"a": "A"}, {"b": "B"}, {"c": "C"}, {"d": "D"})# 访问元素element_a = combined["a"]# 增删,修改只对第一个dict有用# 增加元素combined["big_a"] ="big_A" ...原创 2019-06-24 19:09:12 · 385 阅读 · 0 评论 -
使用multiprocessing.Pool实现并发执行
整理自博友文章https://www.cnblogs.com/kaituorensheng/p/4445418.html,以下代码都在python3.6上测试(win10)类:multiprocessing.Pool(processes)非阻塞例子:#coding: utf-8import multiprocessingimport timedef func(msg): ...原创 2019-07-02 16:02:41 · 2309 阅读 · 0 评论 -
在python中使用subprocess调用子进程
本博文整理自博友文章https://blog.csdn.net/qq_33932782/article/details/79929782。例子都在ubuntu 18.04上使用python3.6进行测试。版本上的说明:python2.4之后有subprocess包,python3.5之后官方建议使用subprocess.run()1. 不捕捉输出(这种可以用在只执行操作,不需要结果的情...原创 2019-07-01 16:43:37 · 428 阅读 · 0 评论 -
argparse基本用法
在编写python程序中,可以使用argparse解析命令行参数来传递参数,进而根据需要控制程序的运行方式。例子:import argparse# create an ArgumentParser objectparser = argparse.ArgumentParser(description="test a parser")# add argumentsparser.a...原创 2019-06-26 10:36:01 · 197 阅读 · 0 评论 -
python2.7下安装PyQt4
问题:在pycharm安装PyQt4或者直接用pip2 install PyQt4失败解决:把.whl文件下载下来使用pip2安装1. 在https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4下载PyQt4‑4.11.4‑cp27‑cp27m‑win32.whl(这里尤其要注意不要下载64位的PyQt4‑4.11.4‑cp27‑cp27m‑win...原创 2019-07-23 19:29:05 · 3486 阅读 · 0 评论 -
代码中终止python程序
参考:https://jingyan.baidu.com/article/6181c3e0cc53a5152ef1532d.html试验过的是sys.exit(0)原创 2019-07-17 08:37:51 · 11956 阅读 · 1 评论 -
shell中使用python运行文件需要文件绝对路径
即使在PATH中加入了python文件所在的文件夹,在shell中使用python运行文件仍然需要文件绝对路径,一个替代是将文件变为可执行文件,但是我并没有验证原创 2019-08-30 10:15:13 · 1257 阅读 · 0 评论 -
zip和zip(*)
lis = list(range(3))square_lis = [i**2 for i in lis]comb = zip(lis, square_lis)a = list(comb)print(a)x = list(zip(*a))print(x)结果:[(0, 0), (1, 1), (2, 4)][(0, 1, 2), (0, 1, 4)]原创 2019-08-23 15:26:22 · 586 阅读 · 0 评论 -
获取py文件的路径
例子:import ospath = r'D:\view1_frontal.jpg'print(os.path.dirname(path))print(os.path.abspath(path))print(os.path.split(path))结果:D:\D:\view1_frontal.jpg('D:\\', 'view1_frontal.jpg')实...原创 2019-08-21 11:39:21 · 907 阅读 · 0 评论 -
Python中整数的~(按位取反)运算
原理:1. 在计算机中整数用补码表示,非负数的补码是自身的二进制表示,负数的补码是相反数的二进制表示按位取反然后加12. python中整数的按位取反运算是先对整数的补码进行按位取反然后再回到整数例子:~(-6)=5步骤:(1)先求-6的补码表示:-6 ——> 6(相反数) ——> 0000 0110(二进制表示)——> 1111 1001(按位取反) ——&g...原创 2019-08-12 10:53:37 · 3011 阅读 · 1 评论