OS module的简单使用, numpy保存文件, (zip,map,lambda)的使用

本文介绍了Python中的OS模块使用,包括获取文件路径、目录操作等;讲解了ZIP函数的打包与解包;探讨了Numpy的savez_compressed方法进行数据保存;提到了openslide库在处理数字病理切片中的应用;还分享了一些Python编程中的小技巧,如指定脚本解释器、添加路径到环境变量、argparse模块的使用等。
摘要由CSDN通过智能技术生成
  1. OS module的简单使用
# 取得当前文件的位置
path=os.path.realpath(__file__)
# path=os.path.abspath(__file__)

# 文件夹名, 文件名
os.path.dirname(path)
os.path.basename(path)

# 也可以直接获取当前文件夹
os.path.realpath("./")
# os.path.abspath("./")

a). os.getcwd()和os.path.realpath(file)的区别 link
b). realpath和abspath的区别 link

  1. zip的使用
a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]
zipped = zip(a,b)     # 打包为元组的列表
[(1, 4), (2, 5), (3, 6)]
zip(a,c)              # 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
zip(*zipped)          # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
[(1, 2, 3), (4, 5, 6)]
# 通过zip函数将两个序列压成字典
items2 = dict(zip(['a', 'b', 'c'], '123'))
  1. 判断key存在吗
if id in dict.keys():
  1. map和lambda的使用
info = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a = map(lambda x:x+1,info)
print([*a])   #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  1. numpy.savez_compressed
    Numpy中数据的常用的保存与读取方法

  2. openslide的使用 link

import openslide
slide = openslide.OpenSlide('filename‘)
slide.close()

小方法

  1. #!/usr/bin/env python3 的作用:指定由哪个解释器来执行脚本 link

  2. 把路径添加到系统的环境变量link

import sys  
sys.path.append(’引用模块的地址')  
  1. argarse.ArgumentParser.parse_known_args() link
FLAGS, unparsed = parser.parse_known_args() #unparsed保存多余参数
  1. python3 限定方法参数
def test(a:int, b:str) -> str:
    print(a, b)
    return 1000
 
if __name__ == '__main__':
    test('test', 'abc')
  1. *_的使用

*_ to express no interest in any further arguments

import numpy as np
arr_1d_bigger = np.arange(24)
arr_3d = arr_1d_bigger.reshape((2, 3, 4))
print(arr_3d)
arr = arr_3d.transpose(0,2,1)
print(arr)
6. Python 软件项目文件结构组织

PYTHON入门

Python - Error Type
Python | Accessing index and value in list
使用__slots__
如何学习Python爬虫[入门篇]?
笨办法学python
笨办法学python视频

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值