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的更多相关文章

  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. android:layout_gravity 和 android:gravity 的区别?

    第一个是让该布局在其父控件中的布局方式,第二个是该布局布置其字对象的布局方式

  2. springboot之RocketMq实现

    环境:win10 1.下载安装包 http://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/4.4.0/rocketmq-all-4.4.0-bin-re ...

  3. CompletableFuture用法介绍

    一.CompletableFuture用法入门介绍 入门介绍的一个例子: package com.cy.java8; import java.util.Random; import java.util ...

  4. 常用 tcpdump 抓包方式

    目录 文章目录 目录 tcpdump 指令 关键字 常用指令选项 常规操作示例 过滤主机 过滤端口 过滤网络(网段) 过滤协议 复杂的逻辑表达式过滤条件 参考资料 tcpdump 指令 tcpdump ...

  5. 阶段3 2.Spring_05.基于XML的IOC的案例1_4 注解IOC案例-把自己编写的类使用注解配置

    注解改造案例 复制之前的xml配置的pom.xml里面的依赖. 复制com文件 bean.xml配置文件也拷贝过来 测试类也复制过来 开始基于注解的IOC配置 右键项目,选择maven.选择更新 更新 ...

  6. asp.net mvc model attribute and razor and form and jquery validate 完美结合

    1.创建Model,添加标注. [Serializable] public class BaseUserModel:BaseModel { [StringLength(100)] [Required( ...

  7. 纯CSS做3D旋转魔方

    昨天偶然看见网友(简单说 用CSS做一个魔方旋转的效果)做的一个3D旋转魔方  效果就是本博客右侧公告栏所示 在这里把做法展现出来 感兴趣的可以试试  做成自己特有的魔方 <!DOCTYPE h ...

  8. UniEAP Platform V5.0 Unable to compile class for JSP

    流程设计器报错: http://127.0.0.1:8080/framework/workflow/webdesign/procmodify/procmodifydetail.jsp?isLoadDa ...

  9. LeetCode.941-有效山形数组(Valid Mountain Array)

    这是悦乐书的第360次更新,第387篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第222题(顺位题号是941).给定一个整数数组A,当且仅当它是一个有效的山形数组时返回 ...

  10. 【VS开发】【图像处理】RGB Bayer Color分析

    RGB Bayer Color分析 Bayer色彩滤波阵列 拜耳色彩滤波阵列(Bayer Color Filter Array,CFA)是非常有名的彩色图片的数字采集格式.色彩滤波器的模式如上图所示, ...