记录python一些有用的方法,不定期更新
判断字典里是否含有某键
Python 3.X 里不包含 has_key() 函数,被 contains(key) 替代:
dict3 = {'name':'z','Age':7,'class':'First'};
print("Value : ",dict3.__contains__('name'))
print("Value : ",dict3.__contains__('sex'))
执行结果:
Value : True
Value : False
python设置坐标轴字体大小的若干方法:
plt.xticks(fontsize= ) to Set Tick Labels Font Size
ax.set_xticklabels(xlabels, fontsize = ) #to Set Tick Labels Font Size
plt.setp(ax.get_xticklabels(), fontsize =) #to Set Tick Labels Font Size
ax.tick_params(axis = 'x', labelsize = ) #to Set Tick Labels Font Size
简单把字典存入本地并读取的方法:
#保存
dict_name = {1:{1:2,3:4},2:{3:4,4:5}}
f = open('temp.txt','w')
f.write(str(dict_name))
f.close()
#读取
f = open('temp.txt','r')
a = f.read()
dict_name = eval(a)
f.close()
查看jupyter各个变量所占内存大小:
import sys
a = %who_ls
for i in a:
print(i,':',sys.getsizeof(i)/1024,'K')
去除画图的边框
fig, Axes = plt.subplots(1,1,figsize = (10,8))
for Key in list(P.keys())[:4]:
Axes.plot(x,P[Key],label = Key)
Axes.spines['top'].set_visible(False)
Axes.spines['right'].set_visible(False)
Axes.text(x[-1], P[Key][-1], Key, fontsize=15)