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]

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章