python常用技巧 — 杂
目录:
1. 找到字符串中的所有数字(python find digits in string)
2. python 生成连续的浮点数(如 0.1, 0.2, 0.3, 0.4, ... , 0.9)(python range() for floats)
3. 判断两个矩形重叠程度 (python calculate overlap of two rectangle)
4. python 画多边形 (python draw polygon)
5. python 条件语句用一行实现 (python condition statement on one row)
内容:
1. 找到字符串中的所有数字(python find digits in string)
方法1:
https://stackoverflow.com/questions/12005558/python-find-digits-in-a-string
name = 'body_flaw_validate_set20191119170917_'
list(filter(str.isdigit, name))
['2', '0', '1', '9', '1', '1', '1', '9', '1', '7', '0', '9', '1', '7']
方法2:
https://www.geeksforgeeks.org/python-extract-digits-from-given-string/
import re
name = 'body_flaw_validate_set20191119170917_'
re.sub("\D", "", name)
'20191119170917'
2. python 生成连续的浮点数(如 0.1, 0.2, 0.3, 0.4, ... , 0.9)python range() for floats
https://stackoverflow.com/questions/7267226/range-for-floats
方法1:
[x / 10.0 for x in range(1, 10)]
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
方法2:
import pylab as pl
pl.frange(0.1,0.9,0.1)
array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
方法3:
import numpy
numpy.linspace(0.1, 0.9, num=9)
array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
3. 判断两个矩形重叠程度 (python calculate overlap of two rectangle)
https://stackoverflow.com/questions/27152904/calculate-overlapped-area-between-two-rectangles
更加详细的例子:
https://shapely.readthedocs.io/en/stable/manual.html
from shapely.geometry import Polygon
polygon = Polygon([(3, 3), (5, 3), (5, 5), (3, 5)])
other_polygon = Polygon([(1, 1), (4, 1), (4, 3.5), (1, 3.5)])
intersection = polygon.intersection(other_polygon)
print(intersection.area)
# 0.5
4. python 画多边形 (python draw polygon)
https://www.life2coding.com/draw-polygon-on-image-using-python-opencv/
import numpy as np
import cv2 img = np.zeros((512, 512, 3), dtype = "uint8") penta = np.array([[[40,160],[120,100],[200,160],[160,240],[80,240]]], np.int32)
triangle = np.array([[[240, 130], [380, 230], [190, 280]]], np.int32)
cv2.polylines(img, [triangle], True, (0,255,0), thickness=3) img_mod = cv2.polylines(img, [penta], True, (255,120,255),3) cv2.imshow('Shapes', img_mod) cv2.waitKey(0)
cv2.destroyAllWindows()

5. python 条件语句用一行实现 (python condition statement on one row)
https://stackoverflow.com/questions/2802726/putting-a-simple-if-then-else-statement-on-one-line
// 格式
value_when_true if condition else value_when_false
// 例子
'Yes' if fruit == 'Apple' else 'No'
6.
python常用技巧 — 杂的更多相关文章
- python常用技巧
1,关于tab键与4个空格: 由于不同平台间,tab键值设置有所区别,据相关介绍,官方在缩进方面推荐使用4个空格.方便起见,可设置tab自动转换为4个空格. 1.1在pycharm中: 通过fi ...
- python 常用技巧
一.字符串与数值的转换 Python中字符串转换为数值: str_num = '99' num = int(str_num) 整型数转换为字符串: num = 99 str_num = str(num ...
- python 常用技巧 — 字典 (dictionary)
目录: 1. python 相加字典所有的键值 (python sum all values in dictionary) 2. python 两个列表分别组成字典的键和值 (python two l ...
- python 常用技巧 — 列表(list)
目录: 1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list) 2. 多个列表对应位置相加(add the correspond ...
- python 常用技巧 — 数组 (array)
目录: 1. 数组每一行除以这一行的总数(numpy divide row by row sum) 2. 数组每一行或者每一列求平均 (python average array columns or ...
- #1 Python灵活技巧
前言 Python基础系列博文已顺利结束,从这一篇开始将进入探索更加高级的Python用法,Python进阶系列文章将包含面向对象.网络编程.GUI编程.线程和进程.连接数据库等.不过在进阶之前,先来 ...
- Python SQLAlchemy基本操作和常用技巧包含大量实例,非常好python
http://www.makaidong.com/%E8%84%9A%E6%9C%AC%E4%B9%8B%E5%AE%B6/28053.shtml "Python SQLAlchemy基本操 ...
- python算法常用技巧与内置库
python算法常用技巧与内置库 近些年随着python的越来越火,python也渐渐成为了很多程序员的喜爱.许多程序员已经开始使用python作为第一语言来刷题. 最近我在用python刷题的时候想 ...
- [转]python 常用类库!
Python学习 On this page... (hide) 1. 基本安装 2. Python文档 2.1 推荐资源站点 2.2 其他参考资料 2.3 代码示例 3. 常用工具 3.1 Pytho ...
随机推荐
- JavaWeb(一):Java技术概览
一.Java技术体系 在早期,Java被称为Java开发工具包或JDK,是一门与平台(由一组 必需的API组成)紧密耦合的语言. 从1998年底的1.2版本开始,Java技术栈被分割为下面关键部分: ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- controllerpom
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...
- FMXUI ANDROID下连续按多次返回出现异常
在ANDROID下,按返回键后,默认是关闭当前Frame,但连接按返回键,会对当前Frame执行多次关闭动作,因为已经释放过对象,再次关闭会出现异常错误,解决办法:定义一个标识如FClosed: ...
- Shiro安全框架的说明及配置入门
Shiro是什么? Shiro是一个非常强大的,易于使用的,开源的,权限框架.它包括了权限校验,权限授予,会话管理,安全加密等组件 什么时候使用它呢? 如果你是设计RBAC基础系统,需要编写大量用于权 ...
- Pangu and Stones HihoCoder - 1636 区间DP
Pangu and Stones HihoCoder - 1636 题意 给你\(n\)堆石子,每次只能合成\(x\)堆石子\((x\in[L, R])\),问把所有石子合成一堆的最小花费. 思路 和 ...
- BSGS算法(模板)
BSGS (大步小步算法) 已知\(a.b. c\),求\(x\).令\(a^x \equiv b \pmod c\). 步骤 \[m = \lceil \sqrtc\ \rceil \]\[x = ...
- 如何为网站启用HTTPS加密传输协议
前言 当今时代对上网的安全性要求比以前更高,chrome和firefox也都大力支持网站使用HTTPS,苹果也从2017年开始在iOS 10系统中强制app使用HTTPS来传输数据,微信小程序也是要求 ...
- DPTR是什么寄存器 它的作用是什么 它由哪几个寄存器组成
数据指针(DPTR)是80C51中一个功能比较特殊的寄存器.从结构DPTR是一个16位的特殊功能寄存器, 其高位字节寄存器用DPH表示,低位字节寄存器用DPL表示,DPTR既可以作为一个16位的寄存器 ...
- error C2065: ‘__in’ : undeclared identifier
转自VC错误:http://www.vcerror.com/?p=1307 问题描述: 编译时出现: error C2065: '__in' : undeclared identifier error ...