python基本(2)

pyhton标准库:
内置函数:

divmod(a,b):以两个(非复数)数字作为参数,并在使用整数除法时返回由它们的商和余数组成的一对数字,结果与(a // b, a % b)相同

enumerate(iterable, start=0)返回一个枚举对象。iterable 必须是一个序列、一个迭代器,或者其它某种支持迭代的对象。enumerate()返回的迭代器的__next__()方法返回一个元组,该元组包含一个计数(从start开始,默认为0)和迭代iterable得到的值。

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

flter() 函数用于过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换。该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。

#!/usr/bin/python3
def is_odd(n):
    return n % 2 == 1
tmplist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
newlist = list(tmplist)
print(newlist)

输出:[1, 3, 5, 7, 9]

float() 函数用于将整数和字符串转换成浮点数。

format来补全

>>> "{1} {0} {1}".format("hello", "world")  # 设置指定位置
'world hello world'
还有一个功能。数字格式化

在这里插入代码片

>>> print("{:.2f}".format(3.1415926));
>>> print('{:.%}'.format(0.25))
输出3,.14
       25%

在这里插入图片描述

sorted() 函数对所有可迭代的对象进行排序操作。

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

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

>>>example_list = [5, 0, 6, 1, 2, 7, 3, 4]
>>> sorted(example_list, reverse=True)
[7, 6, 5, 4, 3, 2, 1, 0]

super()函数是用来调用父类的一个方法

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class FooParent(object):
    def __init__(self):
        self.parent = 'I\'m the parent.'
        print ('Parent')
    
    def bar(self,message):
        print ("%s from Parent" % message)
 
class FooChild(FooParent):
    def __init__(self):
        # super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类B的对象 FooChild 转换为类 FooParent 的对象
        super(FooChild,self).__init__()    
        print ('Child')
        
    def bar(self,message):
        super(FooChild, self).bar(message)
        print ('Child bar fuction')
        print (self.parent)
 
if __name__ == '__main__':
    fooChild = FooChild()
    fooChild.bar('HelloWorld')

reversed 函数返回一个反转的迭代器

 
# 字符串
seqString = 'Runoob'
print(list(reversed(seqString)))
 
# 元组
seqTuple = ('R', 'u', 'n', 'o', 'o', 'b')
print(list(reversed(seqTuple)))
 
# range
seqRange = range(5, 9)
print(list(reversed(seqRange)))
 
# 列表
seqList = [1, 2, 4, 3, 5]
print(list(reversed(seqList)))

输出:
[‘b’, ‘o’, ‘o’, ‘n’, ‘u’, ‘R’]
[‘b’, ‘o’, ‘o’, ‘n’, ‘u’, ‘R’]
[8, 7, 6, 5]
[5, 3, 4, 2, 1]

哈希值:hash(散列、杂凑)函数,是将任意长度的数据映射到有限长度的域上。直观解释起来,就是对一串数据m进行杂糅,输出另一段固定长度的数据h,作为这段数据的特征(指纹)。

>>>hash('test')            # 字符串
2314058222102390712
>>> hash(1)                 # 数字
1
>>> hash(str([1,2,3]))      # 集合
1335416675971793195
>>> hash(str(sorted({'1':1}))) # 字典
7666464346782421378

set函数

>>>x = set('runoob')
>>> y = set('google')
>>> x, y
(set(['b', 'r', 'u', 'o', 'n']), set(['e', 'o', 'g', 'l']))   # 重复的被删除
>>> x & y         # 交集
set(['o'])
>>> x | y         # 并集
set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u'])
>>> x - y         # 差集
set(['r', 'b', 'u', 'n'])

math.floor(x) 返回最大的小于等于x的整数和math.ceil(x)返回最小的大于等于x的整数

猜你喜欢

转载自blog.csdn.net/chengmo123/article/details/86494736