Python 各函数用法

版权声明:版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/qq_31347869/article/details/87909647


Python 3有很多简单的内置函数,可以参考: Python 3内置函数

argsort 函数

argsort 函数的功能是将数组中的元素从小到大排列,并返回其对应的 index (索引)序列。

例1. 对数组 x 进行排序
此例中,x[3] = -5 最小,所以输出序列第一个值为 3,x[4] = 9 最大,所以输出序列最后一个值为 4。

>>> import numpy as np
>>> x = np.array([1, 7, 4, -5, 9, 2])
>>>> x.argsort()
array([3, 0, 5, 2, 1, 4], dtype=int64)

get 函数(字典)

Python 字典 get() 函数的功能是返回指定键的值,如果值不在字典中返回默认值。

get() 方法语法: dict.get(key, default=None)

  • key :字典中要查找的键
  • default :如果指定键的值不存在时,返回该默认值

例1. get 函数应用举例

>>> personDict = {'Name' : 'Eric', 'Age' : '31'}

>>> print("Age 值为: %s" % personDict.get('Age'))
Age 值为: 31
>>> print("Sex 值为: %s" % personDict.get('Sex', 'None'))
Sex 值为: None

items 函数(字典)

items 函数的功能是以列表返回可遍历的 (键, 值) 元组数组

items() 方法语法: dict.items()

例1. items 的使用

>>> dict = {'Name' : 'Eric', 'Age' : '21'}
>>>> print("Value : %s" % dict.items())
Value : dict_items([('Name', 'Eric'), ('Age', '21')])

operator.itemgetter 函数

operator.itemgetter 函数是由 operator 模块提供的函数,功能是用于获取对象的哪些维的数据,参数为一些序号。
例1. operator.itemgetter 函数的使用

>>> import operator
>>> a = [1, 2, 3]
>>> # 定义函数 b
>>> b = operator.itemgetter(2, 0, 1)
>>> # 获取 a 的第 2 个域、第 0 个域、第 1 个域的值
>>> b(a)
(3, 1, 2)
  • 注意 operator.itemgetter 函数获取的不是值,而是定义了一个函数,通过该函数作用到对象(a)上才能获取值。

shape 函数(numpy)

shape 函数是 numpy.core.fromnumeric 中的函数,它的功能是查看矩阵或者数组的维数,返回值是一个元组,表示为(行数,列数)。

例1. 创建一个 3 * 3 的单位矩阵 a,a.shape 返回值为矩阵的行数和列数

>>> a = eye(3)
>>> a
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
>>> a.shape
(3, 3)

例2. 创建一个一维矩阵 b(向量),b.shape 为向量的长度

>>> b = array([1, 2, 3, 4])
>>> b.shape
(4,)

例3. 创建一个 4 * 2 的矩阵 c,c.shape为矩阵的行数和列数
c.shape[0] 为矩阵 c 的第一维,即行数
c.shape[1] 为矩阵 c 的第二位,即列数


>>> c = array( [[1, 1], [1,2], [1, 3], [1, 4]] )      
>>> c     
array([[1, 1],
       [1, 2],
       [1, 3],
       [1, 4]])
>>> c.shape      
(4, 2)
>>> c.shape[0]      
4
>>> c.shape[1]      
2

例4. 一个单独的数值,shape 返回值为空

>>> shape(3)      
()

sort 函数

sorted 函数

sorted() 函数的功能是对所有可迭代的对象进行排序操作,并返回重新排序的列表。

sorted 函数语法: sorted(iterable, key=None, reverse=False)

  • iterable:可迭代对象。
  • key:主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
  • reverse:排序规则,reverse = True 降序 , reverse = False 升序(默认)。

例1. sorted 的使用

>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]

sort 与 sorted 区别:

  • sort 是应用在 list 上的方法,而sorted 可以对所有可迭代的对象进行排序操作。

  • list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。比如:

    >>> list = [5, 2, 3, 1, 4]
    >>> sortedList = sorted(list)  #  用 sorted 函数排序
    >>> print(sortedList)  
    [1, 2, 3, 4, 5]
    >>> print(list)  
    [5, 2, 3, 1, 4]  # 原列表 list 顺序没有改变
    
    >>> list.sort()  # 用 sort 函数排序
    >>> print(list)  
    [1, 2, 3, 4, 5]  # 原列表 list 顺序改变
    

sum 函数(python 自带 & numpy)

sum 函数对系列进行求和计算,返回值为计算结果。python 自带的 sum 函数和 numpy 的 sum 函数功能相同。python 自带的 sum 函数(或者 numpy 中的 sum 函数)在无参时,所有数值全部相加;axis=0 时,按列相加;axis=1 时,按行相加;

例1. 比较 python 自带 sum 函数和 numpy 的 sum 函数

>>> import numpy as np
>>> # python 自带的sum函数
>>> print(sum([[1, 2, 3], [4, 5, 5]]))
20
>>> print(sum([[1, 2, 3], [4, 5, 5]], axis = 0))
[5 7 8]
>>> print(sum([[1, 2, 3], [4, 5, 5]], axis = 1))
[ 6 14]
>>> # numpy 中的sum函数
>>> a = np.sum([[1, 2, 3], [4, 5, 5]])
>>> print(a)
20
>>> print(a.shape)
()
>>> b = np.sum([[1, 2, 3], [4, 5, 5,]], axis = 0)
>>> print(b)
[5 7 8]
>>> print(b.shape)
(3,)
>>> c = np.sum([[1, 2, 3], [4, 5, 5]], axis = 1)
>>> print(c)
[ 6 14]
>>> print(c.shape)
(2,)

tile 函数(numpy)

tile 函数位于 python 模块 numpy.lib.shape_base 中,它的功能是重复某个数组。比如tile( A,n),功能是将数组 A 重复 n 次,构成一个新的数组 B。

例1. 创建一个 a,使用 tile 复制 a 两次得到 b

>>> from numpy import *
>>> a = [0,1,2]      # 模板 a
>>> b = tile(a,2)    # b 就是通过模板 a 构建的另一个数组
>>> print(b)
[0 1 2 0 1 2]

上面的例子,输入的是数字 2,也就是重复两次,也可以不输入数字,而改用元组来表示格式.。

例2. 创建一个 a,用 tile 复制 a 两次得到 b,且 b 是一行两列

>>> from numpy import *
>>> a = [0, 1, 2]
>>> b = tile(a, (1, 2))   
>>> print(b)
[[0 1 2 0 1 2]]

例3. 创建一个 a,用 tile 复制 a 两次得到 b,且 b 是两行一列

>>> from numpy import *
>>> a = [0, 1, 2]    
>>> b = tile(a, (2, 1))   
>>> print(b)
[[0 1 2]
 [0 1 2]]

猜你喜欢

转载自blog.csdn.net/qq_31347869/article/details/87909647
今日推荐