(1)列表/元组 list/tuple
列表/元组:
cmp(a,b) 比较两个列表/元组的元素
sorted(a) 对列表的元素进行升序排序
列表:
a.index(x) 从列表a中找出第一个x索引的位置
a.insert(x,y) 将y插入列表a的索引为x的位置
a.pop(x) 移除列表a中索引值为x的元素
功能上看,列表与元组的区别是,列表可以被修改,二元组不可以。
比如,对于a=[1,2,3],那么语句a[0]=0,就会将列表修改为[0,2,3],而元组b=(4,5,6),语句b[0]=1会报错。
要注意的是如果已经有了一个列表a,同时想复制a,命名为变量b,那么b=a是无效的。这时b仅是a的别名(或者说引用),修改b也会修改a
list的复制正确方法:
浅拷贝:
①copy()方法:
对于List来说,其第一层,是实现了深拷贝,但对于其内嵌套的List,仍然是浅拷贝。
因为嵌套的List保存的是地址,复制过去的时候是把地址复制过去了,嵌套的List在内存中指向的还是同一个。
old = [1,[1,2,3],3]
new = old.copy()
new[0] = 3
new[1][0] =3
'''
---------------------
Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [3, 2, 3], 3]
[3, [3, 2, 3], 3]
---------------------
'''
②使用列表生成:
同上
old = [1,[1,2,3],3]
new = [i for i in old]
new[0] = 3
new[1][0] = 3
'''
----------------------
Before
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After
[1, [3, 2, 3], 3]
[3, [3, 2, 3], 3]
----------------------
'''
③for循环遍历:
同上
old = [1,[1,2,3],3]
new = []
for i in range(len(old)):
new.append(old[i])
new[0] = 3
new[1][0] = 3
'''
-----------------------
Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [3, 2, 3], 3]
[3, [3, 2, 3], 3]
-----------------------
'''
④使用 [ : ] 切片:
同上
old = [1,[1,2,3],3]
new = old[:]
new[0] = 3
new[1][0] = 3
'''
------------------
Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [3, 2, 3], 3]
[3, [3, 2, 3], 3]
------------------
'''
深拷贝:
⑤deepcopy():
无论多少层,无论怎样的形式,得到的新列表都是和原来无关的
需要import copy
import copy
old = [1,[1,2,3],3]
new = copy.deepcopy(old)
new[0] = 3
new[1][0] = 3
'''
-----------------------
Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [1, 2, 3], 3]
[3, [3, 2, 3], 3]
-----------------------
'''
list('ab') => ['a','b']
tuple([1,2]) => (1,2)
(2)字典 dict
创建字典:
1.d = {"today" :20,'tomorrow':30}
2.dict ([['today',20] , [ 'tomorrow', 30 ] ])
3.dict. fromkeys ( [ 'today', 'tomorrow'] , 20) #相当于{'today': 20, 'tomorrow' : 20}
dict.items() (keys,value)元组对
dict.keys() 键列表
dict.setdefault(key,default=None) 类似于get(),若key不存在字典中,则赋值dict['key']=default
dict.update(dict2) 将dict2键值对添加到字典dict
(3)集合
创建集合:
1.s = {1, 2, 2, 3} #注意2会自动去重,得到{1, 2, 3}
2.s = set([1, 2, 2, 3])
(4)函数式编程
filter函数:把函数作用在序列元素上,但是该函数的结果必须是布尔型,filter跟据true或者false进行元素的保护和删除
lambda函数:匿名函数,可以不用定义函数名,因此可以使用匿名函数也可以被一个变量保存,也可以不保存直接用
x=[1,8,3,9,4,6,7,9,6,4,6,74]
list(filter(lambda s:s%2==0,x))
[8, 4, 6, 6, 4, 6, 74]
- map函数:本身是个series的函数,在df结构中无法使用,map函数作用于Serices中每个元素
- applymap函数:作用于df数据中每个数据
- apply函数:作用于df数据中的每行或者每列数据
df = pd.DataFrame(np.random.randint(0,10,(4, 3)), columns=list('bde'), index=range(4))
b d e
0 0 2 3
1 8 8 7
2 7 2 1
3 7 2 6
applymap:
>>> df.applymap(lambda x:x+1 if x%2==0 else x)
b d e
0 3 1 5
1 9 9 1
2 3 7 7
3 5 9 5
apply:
f=lambda x:x.max()-x.min()
>>> df.apply(f)
b 6
d 9
e 5
dtype: int64
>>> df.apply(f,axis=1) # 作用在一行上
0 5
1 8
2 3
3 4
dtype: int64
>>> df.apply(f,axis=0) # 作用在一列上,axis=0可省略
b 6
d 9
e 5
dtype: int64
(5)库的导入与添加
import math // from math import *
函数 数学表示 含义
圆周率pi π π的近似值,15位小数
自然常数e e e的近似值,15位小数
ceil(x) [x] 对浮点数向上取整
floor(x) [x] 对浮点数向下取整
pow(x,y) x^y 计算x的y次方
log(x) lg x 以e为基的对数
log10(x) log10x 以10为基的对数
sqrt(x) 计算x的平方根 平方根
exp(x) e^x e的x次幂
degree(x) 将弧度值转换成角度值
radins(x) 将角度值转换成弧度值
sin(x) sin x 正弦函数
cos(x) cos x 余弦函数
tan(x) tan x 正切函数
asin(x) arcsin x 反正弦函数,x∈[-1.0,1.0]
acos(x) arccos x 反余弦函数,x∈[-1.0,1.0]
atan(x) arctan x 反正切函数,x∈[-1.0,1.0]