1、Find memory used by an object

  1. import sys
  2. print(sys.getsizeof(5)) #
  3. print(sys.getsizeof("Python")) #

2、Combine a list of strings into a single string

  1. strings = ['', 'python', 'snippets']
  2. print(','.join(strings)) # 50,python,snippets

3、Find elements that exist in either of the two lists

  1. def union(a,b):
  2. return list(set(a + b))
  3.  
  4. 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

  1. from collections import Counter
  2. list = [1, 2, 3, 2, 4, 3, 2, 3]
  3. count = Counter(list)
  4. print(count) # {2: 3, 3: 3, 1: 1, 4: 1}

5、Find the most frequent element in a list

  1. def most_frequent(list):
    # 原文取了set,不知道为什么也可以?
  2. return max(list, key = list.count)numbers = [1, 2, 3, 2, 4, 3, 1, 3]
  3. most_frequent(numbers) #

6、Use map functions

  1. def multiply(n):
  2. return n * n
  3.  
  4. list = (1, 2, 3)
  5. result = map(multiply, list)
  6. print(list(result)) # {1, 4, 9}

7、Use filter functions

  1. arr = [1, 2, 3, 4, 5]
  2. arr = list(filter(lambda x : x%2 == 0, arr))
  3. 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的数字排序,比该数字大的放后面,反之放前面。

  1. array = np.array([10, 7, 4, 3, 2, 2, 5, 9, 0, 4, 6, 0])index = np.argpartition(array, -5)[-5:]
  2. index
  3. array([ 6, 1, 10, 7, 0], dtype=int64)
    np.sort(array[index])
  4. array([ 5, 6, 7, 9, 10])

9、Clip : How to keep values in an array within an interval

  1. #Example-1
  2. array = np.array([10, 7, 4, 3, 2, 2, 5, 9, 0, 4, 6, 0])
  3. print (np.clip(array,2,6))[6 6 4 3 2 2 5 6 2 4 6 2]#Example-2
  4. array = np.array([10, -1, 4, -3, 2, 2, 5, 9, 0, 4, 6, 0])
  5. 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

  1. arr = np.arange(10)
  2. arrarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])# Define the codition, here we take MOD 3 if zero
  3. condition = np.mod(arr, 3)==0
  4. conditionarray([ True, False, False, True, False, False, True, False, False,True])np.extract(condition, arr)
  5. array([0, 3, 6, 9])

11、setdiff1d : How to find unique values in an array compared to another

  1. a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
  2. b = np.array([3,4,7,6,7,8,11,12,14])
  3. c = np.setdiff1d(a,b)
  4. carray([1, 2, 5, 9])

12、convert a list-like string to a list of lists

  1. 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
  2. 13appendconcatenate, vstack
    list: append
    array concatenate
  3. 14for filtermap, reduce
    https://mp.weixin.qq.com/s/sdw-pp6ESbsevr_25dGh_Q

python snippets的更多相关文章

  1. 15种Python片段去优化你的数据科学管道

    来源:15 Python Snippets to Optimize your Data Science Pipeline 翻译:RankFan 15种Python片段去优化你的数据科学管道 为什么片段 ...

  2. python开发环境配置(Windows)

    简介 由于在搭建pyhon开发环境时会出现各种各样的问题,因此将这些问题记录下来 1.下载python 从官网下载对应系统的python版本(最新稳定版即可):官网地址为:python下载地址, 建议 ...

  3. Ubuntu1404: 将VIM打造为一个实用的PythonIDE

    参考:  http://www.tuicool.com/articles/ZRv6Rv 说明: 内容非原创, 主要是做了整合和梳理. 在 ubuntu14.04 & debian 8 下测试通 ...

  4. [OpenCV] Install OpenCV 3.4 with DNN

    目标定位 一.开始全面支持 Tensorflow OpenCV3.4 新功能 当前最新进展OpenCV 3.4 dev:https://github.com/opencv/opencv/tree/ma ...

  5. Windows下编译YouCompleteMe流程

    废话 生命在于折腾. 之前不用这个插件的原因: 因为要使这个插件起作用,前前后后需要下载几百MB(win下更是超过了1GB)的东西,包括了Clang编译器,ycmd的c艹源码还有ycm本身的vim s ...

  6. vim 设置

    TL;DR: $ git clone https://github.com/sontek/dotfiles.git $ cd dotfiles $ ./install.sh vim Download  ...

  7. vim中自动补全插件snipmate使用

    vim中自动补全插件snipmate使用 1.下载snipMatezip:https://github.com/msanders/snipmate.vim/archive/master.zip 2.解 ...

  8. Python日常实践(1)——SQL Prompt的Snippets批量整理

    引言 个人平时在写sql脚本的时候会使用到SQL Prompt这款插件,除了强大的智能提示和格式化sql语句功能,我还喜欢使用Snippets代码段功能.比如我们可以在查下分析器输入ssf后按Tab键 ...

  9. 关于Python有用的snippets

    1.将字典的key,value反转换位置 值value可以取任何数据类型,但键key必须是不可变的,如字符串,数字或元组. dict1={'Lisa':1,'Bob':2,'Mick':3} dict ...

随机推荐

  1. Jmeter+Ant+Jenkins接口自动化持续集成环境搭建(Linux)

    一.安装说明 系统环境:CentOS release 6.4 JDK版本:jdk1.8.0_181 Jmeter版本:apache-jmeter-3.0 Ant版本:apache-ant-1.9.13 ...

  2. Mysql数据库事务的四大特性:

    什么是事务? 事务Transaction,是指作为一个基本工作单元执行的一系列SQL语句的操作,要么完全地执行,要么完全地都不执行.为什么要使用事务:保证对数据操作的完整性和准确性.1,原子性:一个事 ...

  3. python__005

    一.字符串格式化 #字符串的拼接#msg='i am a best boy'+'非常帅'print(msg)name=input('name:')hobby=input(('hobby:'))age= ...

  4. Egret入门学习日记 --- 第二篇 (书籍的选择 && 书籍目录 && 书中 3.3 节 内容)

    第二篇 (书籍的选择 && 书籍目录 && 书中 3.3 节 内容) 既然选好了Egret,那我就要想想怎么学了. 开始第一步,先加个Q群先,这不,拿到了一本<E ...

  5. HCL试验2

    PC端配置:配置ip地址 交换机1配置:①创建VLAN system-view vlan 10 vlan 20 ②配置PC端接口 interface gi 1/0/1 port link-type a ...

  6. Jackson快速入门

    1.Jackson的简单用法 2.Jackson框架的高阶应用 3.Jackson不支持Java8 Date解决方法 https://www.cnblogs.com/mkxzy/p/7091381.h ...

  7. 思考--mysql 分库分表的思考

    查询不在分库键上怎么办,扫描所有库?由于分库了,每个库扫描很快?所以比单个表的扫描肯定快,可以这样理解吗. 多表jion怎么弄,把内层表发给每个分库吗? citus,tidb 都有这些问题,citus ...

  8. CodeForces-520E Pluses everywhere

    题目描述 给出一个长度为 \(n\) 的字符串,给出一个非负整数 \(k\),要求给这个字符串中间添加 \(k\) 个$\(+\)'号,变成一个表 达式,比如"\(1000101\)&quo ...

  9. gcc数据对齐之: howto 1.

    GCC支持用__attribute__为变量.类型.函数.标签指定特殊属性.这些不是编程语言标准里的内容,而属于编译器对语言的扩展. 本文介绍其中的两个属性:aligned和packed. align ...

  10. 集成学习-组合策略与Stacking

    集成学习是如何把多个分类器组合在一起的,不同的集成学习有不同的组合策略,本文做个总结. 平均法 对数值型输出,平均法是最常用的策略,解决回归问题. 简单平均法 [h(x)表示基学习器的输出] 加权平均 ...