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)

6. python统计字符串中出现次数前n个的值 (python find the n most frequent words from string)

7. python 字母转换成数字 (python covert alphabet letters to number)

 

内容:

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/

 1 import numpy as np
 2 import cv2
 3  
 4 img = np.zeros((512, 512, 3), dtype = "uint8")
 5  
 6 penta = np.array([[[40,160],[120,100],[200,160],[160,240],[80,240]]], np.int32)
 7 triangle = np.array([[[240, 130], [380, 230], [190, 280]]], np.int32)
 8 cv2.polylines(img, [triangle], True, (0,255,0), thickness=3)
 9  
10 img_mod = cv2.polylines(img, [penta], True, (255,120,255),3)
11  
12 cv2.imshow('Shapes', img_mod)
13   
14 cv2.waitKey(0)
15 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统计字符串中出现次数前n个的值 (python find the n most frequent words from string) 

参考资料: https://www.geeksforgeeks.org/find-k-frequent-words-data-set-python/

 1 In [2]: from collections import Counter  
 2    ...:    
 3    ...: data_set = "Welcome to the world of Geeks. This portal has been created to provide well written well \ 
 4    ...:     thought and well explained solutions for selected questions \ 
 5    ...:     If you like Geeks for Geeks and would like to contribute \ 
 6    ...:     here is your chance You can write article and mail your article \ 
 7    ...:      to contribute at geeksforgeeks org See your article appearing on \ 
 8    ...:     the Geeks for Geeks main page and help thousands of other Geeks. "  
 9    ...:    
10    ...: # split() returns list of all the words in the string  
11    ...: split_it = data_set.split()  
12    ...:    
13    ...: # Pass the split_it list to instance of Counter class.  
14    ...: Counter = Counter(split_it)  
15    ...:    
16    ...: # most_common() produces k frequently encountered  
17    ...: # input values and their respective counts.  
18    ...: most_occur = Counter.most_common(4)  
19    ...:    
20    ...: print(most_occur) 

 结果返回: [('to', 4), ('and', 4), ('Geeks', 4), ('well', 3)]

 

7. python 字母转换成数字 (python covert alphabet letters to number)

参考资料: https://stackoverflow.com/questions/4528982/convert-alphabet-letters-to-number-in-python

1 In [11]: ord("A".lower())-96                                                    
2 Out[11]: 1
3 
4 In [12]: ord("d".lower())-96                                                    
5 Out[12]: 4
6 
7 In [13]: ord("W".lower())-96                                                    
8 Out[13]: 23

 

posted @ 2019-11-21 14:20  ttweixiao9999  阅读(271)  评论(0编辑  收藏  举报