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. Searching for MobileNetV3

    1. 摘要 基于网络架构搜索和 NetAdapt 算法,作者提出了新一代的 MobileNets,并通过一些先进的结构对其进行了改进. 作者发布了两个模型 MobileNetV3-Large 和 Mo ...

  2. 如何将打印内容转换为bmp位图文件

    bmp是一种与硬件设备无关的图像文件格式,使用非常广.它采用位映射存储格式,除了图像深度可选以外,不采用其他任何压缩,因此,BblP文件所占用的空间很大.BMP文件的图像深度可选lbit.4bit.8 ...

  3. 在jsp中出现:Syntax error, insert "Finally" to complete TryStatement错误

    在jsp中出现:Syntax error, insert "Finally" to complete TryStatement错误 可能括号不匹配{}

  4. SVN 清理失败解决方案

    SVN有时因各种不明原因导致清理失败,可以采取如下解决办法进行处理: 方法一: 删除根目录下隐藏文件夹“.svn” 然后在根目录文件夹 外面的空白处 检出.比如你项目文件夹名为“D:/source” ...

  5. 谈谈 Android 的优点和不足之处?

    优点:1.开放性,开源,免费,可定制2.挣脱运营商束缚3.丰富的硬件选择4.不受任何限制的开发商5.无缝结合的 Google 应用缺点:1.安全问题.隐私问题2.同质化严重3.运营商对 Android ...

  6. centos7 禁止root远程ssh直接登录

    修改/etc/ssh/sshd_config文件,将 #PermitRootLogin yes 修改为 PermitRootLogin no   查看 more /etc/ssh/sshd_confi ...

  7. "fatal error LNK1169: 找到一个或多个多重定义的符号" 解决方案

    本人在测试刚刚安装的vs2017时运行出了问题, 错误信息为 "fatal error LNK1169: 找到一个或多个多重定义的符号", 代码如下: //Myfile.h #in ...

  8. css3实现倾斜转动的转盘

    HTML代码: <div class="r-1">a</div> <div class="r-2">a</div> ...

  9. win10 hhctrl.ocx 丢失

    1.我的是从同事电脑上复制过来的,他电脑也是win102.复制文件“hhctrl.ocx”到系统目录下  32位系统目录为:C:\WINNT\System32:64位系统为C:\Windows\Sys ...

  10. mybatis多对多级联查询

    1.实体 package com.govmade.govdata.modules.sys.pojo; import java.util.List; import javax.persistence.T ...