python 技巧集合【不定期更新】

1.变量前怎么加r,要在变量前加r,只需:r''+变量

imageB = r''+path

2.cv2不支持中文路径,使用此方法创建cv2对象

def cv_imread(file_path):
    cv_img = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)
    return cv_img

3.获取文件的后缀名或前缀名

import os
file = "Hello.py"

# 获取前缀(文件名称)
assert os.path.splitext(file)[0] == "Hello"

# 获取后缀(文件类型)
assert os.path.splitext(file)[-1] == ".py"
assert os.path.splitext(file)[-1][1:] == "py"

4.删除数组中重复的结果

def removeDuplicates(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    forth = 0
    back = 1
    while back <= len(nums)-1:
      if nums[forth] == nums[back]:
        nums.pop(back)
      else:
        forth += 1
        back += 1
    return len(nums)
a = [1,1,2,2,2,3,3,4,4,4,4,1,1,1]

结果:

[1, 2, 3, 4, 1]

 

 

 

你可能感兴趣的:(python)