Getting started with python, just read this article

Getting started with python is enough to read this article (recommended collection)

The original intention of writing this article is that I often use different languages ​​​​such as MATLAB, C++\C, R language, python, etc., because of their small differences in data structure and syntax, which often confuse me, hereby make a note, Make a basic backup of python's commonly used data structures and functions, and use them for query.

Is it necessary to learn a language systematically from head to toe? I think it's necessary, but pointless. If you have some foundation in other languages ​​and want to get started with python, the best way is to learn what data structures and loops it has, how to write it, and type some code to test it yourself, and the rest is to start directly with it What you want it to do for you, if you encounter problems with browser-oriented programming, just get familiar with it bit by bit.

Basic data structure (this section will be updated dynamically)

Talk is cheap. Show you the code!

The most basic data structures and usages in python (lists, numpy arrays, etc.) are listed below. It takes you 10 minutes to try each of them. If you can’t understand why the running results are like that, check them out. Understand, then your python has already started. Mainly the 6 basic data structures of python, the usage of numpy package. Pandas and matplotlib can be put away first, because they are not particularly critical in comparison. Other bells and whistles, such as classes, functions, anonymous expressions, etc., are not the key, and you can use them later to explore.

from matplotlib import pyplot as plt
import numpy.matlib
import numpy as np

#基本运算
9 // 4  # 除法,得到一个整数
2 ** 5  # 乘方

## 基本数据类型
# Python3 中有六个标准的数据类型:Number(数字)String(字符串)
# List(列表)Tuple(元组)Set(集合)Dictionary(字典)
# 不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组);
# 可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)。
list = [2.23, 'test', 70.2]  # list 相当于 MATLAB 的元胞数组,支持不同的类型,用中括号括起来
print(list)           # 输出完整列表
list[1:3]     # list 索引从 0 开始
list[1] = []   # 将对应的元素值设置为 [],并不代表去掉这个数
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[3:4] = []  # 去掉某一个数,a[3] = [] 这样不行,只有列表赋值列表可以

str = 'lusongno1'  # 可以把字符串看成是每个位置都是一个字母的 list,但赋值后不可变
str[0:-1]   # 下标从 0 开始,左开右闭区间,-1 表示最后一个位置
str[2:]     # 用法和 MATLAB 类似,只不过 end 直接不写了
str * 2     # 输出字符串两次
str + "TEST"  # 连接字符串直接用加号

dict = {
    
    } #大括号里面不写东西就是初始化为字典,否则初始化为字典
dict2 = {
    
    'one': 'test', 2: 'lusong'}
dict['one'] = "test"
dict[2] = "lusong"
dict.keys()
print(dict.values())
print(dict)

tuple1 = ('abcd', 786, 2.23)  # 注意,元组是用小括号括起来的
print(tuple)             # 打印元组
print(tuple1[1:3])        # 其他用法和list 类似,但是不可修改
tup1 = ()    # 空元组
tup2 = (20,)  # 一个元素,需要在元素后添加逗号

sets = {
    
    'e1', 'e2', 3, 'e4', 'e5', 'e5'} #集合里面也支持不同元素
print(sets)   #输出集合,重复的元素被自动去掉
a = set('abracadabra')
b = set('alacazam')
(a - b)  # a 和 b 的差集
(a | b)  # a 和 b 的并集
(a & b)  # a 和 b 的交集
(a ^ b)  # a 和 b 中不同时存在的元素

# 推导式用法
{
    
    x: x**2 for x in (2, 4, 6)} #元组遍历自动推导出字典,对别的数据结构也有类似用法
names = ['Bob', 'Tom', 'alice', 'Jerry', 'Wendy', 'Smith']
new_names = [name.upper() for name in names if len(name) > 3]#列表自动推导
a = (x for x in range(1, 10)) #返回的是生成器对象
tuple(a)       # 使用 tuple() 函数,可以直接将生成器对象转换成元组

# numpy 用法
np.eye(4)
a1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a1@a1  # np 的矩阵乘法
a = np.array([1, 2, 3, 4, 5], ndmin=2) #ndmin 表示生成的数据是几维的,低于实际维数不起效
a = np.array([1,  2,  3], dtype=complex) #复数类型的
dt = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')]) #用元组来定义类型名称和类型
a = np.array([('abc', 21, 50), ('xyz', 18, 75)], dtype=dt) #相当于定义了一张表,dt 是表头
print(a['age'])
a = np.arange(24)
print(a.ndim)             # a 现只有一个维度,python 是行优先的
b = a.reshape(2, 4, 3)  # b 现在拥有三个维度,等价于使用 a.shape = (2,4,3)
print(b.ndim)
print(a.shape)
xx = np.empty([3, 2], dtype=int)
print(xx)
x = np.zeros([5,2]) #方法的方式,一般同一参数输入用列表或者元组圈一起
x2 = np.zeros([5,2]) #方法的方式,一般同一参数输入用列表或者元组圈一起
x = np.ones([2, 2], dtype=int)
print(x)
x = [(1, 2, 3), (4, 5,6)]
a = np.asarray(x) #把任意的数据结构转成 numpy array,比较随意
print(a)
x = np.arange(10, 20, 2) #指定间隔生成数组
print(x)
a = np.linspace(1, 10, 5) #均分的方式生成数组,和 MATLAB 类似
a = np.logspace(0.0,2.0,10, base=10) #把 0 到 2 十等分,再取 10 为底数
a = np.arange(10) #表示 [0,10],python 总是左闭右开的
b = a[2:7:2]   # 从索引 2 开始到索引 7 停止,间隔为 2,这和 MATLAB 类似,只不过间隔是最后一个
a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
print(a)
print(a[1:]) #取第 1 行到最后 1 行,标号从 0 开始
a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
print(a[..., 1])   # 第2列元素,... 相当于是 MATLAB 的 1:end,即取全部 :
print(a[1, ...])   # 第2行元素
print(a[..., 1:])  # 第2列及剩下的所有元素
x = np.array([[0,  1,  2], [3,  4,  5], [6,  7,  8], [9,  10,  11]])
rows = np.array([[0, 0], [3, 3]])
cols = np.array([[0, 2], [0, 2]])
y = x[rows, cols] #得到和 rows与cols规模大小一样的矩阵,其中 rows 行标,cols 列标
print(y)
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = a[1:3, 1:3]
c = a[1:3, [1, 2]] #注意,1:3 并不等同于 [1,2]
d = a[..., 1:] #当输出的结果形状无要求的时候,用法和 MATLAB 一致
x = np.array([[0,  1,  2], [3,  4,  5], [6,  7,  8], [9,  10,  11]])
print(x[x > 5]) #使用 bool 方式类索引,和 MATLAB 一致
x = np.arange(32).reshape((8, 4))
print(x[[-4, -2, -1, -7]]) #索引一个位置的参数,只能输入一个对象
x = np.arange(32).reshape((8, 4))
print(x[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])]) #取出1572行的0312列
x[1:5, [0, 3, 1, 2]]# 不同于 x[[1,2,3,4], [0, 3, 1, 2]]
a = np.array([[0, 0, 0],
              [10, 10, 10],
              [20, 20, 20],
              [30, 30, 30]])
b = np.array([1, 2, 3]) #自动广播机制
print(a + b)
a = np.arange(6).reshape(2, 3) #数据的内存排布方式由创建时就有了,而不由 reshape 改变
for x in np.nditer(a.T):
    print(x, end=", ")#在输出中自动包含换行取消,改成 ‘,’
print('\n')
print(a.flatten()) #按行拉成一条,相当于 MATLAB 的 (:),和 nditer 不同,不是由数据创建时就决定了
print(a.flatten(order='F')) #列优先,也是 MATLAB 的风格
print(a.T) #求转置
a = np.arange(8).reshape(2, 2, 2)
print(np.where(a == 6)) # 使用 where查找满足条件的下标
a = np.empty([4,4]) #生成空数组
x = np.arange(9).reshape(1, 3, 3)
y = np.squeeze(x) #把一维的维度给挤压掉,
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.concatenate((a, b))) #沿着 x 轴拼接,输入方式以元组的方式提供
print(np.concatenate((a, b), axis=1)) #沿着 y 轴拼接
s1 = np.stack((a, b), 0) #自然堆叠到第 3 维
s2 = np.stack((a, b), 1) # a b 第一行堆叠到第一个维度
c = np.hstack((a, b)) #水平拼接,同 concatenate
c = np.vstack((a, b)) #竖直拼接
a = np.arange(12)
b = np.split(a, 3)  #平均分成 3 份的列表
b = np.split(a, [4, 7]) #在 4 和 7 两个位置分割,47 分到了下一份
a = np.arange(16).reshape(4, 4)
b = np.split(a, 2) #第一个维度,即行分割成两份
c = np.split(a, 2, 1) #沿列方向分割
b = np.vsplit(a, 2) #沿着 x 方向分割
d = np.hsplit(a, 2) #沿着 y 方向分割
a = np.floor(10 * np.random.random((2, 6))) #随机数和取整
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.resize(a, (3, 3)) #不够的话会重复地利用
np.append(a, [[7, 8, 9]], axis=0) #在 x 轴上拼接
np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1) #和list 也可以拼接
a = np.array([[1,2],[3,4],[5,6]])
b = np.insert(a, 3, [11, 12]) #在标号为 3 的数字前面插入 [11,12],并拉成一列(因为未传递参数)
c = np.insert(a, 1, [11,12], axis=0) #在行 1 前面,插入一行 11 12
d = np.insert(a, 1, 11, axis=1)# 在列 1 前面,插入一行全 11
a = np.arange(12).reshape(3, 4)
print(np.delete(a, 5))
print(np.delete(a, 1, axis=1)) #删除列 1
a = np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9])
u = np.unique(a) #去重并且进行了一个从小到大的排列
u, indices = np.unique(a, return_index=True) #去重并返回新数组在原数组中的下标
u, count = np.unique(a, return_counts=True) #返回计数
ans = np.char.add(['hello'], [' xyz']) #连接两个字符串列表
np.char.multiply('hi, ', 3)
str = np.char.center('abcde', 20, fillchar='*') #两边填充字符串到总长度 20
print(np.char.capitalize('lusoNG!')) #变成只有首字母大写的形式
print(np.char.title('i LOVe yOu !')) #每个单词首字母大写
print(np.char.lower(['AMSs', 'GOOGLE'])) #小写全部字母
print(np.char.upper(['Lsec', 'google'])) #大写全部字母
print(np.char.split('i like runoob?')) #分割字符串,默认空格为分隔符
print(np.char.split('www.runoob.com', sep='.')) #以 . 为分隔符
print(np.char.splitlines('i\nlike runoob?')) #识别 \n 为分隔符
print(np.char.splitlines('i\rlike runoob?')) #识别 \r 为分隔符
print(np.char.strip('ashokaaa aruaaanbaaa', 'a')) #移除数组元素头尾的 a 字符
print(np.char.join(':', 'lusongno1')) #用冒号分隔每个字母
print(np.char.join([':', '-'], ['baidu', 'google'])) #两个单词用不同的分隔符
print(np.char.replace('i like dooooog', 'oo', 'cc')) #替换,一对一对替换
a = np.array([0, 30, 45, 60, 90])
np.sin(a/180*np.pi/6) #sin 函数
np.degrees(np.pi/2)#把弧度转为角度
a = np.array([1.0, 5.55,  123,  0.567,  25.532])
print(np.around(a)) #四舍五入
print(np.around(a, decimals=1)) #保留一位小数
print(np.around(a, decimals=-1)) #保留到十位
print(np.floor(a)) #向下取整
print(np.ceil(a)) #向上取整
a = np.arange(9, dtype=np.float_).reshape(3, 3)
b = np.array([10, 20, 30])
print(np.add(a, b)) #广播机制,a的每一列和b的每个元素操作
print(np.subtract(a, b))
print(np.multiply(a, b))
print(np.divide(a, b))
print(np.reciprocal(a)) #求倒数
print(np.power(a, 2))
a = np.array([10, 20, 30])
b = np.array([3, 5, 7])
print(np.mod(a, b)) #求余数
print(np.remainder(a, b)) #也是求余数
a = np.array([[3, 7, 5], [8, 4, 3], [2, 4, 9]])
print(np.amin(a, 1)) #取每一行的最小
print(np.amin(a, 0)) #取每一列的最小
print(np.amax(a))
print(np.amax(a, axis=0))
a = np.array([[3, 7, 5], [8, 4, 3], [2, 4, 9]])
print(np.ptp(a))
print(np.ptp(a, axis=1)) #每一行的最大减最小
print(np.ptp(a, axis=0)) #每一列的最大减最小
print(np.percentile(a, 50))
print(np.percentile(a, 50, axis=0))
print(np.percentile(a, 50, axis=1))#求每一行的 50% 分位数
print(np.percentile(a, 50, axis=1, keepdims=True)) #保持原来的维度不变
print(np.median(a, axis=1)) #求每一行的中位数
print(np.mean(a, axis=1)) #求每一行的均值
a = np.array([1, 2, 3, 4])
wts = np.array([40, 30, 20, 10])
print(np.average(a, weights=wts)) # 求加权平均
print(np.std([1, 2, 3, 4]))
print(np.var([1, 2, 3, 4]))
a = np.array([[3, 7], [9, 1]])
print(np.sort(a)) #把每一行进行排序
print(np.sort(a, axis=0)) #把每一列拿出来进行排序
dt = np.dtype([('name',  'S10'), ('age',  int)])
a = np.array([("raju", 21), ("anil", 25),
             ("ravi",  17),  ("amar", 27)], dtype=dt)
print(np.sort(a, order='name')) #根据列名来排序
x = np.array([3,  1,  2])
y = np.argsort(x) #或者排序后的索引
nm = [3,3,2,8,1,9]
dv = [27,1,17,6,37,4]
ind = np.lexsort((dv, nm))#多级排序,先按 nm 进行排序,相同再按 dv,有电箱排序的第一关键字,第二关键字,返回索引
a = np.array([3, 4, 2, 5,10,11,8,6,4])
np.partition(a, 5) #分割,比位 3 的数字小的排前面,比它大的排后面
arr = np.array([46, 57, 23, 39, 1, 10, 0, 120])
arr.sort()
arr[np.argpartition(arr, 2)[2]] #找到数组的第 3 小(index=2)的值,速度快
arr[np.argpartition(arr, -2)[-2]] #找到数组第 2 大(index=-2)的值
a = np.array([[30, 40, 70], [80, 20, 10], [50, 90, 60]])
print(np.argmax(a)) #找到最大值对应的索引,矩阵按行优先
maxindex = np.argmax(a, axis=0)#把每一列的最大值找出来
maxindex = np.argmax(a, axis=1)#找出每一行的最大值
a = np.array([[30, 40, 0], [0, 20, 10], [50, 0, 60]])
print(np.nonzero(a)) #返回非零的元素的下标 x y 下标各一个向量
x = np.arange(9.).reshape(3,  3)
y = np.where(x > 3) #返回大于 3 的二维索引
print(x[y]) #找出大于 3 的数
x = np.arange(9.).reshape(3,  3)
condition = np.mod(x, 2) == 0
b = np.extract(condition, x) #同a = x[condition]
i = np.matrix('1,2;3,4') #字符串生成矩阵,有点诡异啊
print(i)
j = np.asarray(i)
print(j)
k = np.asmatrix(j)
print(k)
a = np.array([[1, 2], [3, 4]])
b = np.array([[11, 12], [13, 14]])
print(np.dot(a, b)) #矩阵点乘,同 a@b np.matmul(a, b)
print(np.vdot(a, b)) #计算内积,最后称为一个数
np.inner(np.array([1, 2, 3]), np.array([0, 1, 0])) #向量内积
a = np.array([[1, 2], [3, 4]])
np.linalg.det(a) #计算行列式
x = np.array([[1, 2], [3, 4]])
y = np.linalg.inv(x) #求逆
A = np.array([[1, 1, 1], [0, 2, 5], [2, 5, -1]])
b = np.array([[6], [-4], [27]])
x = np.linalg.solve(A, b) #求解 A^-1 @ b 
x = np.arange(1, 11)
y = 2 * x**2 + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x, y)
plt.show()

Congratulations, at this point, your python has been introduced, and you can practice your project below. Obviously, you can't remember them completely now, but it doesn't matter, you can use the above code as a basic version of the Xinhua dictionary, when you need it, open this blog, quickly scan with your eyes, or use Ctrl+F for keyword queries That's it.

browser-oriented programming

Learning the usage of basic data structures and numpy is not enough to support you to complete a project, but at least you have solved the dilemma of not knowing what to write when you open the IDE, because at least you can write import.

One of the disadvantages of not learning systematically is that you don't know "what's available". We can take advantage of the smart supplementary hints and supplementary features of the individual editors. You put a dot "." after each object (maybe some IDEs also need to add a Tab) and it will give you a reminder of the methods. Basically, as the name suggests, you know what these functions are for. There is no need to go to Baidu "How about numpy xxxxxx", "how about python xxxxxx" and so on. Through
insert image description here
the "." prompt, you know which function method can realize your operation, supplemented by the function interface prompt of the IDE, and you can write down this statement without even guessing. When the doc help seems incomprehensible, you can go to GOOGLE or Baidu.

Similarly, any problem that cannot be solved by the existing tools of any programming language can be targeted to the browser. What needs to be reminded is that browser-oriented programming requires you to describe the problem accurately through certain thinking, otherwise, you may not find the answer you want. For the answers found in the search, you should also carefully consider whether it caters to your question, and don't try blindly. Generally speaking, for the thrown error, just copy the error message to the browser window and search.

The so-called practice makes perfect, dripping water pierces the copper hole. A data structure, such as List, and a module, such as numpy, may not be familiar to you at first, but you will become familiar with it after repeated use in the things you want to do.

other studies

There are many things in python, especially a large number of modules that can be imported. You can explore this according to your own needs. For pandas, you can use the above method to go through its basic modules. You can refer to this blog of mine:

Pandas function method summary list query (continuous supplementation and improvement)

I don't recommend other materials, because it goes against my original intention of not recommending you to study systematically. If you really don’t agree with my idea, or your foundation is so poor that you need a tutorial for IDE installation, then you can find some system videos or Take a look at the text tutorial, I am not opposed to it, but I don't advocate it either.

You ask me what IDE to use, everyone has his own ambitions, I don't recommend it, every Hamlet has the ability to hold up the IDE he has used, and then add four characters in front of it - strongly recommended. However, since you can watch my nonsense, I naturally wouldn't recommend you to use vim. If nothing else, I don't think you can use it well. For grammar learning, jupyter notebook is good, anyway, just try it bit by bit.

Guess you like

Origin blog.csdn.net/lusongno1/article/details/125244446