Python不知道学的啥
1.一个list可以存放种类数据
Extend()另一个表所有元素一个个倒进去
Append()末尾加一个元素
Print(list[a:b:c])
遍历
[起点(闭):终点(开): i+= 几]
2.dictionary {}
List[]
Tuple()
3.list查找元素的方法
① 元素 in 列表名 #这个就是一个判断句,还有not in用法
② Count #计算元素个数
③.index(元素,起点,终点) #第二第三个参数不写默认搜索整个列表
4.打乱链表的方法
Import random
random.shuffle(列表名)
5.字符串元素输出
a = '1321321'
print(a[0:4])
print(a[2])
不能修改
6.list遍历
lst=[12, -5, -22, -10, -26, 35, 0, 49, 3, -21]
print(lst[起点:终点:每隔几个])#默认为0,末,1
这样输出结果是不输出最后一位,因为-1就是最后一位
list1 = [1, 2, 3, 4, 5, 4, 3, 2, 1]
print(list1[:-1])
7.字符串多遍输出
Print(串*输出的倍数)
8.list删除指定元素
lst=[3,4,5,6,5,4,3]
lst.remove(3)
print(lst)#只删除第一个3
9.获取随机数
random.randint(0,n)
10.list内pop用法
a = list(range(2,12,2))
#[:-2].pop()
b = a[:-1].pop()
print(b)
pop不是从链表弹出,是把这个元素给出,用于赋值,最后循环停止处的元素pop出来赋值
11.一行输入数组简直有毒
首先用字符串输入
然后链表 = [int(x) for x in 字符串.split(“ ”)]
也就是转换成字符串然后按照空格分裂然后转化成字符串
a = int(input())
s = input()
num = [int(n) for n in s.split()]
id1 = 0
ans1 = num[0]
for i in range(1,a):
if num[i] > ans1:
ans1 = num[i]
id1 = i
print(ans1,id1)
12.倒序遍历方法:
确定范围,然后每次-1
for i in range(0,l)[::-1]:
13.字母转换为ASCII
s = input()
num = [ord(n) for n in s.split()] #ord是转化为ASCII码
14.if else注意事项
elseif写作elif
15.文件的写入
text_path = 'C:/Users/DELL/Desktop/' + file_name + '.txt'
file = open(text_path,"w")
file.write(msg)
file.close
16.excel的读入
import xlrd
excel_path = "C:/Users/DELL/Desktop/data visualization-2-实验二/14级各班成绩.xlsx"
excel = xlrd.open_workbook(excel_path,encoding_override="utf-8")
all_sheet = excel.sheets()
for sheet in all_sheet:
for i in range(2,sheet.nrows):
#print(es.row_values(i)[0])
#continue
#print(es.nrows)
#print( sheet.row_values(i)[1] )
all_msg += read_all_grades(sheet.row_values(1),sheet.row_values(i),sheet.row_values(i)[1])
17.enumerate()函数
正常遍历一个字符串,一个数列,
For I in xx
现在可以同时遍历下标索引和值
for id,val in enumerate(xx):
这样两个都可以用了
18.这句话什么意思?
l=[1]*len(某玩意儿)
意思是[1]这个序列扩充某倍数
19.Python应该可以智能匹配参数
def nprintf(message,n):
for i in range(n):
print(message,end=" ")
nprintf("a",3,)
nprintf(n=5,message="good")
因为第二组数据也没有报错并且自动把两个系数反了过来
20.列表形字符串与列表之间的转化
num = Eval(“[1,2,3]”)
以及sum函数
sum(num) = 6
21.一行多个输入的办法
number,digit=input().split()
22.字符串中每个单词首字母变大写
“ajsakds”.title()即可
23.字符串变成数字
Int(“123”)
24.pandas库
https://www.cnblogs.com/misswangxing/p/7903595.html
25.python文件读写相关
打开一个文件
File_name = open(“路径”,”打开的方式(w只写r只读等)”)
读取到一个字符串
filename.read()
filename.readline()一般不用
filename.readlines()
第三个可以按行读取
其实就相当于啥呢,就相当于.read()就相当于索引这个变量(文件信息作为一个变量)
26.python绘图工具
Plotly
27.数据写入CSV文件
逗号分隔值文件
https://blog.csdn.net/lbj1260200629/article/details/89600055
28.python数据转html
https://blog.csdn.net/u013783095/article/details/97759390
29.字符与ASCII之间的转化:
ord()
chr()
30.如何输入到文件结束
import sys
s = sys.stdin.read()#从控制台读入,ctrl + z
31.字符串提取指定前x位,赋值给另一个字符串
ss = s[:s.find('#')]
word12 = danci[:15]
32.循环内部修改循环变量的值
改用while,for循环range无法修改
33.字典的使用:
定义
dic = dict()
赋值,某个键第一次插入一定要判断
for i in words:
if i not in dic:
dic[i] = 1
else :
dic[i] += 1
排序
ans = sorted(dic.items(),key = lambda x:(-x[1],x[0]))
dic.items()表示全部元素
后面表示排序要用的关键字key定义为x,然后x[i]表示第几关键字,越靠前越优先,±表示升序降序
获取键的数量
print(len(dic))
字典元素的使用:
dic[i][0]:键值
dic[i][1]:value
34.文件写入
Ans是一个字符串,不管读取还是写入,都是open
wt = open("C:/Users/DELL/Desktop/result.txt","w")
wt.write(ans)
wt.close()
35.isdigit,isalpha,isalnum的使用
需要实例
随便创建一个都行
不能直接isdigit(str)
可以str2.isdigit(str1)
36.matplotlib库
教程:https://www.runoob.com/numpy/numpy-matplotlib.html
画一个函数
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,2,0.05)
p = x-2
pp = np.sin(x-2)
ppp = pp*pp
pppp = np.exp(-x*x)
y = ppp*pppp
plt.title("1-1 f(x) = sin^2(x-2)*e^-x^2")
plt.plot(x,y)
plt.savefig("C:/Users/DELL/Desktop/1_1.png")
plt.show()
Numpy函数库
https://blog.csdn.net/create115721/article/details/81266009
eg柱状图
import matplotlib.pyplot as plt
import numpy as np
N = 5
men = [22,30,35,35,26]
women = [25,32,30,35,29]
g = ["G1","G2","G3","G4","G5"]
plt.xticks([i for i in range(len(g))],g)
x = np.arange(N)
plt.bar(x,height = men,width = -0.35,label='men',align="edge",color="r")
x = np.arange(N)
plt.bar(x+0.35,height = women,width = -0.35,label='women',align="edge",color="g")
plt.title("Scores by preson")
plt.legend(loc = "NorthEast")
plt.savefig("C:/Users/DELL/Desktop/2-1.png")
plt.show()
eg.饼图
import matplotlib.pyplot as plt
import numpy as np
data = [22.2,17.6,8.8,8,7.7,6.7]
name = ["Java","Python","PHP","JavaScript","C#","C++"]
plt.pie(data,labels=name,autopct='%1.1f%%')
plt.title("The Popularity of Programming Languages")
plt.savefig("C:/Users/DELL/Desktop/3-1.png")
plt.show()
eg.散点图
import matplotlib.pyplot as plt
import random
a = range(100)#a = 0~99
x = random.sample(a,20)
y = random.sample(a,20)
for i in range(20):
x[i] = x[i]/100
y[i] = y[i]/100
b = ["red","green","blue","pink","purple","sienna","tan","violet","yellow","white","silver"]
col = []
for i in range(20):
col.append(random.choice(b))
print(col)
plt.scatter(x,y,color = col)
plt.xlabel("X")
plt.ylabel("Y")
plt.savefig("C:/Users/DELL/Desktop/4-1.png")
plt.show()
37.三维图的画法以及什么下降算法
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = np.arange(-3,3,0.1)
y = np.arange(-3,3,0.1)
X, Y = np.meshgrid(x, y)#网格的创建,这个是关键
Z = X*X + Y*Y + 2*X*Y + 3*X + Y +1
plt.xlabel('x1')
plt.ylabel('x2')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
Abs = 0.00001
x = 2.0
y = 2.0
z = x*x + y*y + 2*x*y + 3*x + y + 1
a = 0.02
xx = [x]
yy = [y]
zz = [z]
flag = 1
last = 1000
while flag==1:
zx = 2*x + 2*y +3
zy = 2*y + 2*x +1
if last-zx <= Abs:
flag = 0
last = zx
x -= a*zx
y -= a*zy
z = x*x + y*y + 2*x*y + 3*x + y + 1
xx.append(x)
yy.append(y)
zz.append(z)
ax.scatter(xx,yy,zz)
plt.show()
38.Z = sin(x)*cos(y)动态图
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
for i in range(1,10):
P = 2*np.pi + i*0.5#假装跟着时间动
x = np.arange(-P,P,0.2)
y = np.arange(-P,P,0.2)
X, Y = np.meshgrid(x, y)
Z = np.sin(X)*np.cos(Y)#我特么也不知道为什么math.sin不行
plt.xlabel('x1')
plt.ylabel('x2')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
plt.pause(0.1)
plt.cla()
plt.show()