栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python(10)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python(10)

1.定义一个生成器函数,生成1-10
使用next(generator)方法获取1-10
使用for循环获取

def get_num():
    for num in range(1, 11):
        yield num

gen = get_num()
print(type(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))

结果:

2.自己定义一个MyIterator, 实现range的功能
range(start, stop, step) => MyIterator
range(10) # start=0, stop=10, step=1
range(0,10)
range(0,10,2)
MyIterator(10)
MyIterator(0,10)
MyIterator(0,10,2)

class MyRange:
    def __init__(self, start=None, stop=None, step=1):
        if start is not None and stop is  None:   #传递一个参数
           self.stop = start
           self.start = 0
        if start is not None and stop is not None:
           self.start = start
           self.stop = stop
        self.step = step

    def __iter__(self):
        return self

    def __next__(self):
        if self.start < self.stop:
            data = self.start
            self.start += self.step
            return data
        else:
            raise StopIteration

for i in MyRange(10):
    print(i)
for i in MyRange(0, 10):
    print(i)
for i in MyRange(0, 10, 2):
    print(i)

结果:


3. re中函数的使用
match
fullmatch
search
findall
finditer
split
sub
subn
complie

import re
"""def match(pattern, string, flags=0):
    Try to apply the pattern at the start of the string, returning
    a Match object, or None if no match was found."""

pattern = "hello"
string = "hello world"
result = re.match(pattern, string)
print(result, type(result))
"""def fullmatch(pattern, string, flags=0):
    Try to apply the pattern to all of the string, returning
    a Match object, or None if no match was found."""

pattern = "hello"
string = "hello world"
result = re.fullmatch(pattern, string)
print(result)
"""def search(pattern, string, flags=0):
    Scan through string looking for a match to the pattern, returning
    a Match object, or None if no match was found."""

pattern = "hello"
string1 = "hello world"
string2 = "world hello"
string3 = "hello world hello"
result = re.search(pattern, string1)
print(result)
result = re.search(pattern, string2)
print(result)
result = re.search(pattern, string3)
print(result)
"""def sub(pattern, repl, string, count=0, flags=0):
返回值类型:字符串
repl:替换字符串
string:原字符串
count:替换的次数,如果没有指定count默认全部替换"""

result = re.sub(",", "-", "计算机,软件,网络")
print(result)
result = re.sub(",", "-", "计算机,软件,网络", 1)
print(result)
"""def subn(pattern, repl, string, count=0, flags=0):
与sub的区别:
返回两个值(格式:替换之后的字符串,替换次数)"""

result = re.subn(",", "-", "计算机,软件,网络",)
print(result)
result = re.subn(",", "-", "计算机,软件,网络", 1)
print(result)
"""def split(pattern, string, maxsplit=0, flags=0):
返回值类型:列表"""

string = "计算机,软件,网络"
pattern = ","
result = re.split(pattern, string)
print(result)
result = re.split(pattern, string, 1)
print(result)
"""def findall(pattern, string, flags=0):
返回值类型:列表(字符串)"""

pattern = "hello"
string3 = "hello world hello"
result = re.findall(pattern, string3)
print(result)
"""def finditer(pattern, string, flags=0):
返回迭代器,迭代的对象是match obj,不是字符串"""

pattern = "hello"
string3 = "hello world hello"
result = re.finditer(pattern, string3)
print(result)
for i in result:
    print(i)
"""def compile(pattern, flags=0):
    Compile a regular expression pattern, returning a Pattern object.
compile:编译(一次编译,多次使用:提高了效率)
前面使用的match, search, fullmatch...实现的都是先编译pattern,flags->compile的对象,
再调用compile对象的match, search, fullmatch......    """

pattern = "hello"
string3 = "hello world hello"
compile_obj = re.compile(pattern)
print(compile_obj.match(string3))
print(compile_obj.search(string3))

结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/529428.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号