零零散散的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 之面向对象编程 什么时候用面向对象: 多个函数的参数相同: 当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可: ...
随机推荐
- Codeforces Round #587 (Div. 3) A. Prefixes
链接: https://codeforces.com/contest/1216/problem/A 题意: Nikolay got a string s of even length n, which ...
- python--openCV--其它
t1=cv2.getTickCount() # 记录当前时间,以时钟周期计算 t2=cv2.getTickFrequency() #返回时钟周期,返回CPU的频率,返回CPU一秒中所走的时钟周期数
- BZOJ 4562: [Haoi2016]食物链 拓扑排序
建反图,跑一个拓扑排序dp即可. Code: #include <bits/stdc++.h> #define ll long long #define N 100005 #define ...
- 分布式缓存Redis之Pipeline(管道)
Redis的pipeline(管道)功能在命令行中没有,但redis是支持pipeline的,而且在各个语言版的client中都有相应的实现. 由于网络开销延迟,就算redis server端有很强的 ...
- Linux长格式文件属性介绍
长格式文件属性 查看长格式文件命令:ll (或ls -l) (1)-:文件类型 -:普通文件 d:目录 b:块设备文件(随机读取) c:字符设备文件(顺序读取) p:管道文件 s:Socket套接字文 ...
- [CSP-S2019]:赛后总结
笔者有幸参加了$CSP-S\ 2019$,$AFO$之前,写下自己最后一篇赛后总结. $Day\ 0$ 早上起来把自己调了一晚上被卡空间的题卡过了,很开心(内存限制$256MB$,然而我的内存申请是$ ...
- 9.Spring Cloud Config统一管理微服务配置
Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...
- Flume-概述
Flume 是 Cloudera 提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统.Flume 基于流式架构,灵活简单. Flume最主要的作用就是,实时读取服务器本地磁盘的数据, ...
- mac 安装laravel
安装laravel之前先安装composer 使用 curl 指令下载: curl -sS https://getcomposer.org/installer | php 或是沒有安裝 curl ,也 ...
- VIM | vim操作大全
1. 关于Vim vim是Linux下第二强大的编辑器. 虽然emacs是公认的世界第一,我认为使用emacs并没有使用vi进行编辑来得高效. 如果是初学vi,运行一下vimtutor是个聪明的决定. ...