python日常使用细节

1. for循环时,变量的初始化范围:

lenth = 8
for i in range(lenth):  # i的范围是[0,8)
    '''
    processing module  

    '''

for i in range(1,lenth):  # i的范围是[1,8)
    '''
    processing module

    '''

2. list 初始化:

lenth = 8
listItem1 = [1]*lenth  # 初始化一个元素都为1,大小为8的list
listItem2 = [None]*lenth  # 初始化一个大小为8的空list

'''
a= [None]*8
a
Out[58]: [None, None, None, None, None, None, None, None]
b = [0]*8
b
Out[60]: [0, 0, 0, 0, 0, 0, 0, 0]
'''

3.list 插入:

listName.insert(pos,element)
'''
a
Out[68]: [None, None, None, None, None, None, None, None]
a.insert(1,3)
a
Out[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)
people
Out[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 #用来正常显示负号

猜你喜欢

转载自blog.csdn.net/leetcodecl/article/details/81154895