python常用技巧 — 杂

时间:2019-11-21
本文章向大家介绍python常用技巧 — 杂,主要包括python常用技巧 — 杂使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

目录:

1. 找到字符串中的所有数字(python find digits in string)

2. python 生成连续的浮点数(如 0.1, 0.2, 0.3, 0.4, ... , 0.9)(python range() for floats)

 

内容:

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. 

 

原文地址:https://www.cnblogs.com/ttweixiao-IT-program/p/11905379.html