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. 如何在form组件中添加一个单选或者多选的字段

    解决办法: 需要在增加的类里面加入choices   具体操作如下:

  2. 方差分析(python代码实现)

    python机器学习-乳腺癌细胞挖掘(欢迎关注博主主页,学习python视频资源,还有大量免费python经典文章) https://study.163.com/course/introduction ...

  3. 统计学_Wilcoxon signed-rank test(python脚本)

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  4. while循环嵌套

    <1>while嵌套的格式 while 条件1: 条件1满足时,做的事情1 条件1满足时,做的事情2 条件1满足时,做的事情3 ...(省略)... while 条件2: 条件2满足时,做 ...

  5. linux如何杀掉进程(kill)

    方法/步骤1: 使用“ps -e|grep mysql”命令,查看mysql程序的对应的pid号.结果如下图:   方法/步骤2: 使用“kill -9 2891”命令,可以结束掉mysqld_saf ...

  6. os x 技巧: 关闭打字时候光标闪烁

    关闭光标闪烁: defaults write -g NSTextInsertionPointBlinkPeriodOff -float 0 defaults write -g NSTextInsert ...

  7. Python+requests+excel接口测试

    2018-06-14   17:00:13 环境准备: - Python 3.7 - requests库 - xlrd 1.创建Excel文件 2.读取Excel文件 import xlrd clas ...

  8. vtkTestHull将多个平面围成一个凸面体

    1.vtkHull produce an n-sided convex hull vtkHull is a filter which will produce an n-sided convex hu ...

  9. LeetCode.1037-有效的回旋镖(Valid Boomerang)

    这是小川的第387次更新,第416篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第248题(顺位题号是1037).回旋镖是一组各不相同且不在一条直线上的三个点.给出三个点 ...

  10. upd通讯Recvfrom设置阻塞不起作用

    把自己踩到的坑记录一下,在做UDP通讯的时候,发现自己的程序没有收数据居然也有百分之十二的cpu占用率,通过性能分析工具了解到时recvfrom函数一直在执行,虽然设置阻塞并且确认成功了, ;//阻塞 ...