Python 使用小技巧 记录

  • Remove empty string from a list
word_list = filter(bool, word_list)
  • 找打最大 或者最小的 非零数字 in a numpy array (or a tuple) in python
import numpy as np
minval = np.min(a[np.nonzero(a)])
maxval = np.max(a[np.nonzero(a)])
  • string 里面的东西 转到 这个东西的格式 本身
import ast
ast.literal_eval("(255, 0, 0)")
(255, 0, 0)
  • 在 Mac本地装 numpy
pip install --upgrade --ignore-installed --install-option '--install-data=/usr/local' numpy

  • OS local python need numpy
    java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): 1.需要使用本地python ,

    #!/usr/bin/python
    #!/usr/bin/env python 2.Numpy !!
    
  • 运行 jupyter

runipy MyNotebook.ipynb
  • add value to same key in dict
dict.setdefault( “a” , [ ] ) .append ( b )
  • list[:,int] get column ?

Shit u need to convert to np.array

  • server use Jupiter
jupyter notebook --no-browser --port=8889
ssh -N -f -L localhost:8888:localhost:8889 asd@xxxx
  • product(“abcd”,reprat = 2)
“Ab ” ac ad ba ……. 
  • 所有標點符號 标点符号 用re 替换
     re.sub(r'[^\w]', ' ', s)
  • make a vocabulary using my own article’s words
		from sklearn.feature_extraction.text import CountVectorizer
		vectorizer = CountVectorizer()
		vectorizer.fit(x)
		vectorizer.transform(x)
		vectorizer.transform(y)
		mydict = vectorizer.vocabulary_
  • strip 去掉字串结尾的字元
  • split 顾名思义,切割
In [5]: "123,321, 213,,,,".strip(',')
Out[5]: '123,321, 213'

In [6]: "123,321,213".split(',')
Out[6]: ['123', '321', '213']

numpy

内积
np.inner([1,2,3],[2,2,2])
> 12 
随机打散
index = np.random.permutation(10)
> array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])

new_array = array[index]
Unique, set 之后 再 sort
>>> a=np.random.randint(0,5,8) 
>>> a 
array([2, 3, 3, 0, 1, 4, 2, 4]) 

>>> np.unique(a) 
array([0, 1, 2, 3, 4]) 


>>> c,s=np.unique(b,return_index=True) 
>>> c 
array([0, 1, 2, 3, 4]) 
>>> s 
array([3, 4, 0, 1, 5])(元素出现的起始位置)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值