python基本用法以及库函数的使用

一、numpy

1、array二维数组获取行列数

参考博客连接:https://www.cnblogs.com/hezhiyao/p/7879812.html

import numpy as np

x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]])
# 输出数组的行和列数
print x.shape  # (4, 3)
# 只输出行数
print x.shape[0] # 4
# 只输出列数
print x.shape[1] # 3

2、numpy创建零矩阵和255矩阵

import numpy as np
	
zero = np.zeros((512, 512))
zero = np.ones((512, 512))*255

3、numpy获取最小值/最小值索引

# 获取最小值
xmin = np.min(arr)
# 获取最小值索引,但该函数获得的是numpy数组平铺后的索引,也就是一维索引
index = xmin.argmax()

二、pandas操作excel

在这里插入图片描述

1、读取excel

import pandas as pd

excel = pd.read_excel(excel_path)

2、获取id对应列的数据

excel['ID'].values.tolist()

3、获取某列的值获取所有的索引

index = excel[excel['FID']==fid].index.tolist()
# 返回的索引列表,本excel确定索引值唯一,所以确定只有一个索引值
index = index[0]

三、cv2

1、BRG转RGB

opencv读取时安装BGR方式读取,但是再写入文件时,还是RGB方式,比如以下代码,先opencv读取,再写出,还是RGB格式,中间不需要将BRG转为RGB

img = cv2.imread(join(img_path, img_id), i)
# BGR转为RGB
# img_rgb2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(join(img_path,img_id), img)

参考连接:https://blog.csdn.net/caomin1hao/article/details/80590673

img_rgb2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

4.python基本操作

1.一文搞懂Python函数(匿名函数、嵌套函数、闭包、装饰器’@’)!

参考链接:https://bbs.huaweicloud.com/blogs/183686

  • 装饰器@的使用
    1)形式和作用:

    • @装饰器函数,等价于,原函数名=装饰器函数(原函数名)
    • 装饰器就是通过装饰器函数,来修改原函数的一些功能,使得原函数不需要修改,也就是扩展了原函数的功能

    2)装饰器函数写法:

    • 简单装饰器
# 简单装饰器
def my_decorator(func):
    def wrapper():
        print('wrapper of decorator')
        func()
    return wrapper

@my_decorator
def greet():
    print('hello world')  
    
greet()

结果:
在这里插入图片描述

  • 带参数的装饰器
# 带参数的装饰器
def my_decorator(func):
    def wrapper(tt): # tt用于接收message信息
        print('wrapper of decorator')
        func(tt)
    return wrapper
@my_decorator
def greet(message):
    print(message)  
    
greet('hello world')

输出结果及分析:首先从greet(‘hello world’)调用代码,先从@my_decorator修饰器开始执行,当前状态即执行my_decorator(func)函数,func=greet(message)。返回return wrapper,即调用wrapper(tt)函数,这时tt存储的是message信息,先输出print(‘wrapper of decorator’),之后执行func(tt)=greet(message),结束。
在这里插入图片描述

  • 通用的带参数的装饰器
# 通用的带参数的装饰器
def my_decorator(func):
    def wrapper(*args, **kwargs):
        print('wrapper of decorator')
        func(*args, **kwargs)
    return wrapper

@my_decorator
def greet(message):
    print(message)
    
greet('hello world')

结果:
在这里插入图片描述

  • 保留原函数的元信息,将原函数的元信息,拷贝到对应的装饰器函数里
# 保留原函数的元信息,将原函数的元信息,拷贝到对应的装饰器函数里
import functools
def my_decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print('wrapper of decorator')
        func(*args, **kwargs)
    return wrapper

@my_decorator
def greet(message):
    print(message)
    
greet('hello world')
  • 类装饰器,依赖函数__call__()
# 类装饰器,依赖函数__call__()
class Count:
    def __init__(self, func):
        self.func = func
        self.num_calls = 0

    def __call__(self, *args, **kwargs):
        self.num_calls += 1
        print('num of calls is: {}'.format(self.num_calls))
        return self.func(*args, **kwargs)

@Count
def example():
    print("hello world")

example()

结果:
在这里插入图片描述

  • 带参数的装饰器
    构造函数,_ init _(self, outputCategory=SingleRasterResult)
    参数outputCategory得到@StateSwapper(1)中的数值1,即outputCategory=1
class StateSwapper(object):
    SingleRasterResult = 1
    NoSingleRasterResult = 2
    def __init__(self,
            outputCategory=SingleRasterResult): # 参数outputCategory得到@StateSwapper(1)中的参数1,即outputCategory=1
        self.__outputCategory = outputCategory 
    def __call__(self,wrapper):
        print(self.__outputCategory) 
        print(wrapper) # wrapper=funA()
        print('-----------------')
        wrapper() 

@StateSwapper(1)
def funA():
    print('funA')

结果:
在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值