python 代码片段备忘

  1. 数组扩展
    predictors=[‘x’]
    if power>=2:
    predictors.extend([‘x_%d’%i for i in range(2,power+1)])

  2. defaultdict(float), defaultdict(int) 做全局初始化

from collections import defaultdict

首先需要明确的一点是,defaultdict(int) 传递进来的类型参数,不是用来约束值的类型,更不是约束键的类型,而是实现一种值的初始化,如果未对该键赋值的话。所以,defaultdict 的真正意义实现一种全局的初始化,访问任何键都不会抛 KeyError 的异常;
(1)defaultdict(int):初始化为 0
(2)defaultdict(float):初始化为 0.0
(3)defaultdict(str):初始化为 ”

valueError, 运行报错
值可能为 nan,正负无穷,数据数据异常是,可能报错跳出。 infinite, -infinite

np.isfinite(df).all()

python 软件依赖清单 requirements.txt,有利于在其它机器上,构建跟本机项目一样的依赖环境
生成 requirements.txt 文件

pip freeze > requirements.txt

安装 requirements.txt 依赖

pip install -r requirements.txt

作者:Wintersee
原文:https://blog.csdn.net/u012386109/article/details/79778153
之前在自己电脑上一直用 pip instal xx 来安装python的包,后来因为公司电脑的网络连接限制,无法通过正常联网的方式安装,所以总结了几种在线 或 离线安装包的方式,具体如下:

在线安装
pip install xx
正常在线安装方式,如果网络不好可以使用国内镜像,方法为: pip install xx -i http://xx

国内几个常用镜像地址:
豆瓣:https://pypi.douban.com/simple
中国科学技术大学:https://mirrors.ustc.edu.cn/pypi/web/simple/
清华大学TUNA:https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/

离线安装:
2.1 如果下载得到的是.tar.gz压缩包,解压,在解压目录的当前文件夹下,打开DOS命令运行:python setup.py install
2.2 .whl 文件安装: 当前目录下运行: pip install ×××.whl

sh 如何将多行变成一行

cat file.txt | xargs

  说明:这可能是最简单的一种方式。
   This manual page documents the GNU version of xargs. xargs reads argu- ments from the standard input, delimited by blanks (which can be pro-tected with double or single quotes or a backslash) or newlines
  上面是man手册对xargs的说明。其实xargs就是对shell命令行参数限制做的一个扩展。
  这条命令的含义也很简单,将file中的内容作为参数传给X程序
  如果用echo作为X程序,则命令为:
  cat file | xargs echo
  此命令和cat file | xargs 行为一致,因为xargs的默认行为就是打印输出。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值