目录:

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. boost circularBuffer

    1. circular buffer has two fundamental properties: (1): The capacity of the circular buffer is const ...

  2. SQL索引优化方法

    SQL索引优化方法 以下是代码片段: ROW_NUMBER() OVER(ORDER BY ResumeCreateTime DESC) as [RowID] ,[TopDegree] ,[Degre ...

  3. 4412 Linux定时器

    一.Linux定时器基础知识 1.1 定时器的使用范围 延后执行某个操作,定时查询某个状态:前提是对时间要求不高的地方 1.2 内核时间概念 Hz:(系统时钟通过CONFIG_HZ来设置,范围是100 ...

  4. Kattis - gcpc (treap模板)

    ne hundred years from now, in 21172117, the International Collegiate Programming Contest (of which t ...

  5. CSD编码----数字信号处理--006

    有符号数(Signed Digit Number , SD) SD编码 1.有三重值 {0,1,-1} 2.应用在不用进位的加法器或乘法器中能够降低复杂性 因为通常可以通过非零元素的数来估计乘法的工作 ...

  6. python练习题自己实现一个字符串的find函数

    # 第五题:自己实现一个字符串的find函数 # 1.在一个字符串中查找另一个字符串 # 2.找到了返回第一次出现的位置 # 3.没找到返回-1 # 4.参数s1为源字符串,参数s2为要查找的字符串 ...

  7. python中的list和generator

    # -*- coding:utf-8 -*- # author : Keekuun # 列表生成式,list l1 = [i for i in range(10)] # 或者 l2 = list(ra ...

  8. linux设备驱动第三篇:写一个简单的字符设备驱动

          在linux设备驱动第一篇:设备驱动程序简介中简单介绍了字符驱动,本篇简单介绍如何写一个简单的字符设备驱动.本篇借鉴LDD中的源码,实现一个与硬件设备无关的字符设备驱动,仅仅操作从内核中分 ...

  9. Apache: No space left on device: Couldn’t create rewrite_map(XXXX)

    启动apache的时候 有时候会遇到这样的错误:No space left on device: Couldn’t create rewrite_map(XXXX) 第一眼看以为是磁盘没有空间了,其实 ...

  10. 信息安全-攻击-XSS:XSS/CSS 攻击

    ylbtech-信息安全-攻击-XSS:XSS/CSS 攻击 XSS攻击通常指的是通过利用网页开发时留下的漏洞,通过巧妙的方法注入恶意指令代码到网页,使用户加载并执行攻击者恶意制造的网页程序.这些恶意 ...