Python基础中的一些重点


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']


2、在python中任何东西都是对象,只要存在内置方法,就可以直接调用,无需自己实现,非常方便,这也是python代码简洁凝练的一个原因。

如: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()



8、pandas(基于numpy,并且能处理不同数据类型的数据)

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











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值