Python中生成器和re中函数大全!

1.定义一个生成器函数,生成1-10

使用next(generator)方法获取1-10
使用for循环获取

def get_num():
    for num in range(1, 11):
        yield num
 
 
test = get_num()
print(type(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))
print(next(test))

截图:

image-20220709174437668

2.模拟range的功能,自己建立一个range:MyRange

range(10)
range(1, 10)
range(1, 10, 1) =>
start, stop, step
range(10, 1, -1)
range(10, -1, -1)
range(-10, -1, 1)
range(-1, -10, -1)

class MyRange:
 
    def __init__(self, *args):
        if len(args) == 1:
            self.start = 0
            self.stop = args[0]
            self.step = 1
        if len(args) == 2:
            self.start, self.stop = args
            self.step = 1
        if len(args) == 3:
            self.start, self.stop, self.step = args
            if self.step == 0:
                raise ValueError('range() arg 3 must not be zero')
        # print(args, type(args))
        # print(self.start, self.stop, self.step)
 
    def __iter__(self):
        return self
 
    def __next__(self):
        data = self.start
        if self.step > 0:
            if self.start < self.stop:
                self.start += self.step
                return data
            else:
                raise StopIteration
        if self.step < 0:
            if self.start > self.stop:
                self.start += self.step
                return data
            else:
                raise StopIteration
 
    pass
 
 
print(list(MyRange(10)))
print(list(MyRange(1, 10)))
print(list(MyRange(1, 10, 1)))
print(list(MyRange(10, 1, -1)))
print(list(MyRange(10, -1, -1)))
print(list(MyRange(-10, -1, 1)))
print(list(MyRange(-1, -10, -1)))

截图:

image-20220709174539308

3. re中函数的使用(自己写用例来使用):

match
import re
pattern = "hello"
string = "hello world"
result = re.match(pattern, string)
print(result, type(result))

截图:

image-20220709175102656

fullmatch
import re
pattern = "hello"
string = "hello"
match_obj = re.fullmatch(pattern, string)
print(match_obj)

截图:

image-20220709175129604

search
pattern = "hello"
string = "world hello"
match_obj = re.search(pattern, string)
print(match_obj)

截图:

image-20220709175219703

findall
string3 = "hello world hello"
pattern = "hello"
result = re.findall(pattern, string3)
print(result)

截图:

image-20220709175244765

finditer
string = "hello world hello"
pattern = "hello"
result = re.finditer(pattern, string)
print(result)

截图:

image-20220709175318238

split
string = "计算机,软件,网络"
pattern = ","
result = re.split(pattern, string, maxsplit=1)
print(result)

截图:

image-20220709175338101

sub
result = re.subn(",", "-", "计算机,软件,网络", )
print(result)
result = re.subn(",", "-", "计算机,软件,网络", 1)
print(result)

截图:

image-20220709175401383

subn
result = re.subn(",", "-", "计算机,软件,网络", 1)
print(result)
result = re.subn(",", "-", "计算机,软件,网络",)
print(result)

截图:

image-20220709175425050

complie
string = "hello world hello"
pattern = "hello"
compile_obj = re.compile(pattern)
print(compile_obj.search(string))
print(compile_obj.findall(string))
print(compile_obj.match(string))

截图:

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

截图:

image-20220709175447837

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Royyic

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值