目录:

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常用技巧 — 杂的更多相关文章

  1. python常用技巧

    1,关于tab键与4个空格: 由于不同平台间,tab键值设置有所区别,据相关介绍,官方在缩进方面推荐使用4个空格.方便起见,可设置tab自动转换为4个空格. 1.1在pycharm中:    通过fi ...

  2. python 常用技巧

    一.字符串与数值的转换 Python中字符串转换为数值: str_num = '99' num = int(str_num) 整型数转换为字符串: num = 99 str_num = str(num ...

  3. python 常用技巧 — 字典 (dictionary)

    目录: 1. python 相加字典所有的键值 (python sum all values in dictionary) 2. python 两个列表分别组成字典的键和值 (python two l ...

  4. python 常用技巧 — 列表(list)

    目录: 1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list) 2. 多个列表对应位置相加(add the correspond ...

  5. python 常用技巧 — 数组 (array)

    目录: 1. 数组每一行除以这一行的总数(numpy divide row by row sum) 2. 数组每一行或者每一列求平均 (python average array columns or ...

  6. #1 Python灵活技巧

    前言 Python基础系列博文已顺利结束,从这一篇开始将进入探索更加高级的Python用法,Python进阶系列文章将包含面向对象.网络编程.GUI编程.线程和进程.连接数据库等.不过在进阶之前,先来 ...

  7. Python SQLAlchemy基本操作和常用技巧包含大量实例,非常好python

    http://www.makaidong.com/%E8%84%9A%E6%9C%AC%E4%B9%8B%E5%AE%B6/28053.shtml "Python SQLAlchemy基本操 ...

  8. python算法常用技巧与内置库

    python算法常用技巧与内置库 近些年随着python的越来越火,python也渐渐成为了很多程序员的喜爱.许多程序员已经开始使用python作为第一语言来刷题. 最近我在用python刷题的时候想 ...

  9. [转]python 常用类库!

    Python学习 On this page... (hide) 1. 基本安装 2. Python文档 2.1 推荐资源站点 2.2 其他参考资料 2.3 代码示例 3. 常用工具 3.1 Pytho ...

随机推荐

  1. AGC016题解

    呼我竟然真的去刷了016QwQ[本来以为就是个flag的233] 感觉AGC题目写起来都不是很麻烦但是确实动脑子qvq[比较适合训练我这种没脑子选手] 先扔个传送门:点我 A.Shrinking 题意 ...

  2. python关于window文件写入后,换行默认\r\n的问题

    因为python兼容各种平台,所以当在window打开文本文件写入后,换行会默认写成\r\n linux是\n 如果想去掉换行的\r 解决方法:在open函数里写入换行要求即可 with open(f ...

  3. Ubuntu中可以卸载的软件(持续更新)

    sudo apt-get -y --auto-remove purge unity unity-2d* sudo apt-get -y purge empathy sudo apt-get -y pu ...

  4. 【RabbitMQ】使用RabbitMQ实现延迟任务

    场景一:物联网系统经常会遇到向终端下发命令,如果命令一段时间没有应答,就需要设置成超时. 场景二:订单下单之后30分钟后,如果用户没有付钱,则系统自动取消订单. 上述类似的需求是我们经常会遇见的问题. ...

  5. ARGB色彩模式

    看到#ff ff ff 00 00这种 就是啦 .开头两位表示透明度.

  6. PWA 应用

    1. 使用例子,vue官网,在手机浏览器器打开时,保存在桌面那个应用.还有饿了么网站也是PWA应用.

  7. CSS-使整个页面上的全部元素可编辑

    # [在线预览](https://jsfiddle.net/1010543618/6zu1gush/) ## 方法一 - 使用 html 的 contenteditable 属性: [HTML 5 全 ...

  8. jQuery设置checkbox 为选中状态

    1设置第一个checkbox 为选中值$('input:checkbox:first').attr("checked",'checked');或者$('input:checkbox ...

  9. spring, spring mvc, mybatis整合文件配置详解

    转自:http://www.cnblogs.com/wxisme/p/4924561.html 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用 ...

  10. <每日一题>题目4:for循环套生成器的面试题

    题目: def add(n,i): return n+i def test(): for i in range(4): yield i g = test() for n in [1,10,5]: g ...