1、list的拷贝:
浅拷贝:
x = ["a","b","c"]
y = x
y[1]="z"
print(x)
print(y)
输出结果(x,y指向同一块内存):
['a', 'z', 'c']
['a', 'z', 'c']
深拷贝:
x = ["a","b","c"]
y = list(x)
y[1]="z"
print(x)
print(y)
输出结果(x,y分别指向不同内存):
['a', 'b', 'c']
['a', 'z', 'c']
如:list的apppend,max,index方法等,str的len,replace方法等。
3、两个list的合并:
x = ["a","b","c"]
y = ["1","2","3"]
z = x+y
print(x)
print(y)
print(z)
输出结果:
['a', 'b', 'c']
['1', '2', '3']
['a', 'b', 'c', '1', '2', '3']
4、无需遍历list,利用numpy可直接对整个数组进行计算
例子:
list无法直接计算,会报错:
import numpy as np
height = [1.73,1.68,1.71,1.89,1.79]
weight = [65.4,59.2,63.6,88.4,68.7]
print(weight/height ** 2)
运行结果:
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'通过numpy:
import numpy as np
height = [1.73,1.68,1.71,1.89,1.79]
weight = [65.4,59.2,63.6,88.4,68.7]
np_height = np.array(height)
np_weight = np.array(weight)
print(np_weight / np_height ** 2)
运行结果:
[ 21.85171573 20.97505669 21.75028214 24.7473475 21.44127836]
5、numpy构造子集
import numpy as np
bmi = np.array([21.85,20.97,21.75,24.74,21.44])
print(bmi[1])
print(bmi > 21)
print(bmi[bmi > 21])
运行结果:
20.97
[ True False True True True]
[ 21.85 21.75 24.74 21.44]
6、二维numpy数组
#coding=utf-8
import numpy as np
height = np.round(np.random.normal(1.75, 0.20, 5000),2)#参数2指保留两位小数
weight = np.round(np.random.normal(60.32, 15, 5000), 2)
np_city = np.column_stack((height,weight))#列连接,构成5000*2的二维数组
print(np.mean(np_city[:,0]))#平均身高
print(np.median(np_city[:,0]))#身高中位数
print(np.corrcoef(np_city[:,0], np_city[:,1]))#身高与体重的相关度
print(np.std(np_city[:,0]))#身高标准差
运行结果:
1.74872
1.75
[[ 1. 0.02158409]
[ 0.02158409 1. ]]
0.200249748065
7、matplotlib基本作图
#coding=utf-8
import matplotlib.pyplot as plt
from statsmodels.sandbox.regression.kernridgeregress_class import plt_closeall
year = [1950, 1970, 1990, 2010]
pop = [2.519, 3.692, 5.263, 6.972]
#加入历史数据
pop = [1.0,1.262, 1.650] + pop
year = [1800,1850,1900] + year
plt.fill_between(year, pop, 0, color = "green")#填充颜色
plt.xlabel('year')#添加轴的标签
plt.ylabel('population')
plt.title("World Population Projections")#添加图标题
plt.yticks([0,2,4,6,8,10],
['0','2B','4B','6B','10B'])#添加y轴刻度
plt.plot(year, pop)#画线图
plt.scatter(year, pop)#散点图
plt.show()
CSV文件:
#coding=utf-8
import pandas as pd
brics = pd.read_csv(r"C:\brics.csv")
print(brics)
brics = pd.read_csv(r"C:\brics.csv",index_col=0)
print(brics)
#获取列
print(brics["country"])
#添加列
brics["on_earth"] = [True, True, True, True, True]
brics["density"] = brics["population"] / brics["area"] *1000000
print(brics)
#获取行
print(brics.loc["BR"])#以列的形式表示
#元素的获取
print(brics.loc["CH"]["capital"])
运行结果:
Unnamed: 0 country population area capital
0 BR Brazil 200 8515767 Brasilia
1 RS Russion 144 17098242 Moscow
2 IN India 12522 3287590 New Delhi
3 CH China 1357 9596961 Brijing
4 SA South Africa 55 1221037 Pretoria
country population area capital
BR Brazil 200 8515767 Brasilia
RS Russion 144 17098242 Moscow
IN India 12522 3287590 New Delhi
CH China 1357 9596961 Brijing
SA South Africa 55 1221037 Pretoria
BR Brazil
RS Russion
IN India
CH China
SA South Africa
Name: country, dtype: object
country population area capital on_earth density
BR Brazil 200 8515767 Brasilia True 23.485847
RS Russion 144 17098242 Moscow True 8.421918
IN India 12522 3287590 New Delhi True 3808.869111
CH China 1357 9596961 Brijing True 141.398928
SA South Africa 55 1221037 Pretoria True 45.043680
country Brazil
population 200
area 8515767
capital Brasilia
on_earth True
density 23.4858
Name: BR, dtype: object
Brijing