零零散散的python笔记
Python strip()方法
用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
#!/usr/bin/python
# -*- coding: UTF-8 -*- str = "00000003210Runoob01230000000";
print str.strip( '' ); # 去除首尾字符 0 str2 = " Runoob "; # 去除首尾空格
print str2.strip();
random.randint()与np.random.randint()的区别
random.randint()方法里面的取值区间是前闭后闭区间,而np.random.randint()方法的取值区间是前闭后开
import random
for n in range(5):
for i in range(10):
print(random.randint(1,5),end=' ')
print()
#运行结果
1 5 5 3 3 1 3 1 5 2
4 4 4 4 4 4 3 1 5 2
3 2 3 1 1 5 5 1 4 3
3 4 4 2 5 5 3 4 4 4
3 5 4 5 4 5 4 5 2 4
Process finished with exit code 0
import numpy as np
for n in range(5):
for i in range(10):
print(np.random.randint(1, 5), end=' ')
print() #运行结果
2 4 1 1 1 1 2 2 2 4
3 4 3 2 3 4 3 2 2 4
2 2 1 2 1 1 3 3 3 4
4 1 4 2 4 1 3 4 3 2
2 3 3 2 3 4 4 3 4 4
Process finished with exit code 0
numpy.random.randint: low、high、size三个参数。默认high是None,如果只有low,那范围就是[0,low)。如果有high,范围就是[low,high)。
>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) >>> np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) >>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
np.random.rand()

Python xrange() 函数
xrange() 函数用法与 range 完全相同,所不同的是生成的不是一个数组,而是一个生成器。
>>>xrange(8)
xrange(8)
>>> list(xrange(8))
[0, 1, 2, 3, 4, 5, 6, 7]
>>> range(8) # range 使用
[0, 1, 2, 3, 4, 5, 6, 7]
>>> xrange(3, 5)
xrange(3, 5)
>>> list(xrange(3,5))
[3, 4]
>>> range(3,5) # 使用 range
[3, 4]
>>> xrange(0,6,2)
xrange(0, 6, 2) # 步长为 2
>>> list(xrange(0,6,2))
[0, 2, 4]
python强制类型转换astype
- df.astype('数据类型') #改变整个df的数据类型
- df['列名'].astype('数据类型') #仅改变某一列的数据类型
Python的reshape(-1,1)


np.vstack:按垂直方向(行顺序)堆叠数组构成一个新的数组
https://www.jianshu.com/p/2469e0e2a1cf
np.random.seed(0)作用
当我们设置相同的seed,每次生成的随机数相同。
如果不设置seed,则每次会生成不同的随机数。
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55, 0.72, 0.6 , 0.54])
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55, 0.72, 0.6 , 0.54])
>>> numpy.random.rand(4)
array([ 0.42, 0.65, 0.44, 0.89])
>>> numpy.random.rand(4)
array([ 0.96, 0.38, 0.79, 0.53])
python3 * 和 **
* 代表乘法
** 代表乘方
>>> 2 * 5
10
>>> 2 ** 5
32
Python3 assert(断言)
Python assert(断言)用于判断一个表达式,在表达式条件为 false 的时候触发异常。
assert expression
等价于
if not expression:
raise AssertionError
assert expression [, arguments]
等价于
ifnot expression:raiseAssertionError(arguments)
以下为 assert 使用实例:
>>> assert False # 条件为 false 触发异常
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert 1==1 # 条件为 true 正常执行
>>> assert 1==2 # 条件为 false 触发异常
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert 1==2, '1 不等于 2'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: 1 不等于 2
>>>
set_printoption
numpy.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, formatter=None)
precision : int, optional,float输出的精度,即小数点后维数,默认8( Number of digits of precision for floating point output (default 8))
threshold : int, optional,当数组数目过大时,设置显示几个数字,其余用省略号(Total number of array elements which trigger summarization rather than full repr (default 1000).)
edgeitems : int, optional,边缘数目(Number of array items in summary at beginning and end of each dimension (default 3)).
linewidth : int, optional,The number of characters per line for the purpose of inserting line breaks (default 75).
suppress : bool, optional,是否压缩由科学计数法表示的浮点数(Whether or not suppress printing of small floating point values using scientific notation (default False).)
nanstr : str, optional,String representation of floating point not-a-number (default nan).
infstr : str, optional,String representation of floating point infinity (default inf).
np.set_printoptions(threshold=np.nan)
设置打印时显示方式,threshold=np.nan意思是输出数组的时候完全输出,不需要省略号将中间数据省略
零零散散的python笔记的更多相关文章
- 零零散散的python笔记 2
python2和python3的兼容性方面 工具 2to3 python3中自带的工具,可以检查python2升级到python3的diff: 2to3 x.py 2to3 -w x.py # ...
- Python笔记之不可不练
如果您已经有了一定的Python编程基础,那么本文就是为您的编程能力锦上添花,如果您刚刚开始对Python有一点点兴趣,不怕,Python的重点基础知识已经总结在博文<Python笔记之不可不知 ...
- boost.python笔记
boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...
- 20.Python笔记之SqlAlchemy使用
Date:2016-03-27 Title:20.Python笔记之SqlAlchemy使用 Tags:python Category:Python 作者:刘耀 博客:www.liuyao.me 一. ...
- Python笔记——类定义
Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...
- 13.python笔记之pyyaml模块
Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...
- 8.python笔记之面向对象基础
title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...
- python笔记 - day8
python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...
- python笔记 - day7-1 之面向对象编程
python笔记 - day7-1 之面向对象编程 什么时候用面向对象: 多个函数的参数相同: 当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可: ...
随机推荐
- Mybatis的mapper接口在Spring中实例化过程
在spring中使用mybatis时一般有下面的配置 <bean id="mapperScannerConfigurer" class="org.mybatis.s ...
- 布尔(boolean)代数趣味学习法
今天,我想出来一个学习布尔(boolean)代数的趣味方法: 比如:逻辑与(&)运算 逻辑里面就是并且形象的理解就是:从卧室里面外出,必须 卧室的门打开 “并且” 最外面的门打开,才能出去.用 ...
- [CTS2019]田野(80分)
loj嘟嘟嘟 学完模拟退火后开始搞这道题,搞了一下午最终搞到了80分,剩下的实在不知道怎么办了-- 首先肯定是把有交点的线段划分到一个集合,然后对每一个集合求一遍凸包. 然后两两合并,如果新的凸包的周 ...
- 【原创】洛谷 LUOGU P3371 【模板】单源最短路径
P3371 [模板]单源最短路径 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出 ...
- assert 笔记
目录 什么是assert? assert使用 assert错误使用 什么是assert? Python 的 assert 语句是一个 debug 的好工具,主要用于测试一个条件是否满足.如果测试的条件 ...
- Jenkins忘记admin密码补救措施
遇到将Jenkins登录名密码忘记的情况,下面的方式可以重置密码. 进入C:\Program Files (x86)\Jenkins\users 目录可以看到admin开头的文件夹,里面有个confi ...
- deepin grub2017年11月13日折腾记录
http://blog.csdn.net/atbird0321/article/details/78158194 https://bbs.deepin.org/forum.php?mod=viewth ...
- [vbs]脚本启动
Set ws = CreateObject("Wscript.Shell") ws.run "cmd.exe /c start tool.exe config_tence ...
- Shichikuji and Power Grid
D. Shichikuji and Power Grid 参考:Codeforces Round #597 (Div. 2) 思路:一个很裸的最小生成树.把建立基站看成是,城市与源点(虚构的)建边.由 ...
- 2018-2019-2 20175215 实验四《Android程序设计》实验报告
一.实验内容与步骤 1.任务一:Android Studio的安装测试. 任务要求:参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd) ...