Python 细节2

1. for循环时,变量的初始化范围:
lenth = 8for i in range(lenth):  # i的范围是[0,8)    '''    processing module      ''' for i in range(1,lenth):  # i的范围是[1,8)    '''    processing module    '''
2. list 初始化:
lenth = 8listItem1 = [1]*lenth  # 初始化一个元素都为1,大小为8的listlistItem2 = [None]*lenth  # 初始化一个大小为8的空list '''a= [None]*8aOut[58]: [None, None, None, None, None, None, None, None]b = [0]*8bOut[60]: [0, 0, 0, 0, 0, 0, 0, 0]'''
3.list 插入:
listName.insert(pos,element)'''aOut[68]: [None, None, None, None, None, None, None, None]a.insert(1,3)aOut[70]: [None, 3, None, None, None, None, None, None, None]'''
4.list的sort排序:
def cmpa(A):    return A[0],-A[1]  # A[0]升序排列,A[1]降序排列people.sort(key=cmpa) '''people = [[2,0],[1,4],[1,3],[0,8]]people.sort(key = cmpa)peopleOut[79]: [[0, 8], [1, 4], [1, 3], [2, 0]]'''
5. list 查找元素:
if element (not) in List # 查找元素是否存在 List.count(element) # 统计元素出现的次数 List.index(element) # 统计元素第一次出现的位置
6.中文显示乱码问题
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
7.保存高清图片
plt.savefig(‘result.png’, dpi=300)
————————————————
版权声明:本文为CSDN博主「云水谣CS」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/leetcodecl/article/details/81154895

猜你喜欢

转载自www.cnblogs.com/lbwnb/p/12234433.html