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 True     # 条件为 true 正常执行
>>> 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=Nonethreshold=Noneedgeitems=Nonelinewidth=Nonesuppress=Nonenanstr=Noneinfstr=Noneformatter=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笔记的更多相关文章

  1. 零零散散的python笔记 2

    python2和python3的兼容性方面 工具 2to3 python3中自带的工具,可以检查python2升级到python3的diff: 2to3 x.py 2to3 -w x.py     # ...

  2. Python笔记之不可不练

    如果您已经有了一定的Python编程基础,那么本文就是为您的编程能力锦上添花,如果您刚刚开始对Python有一点点兴趣,不怕,Python的重点基础知识已经总结在博文<Python笔记之不可不知 ...

  3. boost.python笔记

    boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...

  4. 20.Python笔记之SqlAlchemy使用

    Date:2016-03-27 Title:20.Python笔记之SqlAlchemy使用 Tags:python Category:Python 作者:刘耀 博客:www.liuyao.me 一. ...

  5. Python笔记——类定义

    Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...

  6. 13.python笔记之pyyaml模块

    Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...

  7. 8.python笔记之面向对象基础

    title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...

  8. python笔记 - day8

    python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...

  9. python笔记 - day7-1 之面向对象编程

    python笔记 - day7-1 之面向对象编程 什么时候用面向对象: 多个函数的参数相同: 当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可: ...

随机推荐

  1. 部署lnmp

    装包 1.安装依赖包 yum - y install gcc openssl-devel pcre-devel zlib-devel 2.解源码包 .tar.gz 3.切换到解压缩后的目录,配置参数 ...

  2. MyEclipse运行项目出现 The user operation is waiting for "Building workspace" to complete

    如图所示 解决方式 1.选择菜单栏的“Project”,然后把菜单栏中“Build Automatically”前面的对钩去掉. 2.当你修改或添加代码后,选择菜单栏的“Project”,然后选择菜单 ...

  3. For 循环的嵌套与九九乘法表

    ㈠通过程序,在页面中输入如下图形 * * * * * * * * * * * * * * * * * * * * * * * * *  代码如下: //向body中输入一个内容 //document. ...

  4. luogu 2577 [ZJOI2005]午餐 贪心+dp

    发现让 $b$ 更大的越靠前越优,然后依次决策将每个人分给哪个窗口. 令 $f[i][j]$ 表示考虑了前 $i$ 个人,且第一个窗口的总等待时间为 $j$ 的最小总时间. 然后转移一下就好了~ #i ...

  5. 一、微服务(Microservices)【翻译】

    1.微服务 “微服务架构(Microservice Architecture)”一词在过去几年里广泛的传播,它用于描述一种设计应用程序的特别方式,作为一套独立可部署的服务.目前,这种架构方式还没有准确 ...

  6. LGU67496 小$s$的玻璃弹珠

    题意 在一幢\(m\)层建筑你将获得\(n\)个一样的鸡蛋,从高于\(x\)的楼层落下的鸡蛋都会碎.如果一个蛋碎了,你就不能再把它掉下去. 你的目标是确切地知道\(x\)的值.问至少要扔几次才能确定. ...

  7. linux下通过进程名查看其占用端口

    linux下通过进程名查看其占用端口: 1.先查看进程pid ps -ef | grep 进程名 2.通过pid查看占用端口 netstat -nap | grep 进程pid 例:通过nginx进程 ...

  8. mysql 查看当前正在执行的语句

    查看当前正在执行的语句 show processlist:show processlist; 结束正在执行的语句进程 kill 进程id

  9. LeetCode109----链表转为二叉搜索树

    给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例:给定的有序链表: [-10, ...

  10. Linux设备驱动程序 之 open和release

    open方法 open方法提供给驱动程序以初始化的能力,在大部分驱动程序汇总,open应该完成以下工作: 1. 检查特定设备的错误,如设备为准备就绪或者硬件问题: 2. 如果设备是首次打开,则对其进行 ...