python snippets
1、Find memory used by an object
import sys
print(sys.getsizeof(5)) #
print(sys.getsizeof("Python")) #
2、Combine a list of strings into a single string
strings = ['', 'python', 'snippets']
print(','.join(strings)) # 50,python,snippets
3、Find elements that exist in either of the two lists
def union(a,b):
return list(set(a + b)) union([1, 2, 3, 4, 5], [6, 2, 8, 1, 4]) # [1,2,3,4,5,6,8]
4、Track frequency of elements in a list
from collections import Counter
list = [1, 2, 3, 2, 4, 3, 2, 3]
count = Counter(list)
print(count) # {2: 3, 3: 3, 1: 1, 4: 1}
5、Find the most frequent element in a list
def most_frequent(list):
# 原文取了set,不知道为什么也可以?
return max(list, key = list.count)numbers = [1, 2, 3, 2, 4, 3, 1, 3]
most_frequent(numbers) #
6、Use map functions
def multiply(n):
return n * n list = (1, 2, 3)
result = map(multiply, list)
print(list(result)) # {1, 4, 9}
7、Use filter functions
arr = [1, 2, 3, 4, 5]
arr = list(filter(lambda x : x%2 == 0, arr))
print (arr) # [2, 4]
参考 https://medium.com/better-programming/25-useful-python-snippets-to-help-in-your-day-to-day-work-d59c636ec1b
8、Argpartition : Find N maximum values in an array
np.argpartition(array, N) 对array中index为N的数字排序,比该数字大的放后面,反之放前面。
array = np.array([10, 7, 4, 3, 2, 2, 5, 9, 0, 4, 6, 0])index = np.argpartition(array, -5)[-5:]
index
array([ 6, 1, 10, 7, 0], dtype=int64)
np.sort(array[index])
array([ 5, 6, 7, 9, 10])
9、Clip : How to keep values in an array within an interval
#Example-1
array = np.array([10, 7, 4, 3, 2, 2, 5, 9, 0, 4, 6, 0])
print (np.clip(array,2,6))[6 6 4 3 2 2 5 6 2 4 6 2]#Example-2
array = np.array([10, -1, 4, -3, 2, 2, 5, 9, 0, 4, 6, 0])
print (np.clip(array,2,5))[5 2 4 2 2 2 5 5 2 4 5 2]
10、Extract: To extract specific elements from an array based on condition
arr = np.arange(10)
arrarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])# Define the codition, here we take MOD 3 if zero
condition = np.mod(arr, 3)==0
conditionarray([ True, False, False, True, False, False, True, False, False,True])np.extract(condition, arr)
array([0, 3, 6, 9])
11、setdiff1d : How to find unique values in an array compared to another
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
b = np.array([3,4,7,6,7,8,11,12,14])
c = np.setdiff1d(a,b)
carray([1, 2, 5, 9])
12、convert a list-like string to a list of lists
executable_output = '[0.011, 0.544, 2.314], [7.895, 6.477, 2.573]'
(1) list comprehension(居中)
(2)literal_eval(最慢)
(3) json(最快)
具体参考https://stackoverflow.com/questions/43419856/python-fastest-and-most-efficent-way-to-convert-a-list-like-string-to-a-list-o?r=SearchResults 13、append,concatenate, vstack
list: append
array concatenate 14、for 与 filter,map, reduce
https://mp.weixin.qq.com/s/sdw-pp6ESbsevr_25dGh_Q
python snippets的更多相关文章
- 15种Python片段去优化你的数据科学管道
来源:15 Python Snippets to Optimize your Data Science Pipeline 翻译:RankFan 15种Python片段去优化你的数据科学管道 为什么片段 ...
- python开发环境配置(Windows)
简介 由于在搭建pyhon开发环境时会出现各种各样的问题,因此将这些问题记录下来 1.下载python 从官网下载对应系统的python版本(最新稳定版即可):官网地址为:python下载地址, 建议 ...
- Ubuntu1404: 将VIM打造为一个实用的PythonIDE
参考: http://www.tuicool.com/articles/ZRv6Rv 说明: 内容非原创, 主要是做了整合和梳理. 在 ubuntu14.04 & debian 8 下测试通 ...
- [OpenCV] Install OpenCV 3.4 with DNN
目标定位 一.开始全面支持 Tensorflow OpenCV3.4 新功能 当前最新进展OpenCV 3.4 dev:https://github.com/opencv/opencv/tree/ma ...
- Windows下编译YouCompleteMe流程
废话 生命在于折腾. 之前不用这个插件的原因: 因为要使这个插件起作用,前前后后需要下载几百MB(win下更是超过了1GB)的东西,包括了Clang编译器,ycmd的c艹源码还有ycm本身的vim s ...
- vim 设置
TL;DR: $ git clone https://github.com/sontek/dotfiles.git $ cd dotfiles $ ./install.sh vim Download ...
- vim中自动补全插件snipmate使用
vim中自动补全插件snipmate使用 1.下载snipMatezip:https://github.com/msanders/snipmate.vim/archive/master.zip 2.解 ...
- Python日常实践(1)——SQL Prompt的Snippets批量整理
引言 个人平时在写sql脚本的时候会使用到SQL Prompt这款插件,除了强大的智能提示和格式化sql语句功能,我还喜欢使用Snippets代码段功能.比如我们可以在查下分析器输入ssf后按Tab键 ...
- 关于Python有用的snippets
1.将字典的key,value反转换位置 值value可以取任何数据类型,但键key必须是不可变的,如字符串,数字或元组. dict1={'Lisa':1,'Bob':2,'Mick':3} dict ...
随机推荐
- Retrofitting Analysis
Retrofitting Analysis To figure out the process of retrofitting[1] objective updating, we do the fol ...
- leetcode 31下一个排列
/** 验证一般情况(元素数目大于等于3)有几个情况分析:两个特殊情况: 6 5 4 3 2 1 完全反序,这种序列没有下一个排序,因此重新排序1 2 3 4 5 6 1 2 3 4 5 6 完全升序 ...
- 浏览器端-W3School-JavaScript:JavaScript RegExp 对象
ylbtech-浏览器端-W3School-JavaScript:JavaScript RegExp 对象 1.返回顶部 1. JavaScript RegExp 对象 RegExp 对象 RegEx ...
- Spring MVC三种返回方式
spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. 下面一一进行说明: 1.ModelAndV ...
- 常用 tcpdump 抓包方式
目录 文章目录 目录 tcpdump 指令 关键字 常用指令选项 常规操作示例 过滤主机 过滤端口 过滤网络(网段) 过滤协议 复杂的逻辑表达式过滤条件 参考资料 tcpdump 指令 tcpdump ...
- Flex TabNavigator
1.获取子项个数 TabNavigator.numChildren(int) 2.对于静态的TabNavigator的如何处理权限显示 for(var i:int=0;i <tab.numChi ...
- Report List 报表开发
1. Report List的输出定义 * ...NO STANDARD PAGE HEADING: 输出的报表不包含表头: * ...LINE-SIZE col : 输出的报表不包含表头: * .. ...
- rhel安装ambari
RHEL/CentOS/Oracle Linux 6 On a server host that has Internet access, use a command line editor to p ...
- java:HTML(table表格,ul列表)和CSS(导入.css文件,三种定义颜色方式,三种样式选择器,a标签属性顺序,)
1.重点掌握: html: 1.form表单:input,checkbox,seelct,radio,button,submit 2.table表格:thead-->tr-->th;tbo ...
- k-交叉验证KFold
交叉验证的原理放在后面,先看函数. 设X是一个9*3的矩阵,即9个样本,3个特征,y是一个9维列向量,即9个标签.现在我要进行3折交叉验证. 执行kFold = KFold(n_splits=3) : ...