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. intellij idea gradle 导入 spring 问题记录

    环境: windows 7 oracle jdk 1.8 intellij idea 2019.3.1 spring-framework 5.1.22.RELEASE 步骤: 1: 下载解压sprin ...

  2. JavaScript变量、作用域和内存问题总结

    ㈠理解基本类型和引用类型的值  ⑴JavaScript变量可以用来保存两种类型的值:基本类型值和引用类型值. ⑵基本类型的值源自以下 5 种基本数据类型:Undefined.Null.Boolean. ...

  3. java读取 xml文件

    java读取xml文件的四种方法  转自https://www.cnblogs.com/lingyao/p/5708929.html Xml代码 1 <?xml version="1. ...

  4. SQL Server孤立用戶

    如何解决孤立用户问题 http://blog.csdn.net/zzl1120/article/details/7394468 SQL SERVER孤立用户问题解决方法 http://www.2cto ...

  5. a a[0] &a &a[0]的理解

    数组中几个关键符号(a a[0] &a &a[0])的理解(前提是 int a[10])(1)这4个符号搞清楚了,数组相关的很多问题都有答案了.理解这些符号的时候要和左值右值结合起来, ...

  6. IVIEW组件Table中加入EChart柱状图

    展示图如下: 主要利用了render函数和updated()钩子函数进行数据填充与渲染. 1.在Table的Colums中加入 1 { 2 title: '比例图', 3 align: 'center ...

  7. 单链表之Java实现

    初次接触java,用java也写了一个链表.代码如下: import java.io.*; class Node{ public int data; //数据域 public Node next; / ...

  8. CF1207A

    CF1207A-There Are Two Types Of Burgers 题意: 出售普通汉堡和鸡肉汉堡,并且两种汉堡所需的原材料价格不同,问最多能卖多少钱. 解法: 对于这道题,我们优先考虑先卖 ...

  9. sql语句中where 1=1和 0=1 的作用

    sql where 1=1和 0=1 的作用   where 1=1; 这个条件始终为True,在不定数量查询条件情况下,1=1可以很方便的规范语句. 一.不用where  1=1  在多条件查询中的 ...

  10. np数组转换函数

    1.多维数组降为一维: a = np.arange(24) np.ravel(a)或者a.ravel a.flatten 2.数据类型转换 a = a.astype(np.float32) //tf是 ...