Python List: A compound data type
1. Name a collection of values
2. Contain any type
3. Contain different type
4. Contain Sub List
![](https://img.haomeiwen.com/i646542/7aa8b96e6a0af6ba.png)
List
![](https://img.haomeiwen.com/i646542/0b8eb6271d88b60e.png)
Sub List
![](https://img.haomeiwen.com/i646542/b5b2e98fda314c34.png)
List Slicing 切片 可正数可倒数
![](https://img.haomeiwen.com/i646542/14d0754ee2f33415.png)
包含头,不包含尾巴:List[0:6] 刚好是前6个数字!List[-6:] 就是倒数6个值
![](https://img.haomeiwen.com/i646542/bee9cba9569cbe50.png)
可省略第一个/最后一个指标
![](https://img.haomeiwen.com/i646542/76958fec90ead485.png)
更改List里的元素:直接重新赋值,然后List就改变了
![](https://img.haomeiwen.com/i646542/efa4f6d9a52ae6ec.png)
List里增加元素:fam=fam+["me",1.79]
![](https://img.haomeiwen.com/i646542/824e8c428a6dfda9.png)
删除List里的元素:del(), fam也直接改变了
![](https://img.haomeiwen.com/i646542/bfc85539fb409dab.png)
把X赋值给了Y,y改了之后X也改了:因为是同一个容器(类似于只是改了个名字)
![](https://img.haomeiwen.com/i646542/88576b0d649fced9.png)
但是,如果你用list()函数赋值,就只是把容器x里的内容复制到了容器y,对y的改动就不影响x
![](https://img.haomeiwen.com/i646542/d8dcd0b5a995640e.png)
同一行的不同命令用分号隔开
![](https://img.haomeiwen.com/i646542/a90bb78238e351b2.png)
用分号隔开的两个命令不是同时执行的,所以删除了倒数第3个之后,原来的倒数第4个就又变成倒数第三个了(所以上图中的答案是错的,应该选第三个)
![](https://img.haomeiwen.com/i646542/496bcf7371301e78.png)
Function
![](https://img.haomeiwen.com/i646542/4b7a1448b68f6afc.png)
max() 求最值的函数
![](https://img.haomeiwen.com/i646542/9a2478125ad2ccc3.png)
通过Help(函数名)可以查询不同函数的原始代码
![](https://img.haomeiwen.com/i646542/d924b93b393ddc50.png)
你觉得应该用函数解决的事情一定就有函数可以解决!
![](https://img.haomeiwen.com/i646542/c4de0e903ea58c92.png)
?max :也可以查询函数使用规则
![](https://img.haomeiwen.com/i646542/cd0768db6afb062a.png)
key=None means that if you don't specify the key argument, it will be None. reverse=False means that if you don't specify the reverse argument, it will be False.
Methods:Functions that belong to objects
![](https://img.haomeiwen.com/i646542/8bad1edca5a932c1.png)
List对应的method:index() count()
![](https://img.haomeiwen.com/i646542/96e83a30698b9798.png)
字符串对应的method: capitalize() replace()
![](https://img.haomeiwen.com/i646542/6dc156f2f2f97a15.png)
不同type的object有不同的method,不可以混用
![](https://img.haomeiwen.com/i646542/40a297a1b7de9a1e.png)
list.append() 可以从list尾巴添加元素
str.upper() 全部大写
str.count('x') 在str里数有几个x
list.count('x') 同上
append(), that adds an element to the list it is called on,
remove(), that removes the first element of a list that matches the input, and
reverse(), that reverses the order of the elements in the list it is called on.
目前看到这里了
![](https://img.haomeiwen.com/i646542/896643b4eda3df0b.png)
Package:分不同的module 包含特定的函数、方法和类型
![](https://img.haomeiwen.com/i646542/2f8a2972a8057210.png)
可以直接在conna环境下装包,具体参考上文
![](https://img.haomeiwen.com/i646542/9bb9b7583791c42c.png)
import xx 就是导入某个特定的包,然后就可以用特定的函数、变量、类型,比如导入numpy后就可以用numpy.array, 它是numpy的数组
![](https://img.haomeiwen.com/i646542/7f00477cc6af0598.png)
使用array函数创建时,参数必须是由方括号括起来的列表
![](https://img.haomeiwen.com/i646542/e440f5d2d07dcace.png)
import math之后就可以用math.pi这个常量
![](https://img.haomeiwen.com/i646542/f7c93f4af763d722.png)
可以选择性的导入某个package里的某个函数:from xx import xx
将角度转换为弧度:radians()
![](https://img.haomeiwen.com/i646542/06406fe05cc78927.png)
the function inv():in the linalg subpackage of the scipy package
The as word allows you to create a local name for the function you're importing
![](https://img.haomeiwen.com/i646542/18aa3501438da618.png)
List不能很好支持数据科学的数学和统计操作
![](https://img.haomeiwen.com/i646542/a9c086987a806387.png)
list**int 是不被支持的
![](https://img.haomeiwen.com/i646542/5850499095657e37.png)
numpy的数组完美支持数据操作:可以直接对整个数组进行操作
![](https://img.haomeiwen.com/i646542/a4b8507989548807.png)
通过np.array将已有的list转化为np数组,然后直接进行数据运算
Elements-wise calculation 会智能对数据进行一一对应的计算
![](https://img.haomeiwen.com/i646542/af20e4b33e77e933.png)
List可以装不同类的数据,array只能装同一类的数据:将所有输入的数值转化成了同一个类型
![](https://img.haomeiwen.com/i646542/e288cd2dc22d04f3.png)
List的加法运算是添加元素,array的加法运算是元素的相加
NumPy Subsetting
![](https://img.haomeiwen.com/i646542/ef6d3c67c63a0dc6.png)
通过array[array>23]可以找到array里所有大于23的元素
![](https://img.haomeiwen.com/i646542/912c9bddeacc891d.png)
np.array(list)——>array
![](https://img.haomeiwen.com/i646542/c551526074699597.png)
print(bmi[light]):输出所有bmi<21的球员的bmi值
type coercion 强制类型转换
numpy arrays cannot contain elements with different types.
![](https://img.haomeiwen.com/i646542/2af100911a3b20d7.png)
True被强制转换成了1,False被强制转换成了0
![](https://img.haomeiwen.com/i646542/80d0e217a4e10371.png)
array的切片和List的切片规则一样
ndarray=N-dimensional Array
![](https://img.haomeiwen.com/i646542/0a6e56c99da3d033.png)
array.shape 可以得到数组的行列数
![](https://img.haomeiwen.com/i646542/c3e077a848ae0b0b.png)
array[0]默认是第一行
![](https://img.haomeiwen.com/i646542/b7faa2353725089b.png)
array[row,column]
![](https://img.haomeiwen.com/i646542/43809e00f1a4e4c5.png)
所有行里的第二和第三个元素
![](https://img.haomeiwen.com/i646542/479a17f1b3389d07.png)
第二行的所有列
![](https://img.haomeiwen.com/i646542/cf0ca0d24d4852e8.png)
array.shape 数组的行数和列数
![](https://img.haomeiwen.com/i646542/70e4986aed975766.png)
每一行代表一个记录,每一列代表一个变量
![](https://img.haomeiwen.com/i646542/24b28d08ee4344b3.png)
可以通过建立一个新的array,然后通过数组运算直接更新/转换数据
![](https://img.haomeiwen.com/i646542/8844ea71fb8ddbae.png)
np.mean()均值 np.median()中值 np.corrcoef() 相关系数 np.std() 方差
![](https://img.haomeiwen.com/i646542/9b7842233791fe01.png)
可以用来生成随机的符合特定分布的数据:np.round(np.random.normal(mean, sd, N),decimal)
![](https://img.haomeiwen.com/i646542/8d9d8bfe3baebe9a.png)
np.mean(array)
![](https://img.haomeiwen.com/i646542/ffc5d487d20653e2.png)
np.corrcoef(array1,array2)
![](https://img.haomeiwen.com/i646542/adee1c55def44772.png)
array[array=='kk'] array[array!='KK']
1. list里每个元素用逗号隔开,如果元素本身是list,不要漏掉逗号
2. 大小写敏感
3. 空格敏感
# 注释
**乘方
Shift+Enter >Run
网友评论