Getting started with python (four): import and add libraries in python

Although there are many powerful libraries (modules, packages) in Python, in our actual use, we often need to load more libraries (modules, packages). You even need to install third-party extension libraries to enrich python's functions.

One, the import of the library

Python itself has many powerful libraries built in, which can be directly imported for us to use.
Example (using math library as an example):

(1) Import the library directly:

Code:

import math
a = math.sin(1) #计算正弦
b = math.cos(1) #计算余弦
c = math.pi #内置的圆周率常数
print(a)
print(b)
print(c)

result:

Insert picture description here

(2) Use alias to import library:

Code:

import math as m
a = m.sin(1)
print(a)

result:

Insert picture description here

(3) Import direct function by name

Code:

from math import exp as e #只导入math库中的exp并起名为e
a = e(1) #计算指数
print(a)
b = sin(1) #因为只导入了exp,所以使用sin会报错
print(b)

result:
Insert picture description here

(4) Import all functions in the library

Code:

from math import *
a = exp(1)
b = sin(1)
print(a)
print(b)

result:

Insert picture description here

Two, add a third-party library

Although there are many powerful libraries (modules, packages) in Python, in our actual use, we often need to load more libraries (modules, packages). You even need to install third-party extension libraries to enrich python's functions.

If the reader installs the anaconda distribution, then he already has the following libraries: numpy, scipy, matplotlib, pandas, scikit-learn.

Insert picture description here

There are many ways to add third-party libraries, here is only the simplest method ( pip installation ), readers can learn about other methods by themselves.
Insert picture description here

pip command format: pip install library name

(1) numpy library
Insert picture description here

Because I have already installed it, it shows installed

After the installation is complete, you can use the numpy library to manipulate the data

Code:

import numpy as np #一般以np为别名导入numpy库
a = np.array([2,0,1,5]) #创建数组
print(a) #输出数组
print(a[:3]) #引用前三个数字(切片)
a.sort() #将a的元素从小到大排序,此操作直接修改a,此时a为[0,1,2,5]
b = np.array([[1,2,3],[4,5,6]]) #创建二维数组
print(b*b) #输出数组的平方阵,即[[1,4,9],[16,25,36]]

result:

Insert picture description here

(2) jieba foot
Insert picture description here

After the installation is complete, you can use the jieba library to manipulate the data

Code:



#读取文件
f=open('乘风破浪的姐姐.txt',encoding='utf-8')
data = f.readlines()  # 直接将文件中按行读到list里,读取后的文件格式为数组
f.close()  # 关

#将文件转换成字符串
text=""
for line in
data:
    text += line
print(text)

import re #导入库
import jieba

text = re.sub(r'[[0-9]*]',' ',text)#去除类似[1],[2]
text = re.sub(r'\s+',' ',text)#用单个空格替换了所有额外的空格
sentences = re.split('(。|!|\!|\.|?|\?)',text)#分句:re.split(’(。|!|!|.|?|?)’,text) 加括号则保留分句符号如。!,不加则不保留分句符号
print(sentences)

result:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45154565/article/details/109146621